我想使用 Passport 在 Laravel 中创建一个多身份验证 Web 应用程序。
我能够完成两个不同警卫api和admin-api的登录过程。重要的部分是两个警卫都使用数据库中的oauth_access_tokens表,该表是由护照创建的,该表有一个名为user_id的字段。
这意味着每当我们为任何一个用户调用logout方法时,如果另一个用户恰好具有相同的id,因为 admins 表与 users 表不同,则两者的登录令牌都将被销毁。因为在每个 Admin 和 User 的模型中,我们指的是一个函数AauthAcessToken
:
public function AauthAcessToken()
{
return $this->hasMany('\App\OauthAccessToken');
}
并且 OauthAccessToken
是表的型号oauth_access_tokens
。
所以我手动创建了一个新表,它是 Admin model functionadmin_oauth_access_tokens
的副本并重新分配给它。但这并没有解决问题,所以我删除了.oauth_access_tokens
AauthAcessToken
admin_oauth_access_tokens
我该如何解决?
这是一些代码:
授权文件
<?php
return [
/*
|--------------------------------------------------------------------------
| Authentication Defaults
|--------------------------------------------------------------------------
|
| This option controls the default authentication "guard" and password
| reset options for your application. You may change these defaults
| as required, but they're a perfect start for most applications.
|
*/
'defaults' => [
'guard' => 'web',
'passwords' => 'users',
],
/*
|--------------------------------------------------------------------------
| Authentication Guards
|--------------------------------------------------------------------------
|
| Next, you may define every authentication guard for your application.
| Of course, a great default configuration has been defined for you
| here which uses session storage and the Eloquent user provider.
|
| All authentication drivers have a user provider. This defines how the
| users are actually retrieved out of your database or other storage
| mechanisms used by this application to persist your user's data.
|
| Supported: "session", "token"
|
*/
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'api' => [
'driver' => 'passport',
'provider' => 'users',
'hash' => false,
],
'admin' => [
'driver' => 'session',
'provider' => 'admins',
],
'admin-api' => [
'driver' => 'passport',
'provider' => 'admins',
'hash' => false,
],
],
/*
|--------------------------------------------------------------------------
| User Providers
|--------------------------------------------------------------------------
|
| All authentication drivers have a user provider. This defines how the
| users are actually retrieved out of your database or other storage
| mechanisms used by this application to persist your user's data.
|
| If you have multiple user tables or models you may configure multiple
| sources which represent each model / table. These sources may then
| be assigned to any extra authentication guards you have defined.
|
| Supported: "database", "eloquent"
|
*/
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\User::class,
],
'admins' => [
'driver' => 'eloquent',
'model' => App\Admin::class,
],
// 'users' => [
// 'driver' => 'database',
// 'table' => 'users',
// ],
],
/*
|--------------------------------------------------------------------------
| Resetting Passwords
|--------------------------------------------------------------------------
|
| You may specify multiple password reset configurations if you have more
| than one user table or model in the application and you want to have
| separate password reset settings based on the specific user types.
|
| The expire time is the number of minutes that the reset token should be
| considered valid. This security feature keeps tokens short-lived so
| they have less time to be guessed. You may change this as needed.
|
*/
'passwords' => [
'users' => [
'provider' => 'users',
'table' => 'password_resets',
'expire' => 60,
'throttle' => 60,
],
'admins' => [
'provider' => 'admins',
'table' => 'password_resets',
'expire' => 15,
'throttle' => 60,
],
],
/*
|--------------------------------------------------------------------------
| Password Confirmation Timeout
|--------------------------------------------------------------------------
|
| Here you may define the amount of seconds before a password confirmation
| times out and the user is prompted to re-enter their password via the
| confirmation screen. By default, the timeout lasts for three hours.
|
*/
'password_timeout' => 10800,
];
身份验证\PassportAuthController.php:
<?php
namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Auth;
class PassportAuthController extends Controller
{
public function __construct()
{
$this->middleware('auth:api', ['except' => ['generateAccessToken', 'login']]);
}
protected function generateAccessToken($user)
{
$token = $user->createToken($user->email.'-'.now());
return $token->accessToken;
}
public function login(Request $request)
{
$request->validate([
'email' => 'required|email|exists:users,email',
'password' => 'required'
]);
$credentials = [
'email' => $request->email,
'password' => $request->password
];
if( Auth::attempt($credentials) ) {
$user = Auth::user();
$token = $user->createToken($user->email.'-'.now());
return response()->json([
'token' => $token->accessToken
]);
}
}
public function logout(Request $request)
{
if (Auth::check()) {
Auth::user()->AauthAcessToken()->delete();
}
return response()->json([
'msg' => 'Logged out complete'
]);
}
}
Auth\Admin\PassportAuthController.php
<?php
namespace App\Http\Controllers\Auth\Admin;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Auth;
class PassportAuthController extends Controller
{
public function __construct()
{
$this->middleware('auth:admin-api', ['except' => ['generateAccessToken', 'login']]);
}
protected function generateAccessToken($user)
{
$token = $user->createToken($user->email.'-'.now());
return $token->accessToken;
}
public function login(Request $request)
{
$request->validate([
'email' => 'required|email|exists:admins,email',
'password' => 'required'
]);
$credentials = [
'email' => $request->email,
'password' => $request->password
];
if(Auth::guard('admin')->attempt($credentials)) {
$user = $this->guard()->user();
$token = $user->createToken($user->email.'-'.now());
return response()->json([
'token' => $token->accessToken
]);
}
}
public function logout(Request $request)
{
if (Auth::check()) {
$this->guard()->user()->AauthAcessToken()->delete();
}
return response()->json([
'msg' => 'Logged out complete'
]);
}
/**
* Get the guard to be used during authentication.
*
* @return \Illuminate\Contracts\Auth\StatefulGuard
*/
protected function guard()
{
return Auth::guard('admin');
}
}
谢谢