Laravel 5.3使用多重身份验证(例如admin, customer)


Laravel 5.3 using multiple authentication (e.g admin, customer)

我读了很多关于Laravel中多认证的线程,我看到的大多数配置都有些复杂,我也看到过一个多认证包,但它不支持Laravel Socialite。
我知道这个问题被问了很多次,但如果有人能给出更好的答案。我将不胜感激!

我尝试过的事情

  1. 熟悉Laravel make:auth

  2. 我也熟悉Laravel社交网站Facebook, Twitter, Google plus。

试试吧。但是你仍然需要一些关于Laravel新多租户的基本知识。

  1. 在config/auth.php中添加guards数组:

    'customer' => [
        'driver' => 'session',
        'provider' => 'customers',
    ],
    
  2. 然后在同一个文件中添加这个到providers数组:

        'customers' => [
            'driver' => 'eloquent',
            'model' => App'Customer::class,
        ],
    
  3. customers DB table创建迁移(您可以使用Laravel的开箱即用迁移用户表)

  4. 下一个是Eloquent模型App'Customer,包括:

    use App'Scopes'AuthorizedScope;
    use Illuminate'Foundation'Auth'User as Authenticatable;
    

这些应该可以让你在应用程序中使用Laravel的Auth facade和这些最常用的方法:

    Auth::guard('customer')->attempt()
    Auth::guard('customer')->check()
    Auth::guard('customer')->logout()
    Auth::guard('customer')->user()

或者像这样使用认证中间件:

    Route::get('customer/dashboard', function () {
        // Only authenticated users may enter...
    })->middleware('auth:customer');

https://laravel.com/docs/5.3/authentication用户身份验证