Laravel 9 - Creating an Authentication System

By Sumeet Shroff
Last Updated On : February 10, 2023
Laravel 9 -  Creating an Authentication System

Laravel 9: Creating an Authentication System Laravel, a PHP-based web framework, offers a simple and easy-to-use authentication system. In this article, we will go through the steps to create an authentication system in Laravel 9, starting with the installation of the framework and making use of the built-in "make:auth" command.

Step 1: Installing Laravel 9

The first step is to install Laravel 9 on your system. To do this, you will need to have composer installed. If you don't have it, you can download it from the official website. To install Laravel 9, open your terminal and run the following command:

composer create-project --prefer-dist laravel/laravel your-project-name

This will create a new Laravel project in a directory named "your-project-name".

Step 2: Running the make:auth Command

The next step is to run the "make:auth" command. This command will generate the necessary views and controllers for authentication, including login and registration pages. To run the command, navigate to your project directory in the terminal and run the following command:

php artisan make:auth

Step 3: Setting up the Database

Now that the authentication views and controllers have been generated, we need to set up a database for our application. Laravel makes it easy to connect to a database and provides support for several different database management systems, including MySQL, PostgreSQL, and SQLite. To set up the database, you will need to configure the database credentials in the ".env" file located in the root of your project.

Step 4: Creating the Login and Registration Pages

With the database set up and the "make:auth" command run, we now have the login and registration pages in place. You can access these pages by navigating to the "/login" and "/register" routes in your application.

Step 5: Adding Authentication Middleware

To secure routes in your application and restrict access to certain pages to only authenticated users, you can use Laravel's built-in authentication middleware. This middleware can be added to any routes you want to secure. To add the authentication middleware to a route, open the "web.php" file located in the "routes" directory and add the following code:

Route::middleware(['auth'])->group(function () {
    Route::get('/home', function () {
        return view('home');
    });
});

This will restrict access to the "/home" route to only authenticated users.

Step 6: Testing the Authentication System

With everything in place, you can now test the authentication system by visiting the "/login" and "/register" routes in your application. You should be able to register a new user and then log in with those credentials.

Conclusion

In this article, we have gone through the steps to create an authentication system in Laravel 9. Laravel makes it easy to set up an authentication system, thanks to its built-in "make:auth" command and support for various database management systems. By following the steps outlined in this article, you should be able to get a basic authentication system up and running in no time.

Categories

Latest Blogs

Ready to Amplify Your Services with our Premium White Label Web Design

Are you looking to expand your service offerings without the overhead of an in-house design team? Our white label web design services are the perfect solution. We provide top-notch, fully customizable web designs that seamlessly blend with your brand, allowing you to offer additional value to your clients.
Contact Us Today to Start Partnering with Our White Label Web Design Experts!
© 2024 · Prateeksha Web Design. Built with Gatsby All rights reserved | Privacy Policy
Designed by Prateeksha Web