This tutorial is focused on laravel multi auth. Implementing laravel multi auth using guards is highly significant for large-scale applications. When working on extensive projects, it is advisable to utilize distinct tables. For instance, you would typically use the “users” table for site user registration and the “admins” table for administrative users to bolster security measures.

In Laravel, you can create multiple authentication systems using guards to handle different types of users or roles, such as user authentication and admin authentication. This allows you to have separate login systems for different parts of your application.

  • 1. Create a New Guard

    In Laravel, guards define how users are authenticated. You can create a new guard using the artisan command. For example, let’s create an “admin” guard:

    Copy to Clipboard
    This will generate an AdminGuard class in the app/Guards directory. You can customize this guard to fit your needs.
  • 2. Authentication Configuration Settings

    In this step, we will introduce a new admin guard by editing the “auth.php” file in the configuration directory. To begin, open the “auth.php” file and insert the following code snippet.

    config/auth.php

    Copy to Clipboard
  • 3. Create migration and model for admins

    To make the model and migration for admins table, run the following command

    Copy to Clipboard

    This process will generate both a migration and a model. Navigate to the “database/migrations” directory, open the migration file for the admin, and make the following modifications:

    Copy to Clipboard

    Now that we’ve defined our tables, it’s time to execute the database migration:

    Copy to Clipboard

    Open the Admin model in app/Models/Admin.php and add the following

    Copy to Clipboard
  • 4. Create a New User Provider

    You’ll also need a user provider for the new guard. Let’s create an AdminProvider:

    Copy to Clipboard

    This will generate an AdminProvider class in the app/Providers directory. Customize it to fetch admin users from your database.

    Update the config/auth.php file to use your new provider:

    Copy to Clipboard
  • 5. Create Admin Authentication Routes and Views

    Create routes and views for admin authentication. For example, in routes/web.php, you can define admin routes like this:

    Copy to Clipboard

    Create an AdminAuthController to handle admin authentication, similar to how the AuthController handles user authentication. You can use the AdminGuard you created for this controller.

  • 6. Implement Admin Authentication Logic

    Implement admin authentication logic in your AdminAuthController. You can use the Auth facade to attempt authentication using the admin guard

    Copy to Clipboard
  • 7. Create Admin Middleware

    If you want to protect certain routes for admin users, create an admin middleware. For example:

    Copy to Clipboard

    Implement the middleware logic to check if the user is authenticated with the admin guard and apply it to the routes or controllers as needed

  • 8. Create Admin Views

    Create views for the admin authentication pages (login, registration, etc.) in the resources/views/admin directory. Customize them according to your application’s design.

  • 9. Update Route Middleware

    In your Kernel.php file (app/Http/Kernel.php), add the admin middleware to the $routeMiddleware array:

    Copy to Clipboard
  • 10. Use the Admin Guard

    In your application, when you need to use the admin guard for authentication, specify it in your code, routes, or middleware, like this:

    Copy to Clipboard

    That’s it! You’ve created multiple authentication systems in Laravel using guards. You can extend this approach to create additional guards and authentication systems as needed for your application.