How to use authentication for multiple tables in L

2019-01-09 04:58发布

Sometimes, we'd like to separate users and admins in different 2 tables.
I think it is a good practice.

So I was looking if that is possible in Laravel 5.

After a quick search, I found many people having the same question as mine.
I found some answers, but I don't think any of them is good enough.

Therefore, I spent some time digging into the source code and finally find a way to achieve this.

标签: php laravel-5
3条回答
Luminary・发光体
2楼-- · 2019-01-09 05:26

I have created a laravel package where you can handle multiple authentication.

Step 1 : Composer require

Firstly, composer require the multiauth package

composer require sarav/laravel-multiauth dev-master

Step 2 : Replacing default auth service provider

Replace

Illuminate\Auth\AuthServiceProvider::class

with

Sarav\Multiauth\MultiauthServiceProvider

in your config/app.php file

Step 3 : Modify auth.php

Modify your config/auth.php file to something like this

'multi' => [
    'user' => [
        'driver' => 'eloquent',
        'model'  => App\User::class,
        'table'  => 'users'
    ],
'admin' => [
    'driver' => 'eloquent',
    'model'  => App\Admin::class,
    'table'  => 'admins'
   ]
],

Thats it! Now you can try multiple authentication by passing the user as first parameter. For example

\Auth::loginUsingId("user", 1); // Login user with id 1

\Auth::loginUsingId("admin", 1); // Login admin with id 1

// Attempts to login user with email id johndoe@gmail.com 
\Auth::attempt("user", ['email' => 'johndoe@gmail.com', 'password' => 'password']);

// Attempts to login admin with email id johndoe@gmail.com
\Auth::attempt("admin", ['email' => 'johndoe@gmail.com', 'password' => 'password']); 

For more detailed documentation

http://sarav.co/blog/multiple-authentication-in-laravel/

http://sarav.co/blog/multiple-authentication-in-laravel-continued/

查看更多
贪生不怕死
3楼-- · 2019-01-09 05:27

there are good packages for handling multi auth. check these links:
sboo multiauth package

mohamednagy/multiauth

查看更多
Melony?
4楼-- · 2019-01-09 05:41

Before reading the following, you are supposed to have basic knowledge on ServiceProvider, Facade and IoC in Laravel 5. Here we go.

According to the doc of Laravel, you could find the Facade 'Auth' is refering to the Illuminate\Auth\AuthManager, which has a magic __call(). You could see the major function is not in AuthManager, but in Illuminate\Auth\Guard

Guard has a Provider. This provider has a $model property, according to which the EloquentUserProvider would create this model by "new $model". These are all we need to know. Here goes the code.

1.We need to create a AdminAuthServiceProvider.

public function register(){
    Auth::extend('adminEloquent', function($app){
        // you can use Config::get() to retrieve the model class name from config file
        $myProvider = new EloquentUserProvider($app['hash'], '\App\AdminModel') 
        return new Guard($myProvider, $app['session.store']);
    })
    $app->singleton('auth.driver_admin', function($app){
        return Auth::driver('adminEloquent');
    });
}

2.Facade:

class AdminAuth extends Facade {
        protected static function getFacadeAccessor() { return 'auth.driver_admin'; }
    }

3. add the alias to Kernel:

'aliases' => [
    //has to be beneath the 'Auth' alias
    'AdminAuth' => '\App\Facades\AdminAuth'
]

Hope this could be helpful.

查看更多
登录 后发表回答