Laravel 9 Authentication is very easy to apply inside of a project. We need an authentication system for keeping our application more secure and private. Making authentication in Laravel is quite easy. It has a built-in solution for authentication and various facility to customize it according to our requirements. If you are new to Laravel 9 then in this post I’ll show you the step-by-step process for making an authentication system in Laravel 9. Let’s follow the step-by-step process for making an authentication system in Laravel 9.
- Create a Laravel 9 project
- Install the Laravel UI package
- Generate auth scaffolding
- Install NPM dependencies
- Test the authentication system
- Restrict the required routes
- Change the necessary configuration
#Laravel 9
As you may know, Laravel transitioned to yearly releases with the release of Laravel 8. Previously, major versions were released every 6 months. This transition is intended to ease the maintenance burden on the community and challenge our development team to ship amazing, powerful new features without introducing breaking changes. Therefore, we have shipped a variety of robust features to Laravel 8 without breaking backward compatibilities, such as parallel testing support, improved Breeze starter kits, HTTP client improvements, and even new Eloquent relationship types such as “has one of many”.
Therefore, this commitment to ship great new features during the current release will likely lead to future “major” releases being primarily used for “maintenance” tasks such as upgrading upstream dependencies, which can be seen in these release notes.
Laravel 9 continues the improvements made in Laravel 8. x by introducing support for Symfony 6.0 components, Symfony Mailer, Flysystem 3.0, improved route:list
output, a Laravel Scout database driver, new Eloquent accessor/mutator syntax, implicit route bindings via Enums, and a variety of other bug fixes and usability improvements. You can also learn how to make laravel 8 authentication from another article.
Create a Laravel 9 project
First, we have to create a Laravel 9 project. If you already installed Laravel 9 then skip this step and look forward. If you are using a lower PHP version like 5.6 or 7.4 then you have to set the proper Laravel 9 PHP version. In their official website, Laravel said a minimum of 8 versions of PHP is needed to install the laravel 9.
composer create-project laravel/laravel laravel9
Install the Laravel UI package
Install the Laravel UI official package for making auth scaffolding in Laravel 9. Run the composer command to install the Laravel UI package.
composer require laravel/ui
Generate AUTH scaffolding With Bootstrap
After installation of the Laravel UI package. We are now able to scaffold our AUTH with Bootstrap, VUE, React, etc. If we want to generate a scaffold with Bootstrap then we have to run the command like below.
php artisan ui bootstrap --auth
Or you can make your application authenticated using Vue JS in this way you can get the default scaffold on Vue.
Generate AUTH scaffolding With VUE JS Wih
php artisan ui vue --auth
Install NPM dependencies
In this step, we have to install all our NPM dependencies. To install NPM dependencies run the command given below.
npm install && npm run dev
To compile assets run the command npm run dev
npm run watch
Test the Laravel 9 authentication system
Now our Laravel 9 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 Register Check
example.com/register
Disable Registration System
If you want to disable the new user registration system. Then go to the web.php route file and change the auth route.
Auth::routes(['register' => false]);
Route restriction
After successful installation of Laravel 9 auth system. We can protect our routes for unauthenticated users by using auth middleware in our routes or controller.
Route::get('dashboard', 'App\Http\Controllers\UserController@dashboard')->middleware('auth');
or we can protect it 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();
Change the necessary configuration
Laravel 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';
Laravel Username customization
Laravel auth system by default checks user email. If we want to check username instead of checking email then we have to 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 9 authentication will help you to make your own Laravel 9 authentication system. If this tutorial post helps you then please share this tutorial with others.
Is this snippet still work? if not working the latest version fo laravel just let us know in the comment then we will update the process with laravel version of Laravel.
Please contact with us for need any help Contact Us
Hi, My name is Shamim. I am a freelance PHP developer in Bangladesh. I have been working as a freelance developer since 2014. I am a passionate and creative web development person. As a senior level, I focus on your requirements in detail and deliver high-quality work on your budget.
Thank you very much, Shamim!