How to make the multi-Authentication in laravel 5.2

laravel5.2 start to provides multi-Authentication.
And I try it.

By The way, multi-Authentication means to enable your application has two or more Authentication like below.

  • front screen – customer authentication
  • management screen – administrator authentication

Before you start this tutorial,
you need to complete to install laravel5.2 and setting for database, mail, etc.

You can see all file used in this tutorial at github.

■github( dog-ears/laravel-multi-auth )
https://github.com/dog-ears/laravel-multi-auth

Well, let’s do it.

(i) Set up bacic user Authentication

It’s very easy.
Run two command below.

php artisan make:auth
php artisan migrate

 

(ii) Create database for administrator

Duplicate migration file for users.
Named table “admins”

■/database/migrations/2016_05_20_000000_create_admins_table.php
https://github.com/dog-ears/laravel-multi-auth/commit/ca0c237d44ec8cd9facdd3e7fa57139a5aca28a6#diff-11

Basically change “users” to “admins”

php artisan migrate

※I didn’t duplicate password_resets table.

 

(iii) Modify config

■/config/auth.php
https://github.com/dog-ears/laravel-multi-auth/commit/ca0c237d44ec8cd9facdd3e7fa57139a5aca28a6#diff-9

    'guards' => [
(...)
        'admin' => [
            'driver' => 'session',
            'provider' => 'admins',
        ],
(...)
    'providers' => [
(...)
        'admins' => [
            'driver' => 'eloquent',
            'model' => App\Admin::class,
        ],
(...)
    'passwords' => [
(...)
        'admins' => [
            'provider' => 'admins',
            'email' => 'adminAuth.emails.password',
            'table' => 'password_resets',
            'expire' => 60,
        ],

Guards specify name for Authentication.
In this case we have two Authentication named “web” for user and “admin” for administrator.

Providers specify what model we use.

Passwords specify name of password reset setting.

Passwords has property below.

  • provider -> model to use
  • email -> decide email view file to someone forget password
  • table -> specify table for password reset
  • expire -> specify time (minutes) from recieving email to reset password.

 

(iv) Add model

Duplicate /app/User.php

■/app/Admin.php
https://github.com/dog-ears/laravel-multi-auth/commit/ca0c237d44ec8cd9facdd3e7fa57139a5aca28a6#diff-0

Basically Change “User” to “Admin”.

 

(v) Add controller

Duplicate /app/Http/Auth folder.
Rename it to /app/Http/AdminAuth.

■/app/Http/AdminAuth/AuthController.php
https://github.com/dog-ears/laravel-multi-auth/commit/ca0c237d44ec8cd9facdd3e7fa57139a5aca28a6#diff-1

protected $redirectTo = '/admin/home';

This specify redirect url after login and registration.

And add below few codes.

protected $guard = 'admin';

-> Specify what guard to use

protected $loginView = 'adminAuth.login';

-> Specify login view

protected $registerView = 'adminAuth.register';

-> Specify registration view

■/app/Http/AdminAuth/PasswordController.php
https://github.com/dog-ears/laravel-multi-auth/commit/ca0c237d44ec8cd9facdd3e7fa57139a5aca28a6#diff-2

$this->middleware('guest:admin');

Above code specify use “admin” guard with guest middleware.

By the way, guest middleware has function to redirect to login page if you are not login.

And Add below codes.

protected $redirectTo = '/admin/home';

-> Specify redirect url after password reset.

protected $linkRequestView = 'adminAuth.passwords.email';
protected $resetView = 'adminAuth.passwords.reset';

-> Specify view for password reset.

protected $guard = 'admin';

-> Specify what guard we use

protected $broker = 'admins';

-> specify password name that is listed in /config/auth.php

Duplicate /app/Http/HomeController.php.
And Rename it to /app/Http/AdminHomeController.php

■/app/Http/AdminHomeController.php
https://github.com/dog-ears/laravel-multi-auth/commit/ca0c237d44ec8cd9facdd3e7fa57139a5aca28a6#diff-3

$this->middleware('auth');
↓
$this->middleware('auth:admin');

In this controller, we specify admin guard with auth middleware.
By the way, auth middleware has function to redirect to appointed page  if you are login.

 

(vi) Modify middleware

■/app/Http/Middleware/Authenticate.php
https://github.com/dog-ears/laravel-multi-auth/commit/ca0c237d44ec8cd9facdd3e7fa57139a5aca28a6#diff-6

This middleware redirect to login screen if you are not login.

Then modify this middleware.
If guard is “admin”, redirect to admin/login.
And if guard is no “admin”, redirect to login.

return redirect()->guest('login');
↓
if( $guard === 'admin' ){
    return redirect()->guest('admin/login');
}else{
    return redirect()->guest('login');
}

■/app/Http/Middleware/RedirectIfAuthenticated.php
https://github.com/dog-ears/laravel-multi-auth/commit/ca0c237d44ec8cd9facdd3e7fa57139a5aca28a6#diff-7

This middleware redirect to appointed page if you are login.

return redirect('/');
↓
if( $guard === 'admin' ){
    return redirect('/admin/home');
}else{
    return redirect('/home');
}

 

(vii) Modify routes

Add below codes.

■/app/Http/routes.php
https://github.com/dog-ears/laravel-multi-auth/commit/ca0c237d44ec8cd9facdd3e7fa57139a5aca28a6#diff-8

Add code Created by “Route::auth();” code,
and modify some parts.

// Authentication Routes...
$this->get('admin/login', 'AdminAuth\AuthController@showLoginForm');
$this->post('admin/login', 'AdminAuth\AuthController@login');
$this->get('admin/logout', 'AdminAuth\AuthController@logout');

// Registration Routes...
$this->get('admin/register', 'AdminAuth\AuthController@showRegistrationForm');
$this->post('admin/register', 'AdminAuth\AuthController@register');

// Password Reset Routes...
$this->get('admin/password/reset/{token?}', 'AdminAuth\PasswordController@showResetForm');
$this->post('admin/password/email', 'AdminAuth\PasswordController@sendResetLinkEmail');
$this->post('admin/password/reset', 'AdminAuth\PasswordController@reset');

Route::get('admin/home', 'AdminHomeController@index');

 

(viii) Modify views

Duplicate /resources/views/auth.
Rename it to /resources/views/adminAuth.

I add “admin” to path and title.

Duplicate /resources/views/home.blade.php.
Rename it to /resources/views/admin/home.blade.php.

 

(ix) [appendix.1] Add links in navigation for check.

■/resources/views/layouts/app.blade.php
https://github.com/dog-ears/laravel-multi-auth/commit/ca0c237d44ec8cd9facdd3e7fa57139a5aca28a6#diff-24

You can display message depending on user state by using below codes.

@if (Auth::check())
[ Show this part for someone login as user ]
@endif
@if (Auth::guest())
[ Show this part for someone not login as user ]
@endif
Auth::guard('admin')->check()
[ Show this part for someone login as admin ]
@endif
Auth::guard('admin')->guest()
[ Show this part for someone not login as admin ]
@endif

 

(x) [appendix.2] Mail setting

cloud9 recommend mailtrap.io as test smtp server.

■mailtrap
https://mailtrap.io/

You can sign in with github account.

You can use mail to write username and password in .env.

After setting mailtrap, you can confirm email on mailtrap screen without send message to recieved address.

That’s all.
Thank you for your reading.

10 thoughts on “How to make the multi-Authentication in laravel 5.2”

  1. Their is a Mistake in section 3 :
    in guards array, provider should be admin not use

    ‘provider’ => ‘users’,

    Should be
    ‘provider’ => ‘admins’,

  2. hi need help here, is there a way to create 2 step login? By default, the login for laravel system is: User->Login Page->Dashboard What i want to achieve here is user -> company login section -> user login -> dashboard

Leave a Reply

Your email address will not be published. Required fields are marked *