Laravel Separate Admin Panel | Multiple Authentication System Using Guards

In this tutorial you will learn about the Laravel Separate Admin Panel | Multiple Authentication System Using Guards and its application with practical example.

Laravel 5.8 Multiple Authentication Using Custom Guard

In this Laravel multi (auth) authentication tutorial we will learn how to create separate admin panel or login using custom guard. Laravel multi (auth) authentication system allows to create multiple users login in single application.

While developing a laravel application there comes situations where we want create separate user login or separate user/admin panel for users having different privileges or access rights in same application. In laravel this can be achieved in following ways –

1. Laravel multi (auth) authentication using middleware

2. Laravel multi (auth) authentication Using Guards

In one of my previous article we have created separate admin login using laravel multi (auth) authentication using MIddleware In this article we will creating separate admin login using laravel multi (auth) authentication system with guard.

Install Laravel 5.8

First of all we need to create a fresh laravel project, download and install Laravel 5.8 using the below command

Configure Database In .env file

Now, lets create a MySQL database and connect it with laravel application. After creating database we need to set database credential in application’s .env file.

Create User Authentication Scaffolding

Laravel comes with in-built user authentication system, use the below command to generate default authentication scaffolding for user –

This command will generate required authentication Controller files, views and add routes in our web.php routes file that are required for user authentication system.

Now, we have to define table schema for admins table. Open terminal and use the following artisan command to generate <timestamp>create_admins_table.php migration.

Once this command is executed you will find a migration file created under “database/migrations”. Open migration file created (<timestamp>create_admins_table.php) and modify up method as following –

Run Migration

Now, run following command to migrate database schema.

After, the migration executed successfully the admins table will be created in database along with migrations, password_resets and users table.

Next we need to create a Admin model class. To make the model for the admins, run the following command:

Again our Admin model is really simple for now, this model will be like the user model and extends the Authenticable class. Open Admin model in app/Admin.php and add the following:

app/Admin.php

Next we need to add a new guard for admin. Laravel guards are used for authentication which manages multiple authenticated instances from multiple tables. Open config/auth.php file and add a custom guard and provider for admins. Add admin guard in guards array as following:

Now we need to add a provider for admins in providers array as following:

Next, we need to create the route for admin authentication. Open the routes/web.php file and add the following code in it.

Now, we will create admin login and home/dashboard controller files. For simplicity we’ll copy user auth login controller file:

into app/Http/Controllers/Admin/Auth and fix the class namespaces and update Admin LoginController as following:

Create HomeController.php file into app/Http/Controllers/Admin directory and update it as following:

Now we need to update the guest middleware RedirectIfAuthenticated for the admin auth guard. Open app/Http/Middleware/RedirectIfAuthenticated.php and add a check for the admin guard, so the handle method will now look something like this:

Update Exception Handler

At the moment if an user is authenticated they will redirect back to the same place, no matter what the guard is. Open app/Exceptions/Handler.php and add the unauthenticated method as following:

app/Exceptions/Handler.php

Add following directives on top:

Add following method in handler class:

This will override the parent handler method.

Next we we will create all relevant views files for admin.

Admin Layout:-

Create the following directory resources/views/admin/layouts and in it we’ll add a new file app.blade.php and put following code in it:

resources/views/admin/layouts/app.blade.php

Admin Login:-

Create the following directory resources/views/admin/auth and in it we’ll add a new file login.blade.php and put following code in it:

resources/views/admin/auth/login.blade.php

Admin Home/Dashboard:-

Create the following directory resources/views/admin/ and in it we’ll add a new file home.blade.php and put following code in it:

resources/views/admin/home.blade.php

Create Admin Seeder

Generate a admin seeder file with following artisan command:

Lets open database/seeds/AdminsTableSeeder.php file created and populate admin user in it as following:

Call Admin Seeder

Lets open database/seeds/DatabaseSeeder.php and call AdminsTableSeeder in run method as following:

database/seeds/DatabaseSeeder.php

Run Admin Seeder

Lets open terminal and run seeder using following artisan command:

Start Application Server

Now we are ready to run our example so lets start the development server using following artisan command –

Now, open the following URL in browser to see the output –

http://localhost:8000/admin/login

Output:-

admin_login

http://localhost:8000/admin/

Output:-

admin_home

http://localhost:8000/register/

Output:-

user_register

http://localhost:8000/login/

Output:-

user_login

http://localhost:8000/home/

Output:-

user_dashboard

 

In this tutorial we have learn about the Laravel Separate Admin Panel | Multiple Authentication System Using Guards and its application with practical example. I hope you will like this tutorial.