Contact Us

Developer’s comprehensive guide to Laravel MultiAuth

For the development of dynamic and higher level website Laravel is the best platform.

To manage the content and other stuffs of the site we always need an admin panel through which we can handle the content of the site.
To implement a separate admin panel in any laravel project we need to add multiple authentication functionality in site. Let us proceed to that.

Laravel 5.2 comes with new artisan command which will generate route, controller and views for users table.
Open terminal, go to your project directory and fire below command :

After you fire this command your controller will have following files :

app/Http/Controllers/Auth/AuthController
app/Http/Controllers/Auth/PasswordController

Now create new table called admin (For simplicity you can create same migration for admin as users table)

Now create new directory called AdminAuth in app/Http/Controllers and copy above files to AdminAuth directory, the directory structure will be like:

app/Http/Controllers/AdminAuth/AuthController
app/Http/Controllers/AdminAuth/PasswordController

Now Copy files from resources/views/auth directory to resources/views/admin/auth directory and update the action path of the forms accordingly.

Now let’s modify config/auth.php,
replace below code in auth.php

Now let’s add new rules for admin in Routes.php

Now go to AdminAuth\AuthController.php and override two methods and variables below:

In above code you can see that we have overridden three protected variables which will change the actual behaviour of the laravel authentication library.

$redirectTo will be used to redirect the user once s/he successfully logged in to the admin panel.

$redirectAfterLogout will define where to redirect user after logout from admin panel.

$guard this is the crucial part of the Auth, here we are defining the guard for the panel as we have added new guard in Auth.php.

showLoginForm()  will override the actual behaviour of auth and will show the admin login form.

showRegistrationForm() will show the admin registration form. In my case, this is not needed as I have removed the registration functionality for admin.

Now create new middleware called RedirectIfNotAdmin using below command

Now add below code in the middleware

Now Register middleware in kernel.php

Now we will use this middleware in our AdminController.php

That’s it!! this is all we needed to make it working.

Please Note:
We can access authenticated user details directly using Auth::user() but if we have multiple authentication then we have to use :

Auth::guard(‘guard_name’)->user();

For logout
Auth::guard(‘guard_name’)->user->logout();

And to get details of authenticated user in json format :
Auth::guard(‘guard_name’)->user();

For Password Reset

In App/Http/Controllers/AdminAuth/PasswordController.php  file, add two protected variables

protected $guard = ‘admin’; //For guard

protected $broker = ‘admins’; //For letting laravel know which config you’re going to use for resetting password

Now add below mentioned three public methods

That’s all with the Laravel MultiAuth functionality.

Chirag Malaviya

Chirag Malaviya | Web Developer

Chirag is a PHP Web Developer at Yudiz Solutions Pvt. Ltd., who has the knowledge of Codeigniter and laravel frameworks, Node.js and socket programming. He is always eager to learn latest technology and find the tricky ways to make the things easy. He likes to face the technological challenges. He likes to get in touch with people with whom he can do something innovative.

Leave a Reply

Top
Yudiz Solutions Pvt. Ltd.
Career
Are you looking for a company where you are appreciated for your work?
Get a Free Quote
Tell Us Your Project Requirements

Career
Are you looking for a company where you are appreciated for your work?
Get a Free Quote
Tell Us Your Project Requirements