0

In Laravel 5.2 I made multi auth with post:

can-anyone-explain-laravel-5-2-multi-auth-with-example

But in Laravel 5.3 is deterrent.

I need create multi auth in Laravel 5.3 without Hesto package.

Community
  • 1
  • 1

3 Answers3

1

For using Multi-auth in laravel 5 you will have to configure guards in the auth config file.

For example if you want to have a users table and a admins table, you will have to change the following lines in auth.php file in the config folder.

'guards' => [
    'admins' => [
        'driver' => 'session',
        'provider' => 'admins',
    ],

    'users' => [
        'driver' => 'session',
        'provider' => 'users',
    ],
],

And then

'providers' => [
    'users' => [
        'driver' => 'eloquent',
        'model' => App\User::class,
    ],
    'admins' => [
        'driver' => 'eloquent',
        'model' => App\Admin::class,
    ],
],

Also you will have to add a config for forgotten passwords table:

'passwords' => [
    'users' => [
        'provider' => 'users',
        'table' => 'password_resets',
        'expire' => 60,
    ],
    'admins' => [
        'provider' => 'admins',
        'table' => 'password_resets',
        'expire' => 60,
    ],
],

Then in your auth controllers you will have to add the following property:

protected $guard = 'admins'

in the admins auth controllers and

protected $guard = 'users'

in the users auth controllers.

Then it should work, from now every call to the auth facade will have to include the guard method, for example for getting the current user you used to do:

Auth::user();

now it will look like:

Auth::guard('users')->user();

And the same for admins

Auth::guard('admins')->user();
  • This solution is not warking for laravel-5.3. Don't copy the others answer. See the url:http://stackoverflow.com/questions/34614753/can-anyone-explain-laravel-5-2-multi-auth-with-example – Priyabrata Atha Jan 23 '17 at 14:00
  • We use Laravel 5.3 and this works fine to me, have you configured it right? I can help you out if you need, and I didn't copied any answer. –  Jan 23 '17 at 15:20
  • Thanks and No, I found the solution, you can not use guard like this way it can work only laravel 5.2 but in laravel 5.3 you should use in controller -- protected function guard() { return Auth::guard('admins'); } – Priyabrata Atha Jan 24 '17 at 06:11
  • That's pretty weird, but ok whatever.. If it works :))) –  Jan 24 '17 at 06:14
1

For multi auth system in laravel5.3 you need to manage all the things manually like Guard, Routes for every login.

Just check below link it will be useful for multi auth. Hope it helps you.

https://github.com/jayminpanchal/laravel-multiauth-demo

Jaymin Panchal
  • 2,664
  • 2
  • 24
  • 28
1

The complete working example in laravel 5.3:

Laravel 5.3 Multi auth simple configuration with forntend and backend/admin

If you want to have a users table and a admin table, you will have to change the following lines in auth.php file in the config folder.

 'guards' => [
            'web' => [
                'driver' => 'session',
                'provider' => 'users',
            ],
            'admin' => [
                'driver' => 'session',
                'provider' => 'admin',
            ],  
            'api' => [
                'driver' => 'token',
                'provider' => 'users',
            ],
        ],

And then

     'providers' => [
                'users' =>[
                    'driver'=>'eloquent',
                    'model'=>App\User::class,
                ],
                'admin'=>[
                    'driver'=>'eloquent',
                    'model'=>App\Admin::class,
                ],
                // 'users' => [
                //     'driver' => 'database',
                //     'table' => 'users',
                // ],
        ],

Also you will have to add a config for forgot password table:

'passwords' => [
        'users' => [
            'provider' => 'users',
            'email' => 'auth.emails.password',
            'table' => 'password_resets',
            'expire' => 60,
        ],
        'admin' => [
            'provider' => 'admin',
            'email' => 'admin.auth.emails.password',
            'table' => 'admin_password_resets',
            'expire' => 60,
        ],
    ],

routes/web.php

//Routes for admin...
Route::group(array('prefix' =>'admin','middleware'=>array('web')), function () {

    Route::get('/admin/login','AdminAuth\AuthController@showLoginForm');
    Route::post('/admin/login','AdminAuth\AuthController@login');
    Route::get('/admin/logout','AdminAuth\AuthController@logout');


    Route::get('admin/register', 'AdminAuth\AuthController@showRegistrationForm');
    Route::post('admin/register', 'AdminAuth\AuthController@register');
    Route::get('/admin', 'AdminController@index');
});  

AdminAuth\AuthController.php Add two methods and specify redirectTo and guard

protected $redirectTo = '/admin';
public function showLoginForm()
{
    if (view()->exists('auth.authenticate')) {
        return view('auth.authenticate');
    }

    return view('admin.auth.login');
}
public function showRegistrationForm()
{
    return view('admin.auth.register');
} 

protected function guard()
{
    return Auth::guard('admin');
}

Create a middleware for admin

class RedirectIfNotAdmin
{
/**
 * Handle an incoming request.
 *
 * @param  \Illuminate\Http\Request  $request
 * @param  \Closure  $next
 * @param  string|null  $guard
 * @return mixed
 */
public function handle($request, Closure $next, $guard = 'admin')
{
    if (!Auth::guard($guard)->check()) {
        return redirect('/');
    }

    return $next($request);
    }
}  

Now register the middleware in kernel.php

 protected $routeMiddleware = [
    'admin' => \App\Http\Middleware\RedirectIfNotAdmin::class,
];  

Use this middleware in AdminController.php

namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;
class AdminController extends Controller
{
    public function __construct(){
        $this->middleware('admin');
   }
public function index(){
        return view('admin.dashboard');
    }
}  

We can access authenticated user directly using Auth::user() but if you have two authentication table then you have to use

Auth::guard('guard_name')->user()

For Authenticated admin use

Auth::guard('admin')->user()

In file App/Http/Controllers/AdminAuth/PasswordController.php add two functions

 //For guard
    protected function guard()
    {
        return Auth::guard('admin');
    }

    //For letting laravel know which config you're going to use for resetting password
    public function broker()
    {
        return Password::broker('admin');
    }

And add three public method showResetForm, getEmail(), showLinkRequestForm

public function getEmail()
{
    return $this->showLinkRequestForm();
}

public function showLinkRequestForm()
{
    if (property_exists($this, 'linkRequestView')) {
        return view($this->linkRequestView);
    }

    if (view()->exists('admin.auth.passwords.email')) {
        return view('admin.auth.passwords.email');
    }

    return view('admin.auth.password');
}

public function showResetForm(Request $request, $token = null)
{

    if (is_null($token)) {
        return $this->getEmail();
    }
    $email = $request->input('email');

    if (property_exists($this, 'resetView')) {
        return view($this->resetView)->with(compact('token', 'email'));
    }

    if (view()->exists('admin.auth.passwords.reset')) {
        return view('admin.auth.passwords.reset')->with(compact('token', 'email'));
    }

    return view('admin.passwords.auth.reset')->with(compact('token', 'email'));
}

Now Update add three routes in your routes/web.php

Route::post('admin/password/email','AdminAuth\PasswordController@sendResetLinkEmail');
Route::post('admin/password/reset','AdminAuth\PasswordController@reset');
Route::get('admin/password/reset/{token?}','AdminAuth\PasswordController@showResetForm');
Priyabrata Atha
  • 416
  • 4
  • 12