Laravel Authentication System

In this post , we will show you how to build a complete authentication system in Laravel 12 – including login, register, and logout – step by step.

prerequisites:

  • Fresh Laravel Installed // to install
    • laravel use composer create-project laravel/laravel auth-demo
  • Database ready
  • Composer installed
  • A Active MySQL database
  • Some basic php and Laravel knowledge

Setup Laravel Project

First create fresh Laravel projects

laravel new auth-demo or composer create-project laravel/laravel auth-demo
cd auth-demo
php artisan serve
installing Laravel Project by composer

Visit by typing http://127.0.0.1:8000 in your browser to confirm Laravel is running.

Configure Database

Create new database on your local server and name it auth_db and configure it. To configure update .env file on the root directory of the project.

DB_DATABASE=auth_demo
DB_USERNAME=root
DB_PASSWORD=

Users Table & Migration

Laravel by default comes with users migration and User models.

In Laravel we use php artisan migrate to migrate.

php artisan migrate

The above command create users table on your authdb databases.

Authentication Routes & Controller

Route and Controller is the most focussed area on the Laravel development concepts.

Route: define an application how to respond incoming request.

Controller is the middle man of view and models.

Lets create Controller by the following commands.

Next, let’s create an AuthController:

php artisan make:controller AuthController


After creating Controller the update routes/web.php

User Registration

User registration is the crud part of authentication of users.

Open app/Http/Controllers/AuthController.php and add registration logic:

Create view part

Create the registration form at resources/views/auth/register.blade.php:

In these tutorial we use bootstrap for all view parts , registeration,login and dashboard

User Login Form/ User Login

Update AuthController

Add login logic in AuthController.php:

User Logout

Add logout logic in AuthController.php:

To get dashboard after login create view blade template.

Add a logout button in resources/views/dashboard.blade.php:

<form method=”POST” action=”{{ route(‘logout’) }}”>

@csrf

<button type=”submit”>Logout</button>

</form>

Dashboard (Protected Route)

Create a simple dashboard view in resources/views/dashboard.blade.php:

<h1>Welcome, {{ Auth::user()->name }}</h1>
<p>You are logged in!</p>

Protect it using middleware in routes/web.php:


Route::get('/dashboard',[AuthController::class, 'dashboard']) ->middleware('auth')
->name('dashboard');

If you need full code

Github view source code

Laravel Auth-system Install steps

Installation Steps

1. CLone the project

2. Extract the project

3. on auth-demo

    composer update // to install all packages.

4. php artisan migrate // to generate migrations.

5. run the server by type :// php artisan serve

Leave a Comment