jwt 多使用者認證,一次簡單處理。

全場我最姜姜姜丶發表於2019-03-22

https://www.jianshu.com/p/9e95a5f8ac4a 前面教程部分,不多解釋

'defaults' => [
  'guard' => 'api',
],
'guards' => [
  'api' => [
  'driver' => 'jwt',
  'provider' => 'admins',
      ],
'memberapi' => [
  'driver' => 'jwt',
  'provider' => 'members',
    ],
],

'providers' => [
  'admins' => [
  'driver' => 'eloquent',
  'model' => \App\Models\AdminUser::class,
  ],
'members' => [
  'driver' => 'eloquent',
  'model' => \App\Models\Member::class,
],
RefreshToken.php和 MemberRefreshToken.php
第一個路由
Route::group([
  'middleware' => ['refresh.token']
], function ($router) {
});
第二個路由
Route::group([
  'middleware' => ['member.refresh.token']
], function ($router) {
});

然後你會發現第二個路由驗證token始終過不了,走的是adminusers表。

我再寫了一箇中介軟體

namespace App\Http\Middleware;
use Closure;
class ChangAuth
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        \Config::set( 'auth.defaults.guard','memberapi');
        return $next($request);
    }
}

然後把第二個路由

Route::group([
  'middleware' => ['chang.auth','member.refresh.token']
], function ($router) {

});

然後你就會發現成功了。他驗證了不同的使用者表。
哈哈,經過大佬提醒,還有個串號的bug在裡面,解決方法出門左拐,大佬部落格。

本作品採用《CC 協議》,轉載必須註明作者和本文連結

相關文章