3

Authentication Drivers / "Multi-Auth"

as prior to release of laravel 5.2 it is stated that multi auth suppots out of the box. but there is no any example codes showing how to authenticate using different drivers with routes. So I need help setting up the multi-auth using default laravel 5.2

msonowal
  • 1,346
  • 2
  • 14
  • 31
  • Laravel 5.2 Multi Auth solution is here. Please check this link: http://stackoverflow.com/questions/34490600/how-to-use-multi-auth-in-laravel-5-2/34531445#34531445 – Hasmukh Tank Dec 31 '15 at 10:44
  • Here's the solution http://stackoverflow.com/questions/34614753/can-anyone-explain-laravel-5-2-multi-auth-with-example/34783443#34783443 – imrealashu Jan 14 '16 at 08:04
  • http://stackoverflow.com/questions/34614753/can-anyone-explain-laravel-5-2-multi-auth-with-example – msonowal Mar 13 '16 at 00:22
  • another example http://stackoverflow.com/questions/34490600/how-to-use-multi-auth-in-laravel-5-2 – msonowal Mar 13 '16 at 00:23

3 Answers3

6

Create two new models: App\Admin and App\User. Update config/auth.php:

return [
    'defaults' => [
        'guard' => 'user',
        'passwords' => 'user',
    ],
    'guards' => [
        'user' => [
            'driver' => 'session',
            'provider' => 'user',
        ],
        'admin' => [
            'driver' => 'session',
            'provider' => 'admin',
        ],
    ],
    'providers' => [
        'user' => [
            'driver' => 'eloquent',
            'model' => 'App\User',
        ],
        'admin' => [
            'driver' => 'eloquent',
            'model' => 'App\Admin',
        ],
    ],
    'passwords' => [
        'user' => [
            'provider' => 'user',
            'email' => 'auth.emails.password',
            'table' => 'password_resets',
            'expire' => 60,
        ],
        'admin' => [
            'provider' => 'admin',
            'email' => 'auth.emails.password',
            'table' => 'password_resets',
            'expire' => 60,
        ]
    ]
];

In kernel.php

    protected $middleware = [
        \Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,
        \Illuminate\Session\Middleware\StartSession::class,
        \Illuminate\View\Middleware\ShareErrorsFromSession::class
    ];

    /**
     * The application's route middleware groups.
     *
     * @var array
     */
    protected $middlewareGroups = [
        'web' => [
            \App\Http\Middleware\EncryptCookies::class,
            \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,

            //\App\Http\Middleware\VerifyCsrfToken::class,
        ],

        'api' => [
            'throttle:60,1',
        ],
    ];

and in Route.php set below code and test

    Route::get('/login', function() {
        $auth = auth()->guard('admin');

        $credentials = [
            'email' =>  'admin@gmail.com',
            'password' =>  'password',
        ];

        if ($auth->attempt($credentials)) {
            return redirect('/profile');
        } 
    });



    Route::get('/profile', function() {
            if(auth()->guard('admin')->check()){
                 print_r(auth()->guard('admin')->user()->toArray());
            } 

            if(auth()->guard('user')->check()){
                print_r(auth()->guard('user')->user()->toArray());
            }
        });
miken32
  • 35,483
  • 13
  • 81
  • 108
DsRaj
  • 2,122
  • 1
  • 13
  • 24
1

Using the rajpurohit-dinesh example, we just need to finnish first step:

1: Create an App\Admin model (on our app folder). Here is how should be your Authenticatable class.

<?php

namespace App;

use Illuminate\Foundation\Auth\User as Authenticatable;

Class Admin extends Authenticatable
{
//
}

2: Update config/auth.php.

return [
    // This is the default guard used, not need to declare
    // another guard here
    'defaults' => [
        'guard' => 'user',
            'passwords' => 'user',
    ],

    // Here we must to declare the guards, if we created the App\Admin
    // class as first step, we don't need to create a custom guard
    'guards' => [
        'user' => [
            'driver' => 'session',
            'provider' => 'user',
        ],
        'admin' => [
            'driver' => 'session',
            'provider' => 'admin',
        ],
    ],
    // In this example we are using only 'eloquent' driver
    'providers' => [
        'user' => [
            'driver' => 'eloquent',
            'model' => 'App\User',
        ],
        'admin' => [
            'driver' => 'eloquent',
            'model' => 'App\Admin',
        ],
    ],
    'passwords' => [
        'user' => [
            'provider' => 'user',
            'email' => 'auth.emails.password',
            'table' => 'password_resets',
            'expire' => 60,
        ],
        'admin' => [
            'provider' => 'admin',
            'email' => 'auth.emails.password',
            'table' => 'password_resets',
            'expire' => 60,
        ]
    ]
];

3: To test it, we can use our app\Http\Route.php file:

Route::get('/login', function() {
    $auth = auth()->guard('admin');

    $credentials = [
        'email' =>  'admin@gmail.com',
        'password' =>  'password',
    ];

    if ($auth->attempt($credentials)) {
        return 'Success';
    } else {
        return 'Not Success';    
});
HoLiC
  • 376
  • 2
  • 7
0

Thanks for answer HoLiC, it's working right now but can you try to implement it with classes what Laravel's bring on start? You just have to add routes:

Route::controller('/auth', 'Auth\AuthController');
Route::controller('/password', 'Auth\PasswordController');

and create form in resources/views/auth/login.blade.php from my post above. After this you can use routes laravel.dev/auth/login and laravel.dev/auth/logout

Starter auth mechanism doesn't working good and its not compatibile with multi auth. If you check you can login via laravel.dev/auth/login but only user(theres no any way to setup in AuthController or anyplace to use admins) and logout action not working. If you check this trait Illuminate\Foundation\Auth\AuthenticatesUsers you will see that this mechanims is unuseful right now for example logout method not defines anywhere provider admin:

Auth::logout(); // not working logout should be smth like Auth::guard($provider)->logout();
bartw2k9
  • 19
  • 1