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 )

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"


Basically change “users" to “admins"

php artisan migrate

※I didn’t duplicate password_resets table.


(iii) Modify config


    '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


Basically Change “User" to “Admin".


(v) Add controller

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


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



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 = '';
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



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


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');
    return redirect()->guest('login');


This middleware redirect to appointed page if you are login.

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


(vii) Modify routes

Add below codes.


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.


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

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


(x) [appendix.2] Mail setting

cloud9 recommend as test smtp server.


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.


Posted by dog-ears