Wednesday, 21st November 2018
21 November 2018

How to build Multiple Authentication in Laravel 5.7 using Middleware (VIDEO)

How to build Multiple Authentication in Laravel 5.7 using Middleware.
This is a fresh Laravel installation.

1. Open .env file and set database credentials in these files.

2. Open the migration of user in Database/migration/…user.php and update the following field for admin.

 

3. Run the following command to create tables in the database: “php artisan migrate”

4. Run “php artisan make:auth” for built-in authentication for registration and login.

5. Create a middleware by typing the following Laravel command: “php artisan make:middleware Admin”

6. Go to app/Http/ Middleware/Admin.php Update the code in this function with the following code:
public function handle($request, Closure $next)

 

7. Now, register this route in the app/Http/Kernel.php . Update the $routeMiddleware property with:

 

8. Open the routes/web.php file and enter the following code in it:
Route::get(‘admin/routes’, ‘HomeController@admin’)->middleware(‘admin’);

9. Open the app/http/controller/HomeController and update the following methods.

 

10. Open the resources/views/home.blade.php file and update the following code:

In this code, I used if(auth()->user()->isAdmin == 1) to check the user profile. If it is admin,
it will navigate to the admin area. Otherwise, it will redirect to users area.

11. Create a view called admin.blade.php in the root of the views folder, and add the following code to it:

 

 

12. For setting up admin auth, first register a user through Laravel register and then change the status of isadmin to ‘1’. Next , login to the application.

Close