Laravel 9 Authentication Tutorial
In this blog, we will see step by step how to implement authentication in your Laravel 9 application.
What is Laravel?
Laravel is an open-source PHP web application framework with expressive, elegant syntax. It is an MVC framework for building simple to complex web applications using the PHP programming language.
Laravel strictly follows the MVC (Model-View-Controller) architectural pattern. It is known for its beautiful and elegant syntax as a web framework.
Key Features of Laravel
Some of the main features of Laravel are:
- Eloquent ORM
- Query builder
- Reverse Routing
- Restful Controllers
- Migrations
- Database Seeding
- Unit Testing
- Homestead
- Source code hosted on GitHub and licensed under MIT License.
- Most Starred PHP Framework for custom software development on Github.
- Its ability to use all of the new features of PHP sets it apart.
- Friendly online community
- Detailed documentation
- Security
New Features in Laravel 9
- Minimum PHP Requirement
- New Design for routes:list
- Anonymous stub migration
- New Query Builder Interface
- PHP 8 String Functions
For the straightforward phase of auth scaffolding, Laravel offers a very straightforward and cool UI module. Using bootstrap, react, and vue, the Laravel UI composer package offers straightforward authentication capabilities including login, registration, password reset, email verification, and password confirmation.
To protect the privacy of our application, we require an authentication method. It’s simple to create authentication in Laravel. It features a built-in authentication method and a number of customization options to meet our needs. If you’re new to Laravel 9, I’ll walk you through the process of creating an authentication system in this post.
To make authentication in Laravel 9, follow these steps:
- Install the Laravel UI package by running the following command:
composer require laravel/ui
. - Run the following command to install the authentication scaffolding:
php artisan ui:auth
. - Set up the database connection in your .env file.
- Run the following command to create the database tables:
php artisan migrate
. - In your routes file (usually located at routes/web.php), add the following lines to define the routes for the authentication pages:
Auth::routes(); Route::get('/home', 'HomeController@index')->name('home');
In your app/Http/Controllers/Auth/LoginController.php file, add the following line in the redirectTo() method to specify the route to redirect to after successful login:
protected function redirectTo() { return '/home'; }
In your app/Http/Controllers/Auth/RegisterController.php file, add the following line in the redirectTo() method to specify the route to redirect to after successful registration:
protected function redirectTo() { return '/home'; }
- In your views/layouts/app.blade.php file, add the following lines to include the authentication links in the layout:
@guest <a href="{{ route('login') }}">Login</a> <a href="{{ route('register') }}">Register</a> @else <a href="#" onclick="event.preventDefault(); document.getElementById('logout-form').submit();">Logout</a> <form id="logout-form" action="{{ route('logout') }}" method="POST" style="display: none;"> @csrf </form> @endguest
Test the authentication by visiting the login and registration pages in your web browser. You should now be able to register a new user and log in with that user.