The authentication system is a very important part for every web application to securing the application from the unauthenticated user. If you are new in Laravel 7 then in this post I'll show you the step by step process for making authentication system in Laravel 7. The auth scaffolding completely removed from Laravel 7 which was available until Laravel version 6. We have to install the laravel/ui
official package before the start. Let's follow the step by step process for making authentication system in Laravel 7.
First, we have to create a Laravel 7 project. If you already installed Laravel 7 then skip this step and look forward.
composer create-project laravel/laravel laravel7
Install the Laravel UI official package for making auth scaffolding in Laravel 7. Run the composer command to install Laravel UI package.
composer require laravel/ui
After installation of Laravel UI package. We are now able to scaffold our auth with Bootstrap, Vue, React etc. If we want to generate scaffold with Vue then we have to run command like below.
php artisan ui vue --auth
In this step, we have to install all our NPM dependencies. To install NPM dependencies run the command given below.
npm install
to compile assets run the command npm run dev
npm run dev
Now our Laravel 7 auth system is ready to use. To check authentication is successfully installed or not. Please browse the links given below.
To login check
example.com/login
To registration check
example.com/register
After successfully installation of Laravel 7 auth system. We can protect our routes for unauthenticated users by using auth middleware in our routes or controller.
Route::get('dashboard', 'UserController@dashboard')->middleware('auth');
or we can protect by our controller in the constructor function.
<?php
class UserController extends Controller
{
public function __construct()
{
$this->middleware('auth');
}
public function dashboard(){
//
}
...
}
To check user authenticated or not in the view or anywhere in the controller we can use auth()->check()
if(auth()->check()){
// If the user only authenticated
}
To get current authenticated user data.
$user = auth()->user();
Redirect path customization
When a user successfully login the default auth system redirects the user to /home path. If we want to change the path we have to change public const HOME = '/home';
from the RouteServiceProvider
public const HOME = '/dashboard';
Username customization
Laravel auth system by default check user email. If we want to check username instead of checking email then we have add this method into the login controller.
// app/Http/Controllers/Auth/LoginController.php
public function username()
{
return 'username';
}
Hope this step by step tutorial for Laravel 7 authentication will help you to make your own Laravel 7 authentication system. If this tutorial post helps you then please share this tutorial with others.