如何在 laravel 护照中实施多重身份验证
how to implement multiauth in laravel passport
我有两个用户 admin/user 我想为 api 验证这两个用户,
它适用于一个用户,但不适用于管理员
看看我试过什么
在管理控制器中
public function login(Request $request){
// $res=;
// dd ($res);
if(Auth::guard('admin')->attempt(['email' => $request->email, 'password' => $request->password]))
{
// if successful, then redirect to their intended location
$user = auth()->guard('admin')->user();
$success['token'] = $user->createToken('admin')->accessToken;
return response()->json(['success' => $success], $this->successStatus);
}
else{
return response()->json(['error'=>'Unauthorised','email'=> $request->email,'password'=> $request->password], 401);
}
}
并在 api.php
Route::prefix('admin')->group(function () {
Route::post('login', 'API\Admin\AdminController@login')->name('admin.login');
Route::post('register', 'API\Admin\AdminController@register')->name('admin.register');
Route::group(['middleware' => 'auth:admin-api'], function(){
Route::post('get-details', 'API\Admin\AdminController@getDetails');
});
});
当我尝试为管理员调用尝试函数时,出现错误
BadMethodCallException
Method Illuminate\Auth\RequestGuard::attempt does not exist.
能否就如何在 laravel/passport
中进行多重身份验证分享您的想法?
您可以在 laravel 护照中实施多重身份验证,您必须按照以下步骤操作
1) 在我创建的情况下创建 4 个守卫
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'api' => [
'driver' => 'passport',
'provider' => 'users',
],
'admin-api' => [
'driver' => 'passport',
'provider' => 'admin',
],
'admin' => [
'driver' => 'session',
'provider' => 'admin',
],
],
然后在控制器中记录该类型的用户时,确保您使用的是 sessio driver 守卫,例如
usercontroller.php
public function login(){
if(Auth::attempt(['email' => request('email'), 'password' => request('password')])){
$user = Auth::user();
$success['token'] = $user->createToken('MyApp')->accessToken;
return response()->json(['success' => $success], $this->successStatus);
}
else{
return response()->json(['error'=>'Unauthorised'], 401);
}
}
在admincontroller.php
public function login(){
if(Auth::guard('admin')->attempt(['email' => request('email'), 'password' => request('password')])){
$user = Auth::guard('admin')->user();
$success['token'] = $user->createToken('admin')->accessToken;
return response()->json(['success' => $success], $this->successStatus);
}
else{
return response()->json(['error'=>'Unauthorised'], 401);
}
}
并且在检查令牌时使用护照 driver 守卫 like
public function getDetails()
{
$user = Auth::guard('admin-api')->user();
return response()->json(['success' => $user], $this->successStatus);
}
在中间件中
Route::group(['middleware' => 'auth:api'], function(){
Route::post('admin/get-details', 'API\Admin\AdminController@getDetails');
});
我设法通过中间件做到了,希望这会对你有所帮助 -
我有两个用户 admin/user 我想为 api 验证这两个用户, 它适用于一个用户,但不适用于管理员
看看我试过什么 在管理控制器中
public function login(Request $request){
// $res=;
// dd ($res);
if(Auth::guard('admin')->attempt(['email' => $request->email, 'password' => $request->password]))
{
// if successful, then redirect to their intended location
$user = auth()->guard('admin')->user();
$success['token'] = $user->createToken('admin')->accessToken;
return response()->json(['success' => $success], $this->successStatus);
}
else{
return response()->json(['error'=>'Unauthorised','email'=> $request->email,'password'=> $request->password], 401);
}
}
并在 api.php
Route::prefix('admin')->group(function () {
Route::post('login', 'API\Admin\AdminController@login')->name('admin.login');
Route::post('register', 'API\Admin\AdminController@register')->name('admin.register');
Route::group(['middleware' => 'auth:admin-api'], function(){
Route::post('get-details', 'API\Admin\AdminController@getDetails');
});
});
当我尝试为管理员调用尝试函数时,出现错误
BadMethodCallException Method Illuminate\Auth\RequestGuard::attempt does not exist.
能否就如何在 laravel/passport
中进行多重身份验证分享您的想法?您可以在 laravel 护照中实施多重身份验证,您必须按照以下步骤操作
1) 在我创建的情况下创建 4 个守卫
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'api' => [
'driver' => 'passport',
'provider' => 'users',
],
'admin-api' => [
'driver' => 'passport',
'provider' => 'admin',
],
'admin' => [
'driver' => 'session',
'provider' => 'admin',
],
],
然后在控制器中记录该类型的用户时,确保您使用的是 sessio driver 守卫,例如
usercontroller.php
public function login(){
if(Auth::attempt(['email' => request('email'), 'password' => request('password')])){
$user = Auth::user();
$success['token'] = $user->createToken('MyApp')->accessToken;
return response()->json(['success' => $success], $this->successStatus);
}
else{
return response()->json(['error'=>'Unauthorised'], 401);
}
}
在admincontroller.php
public function login(){
if(Auth::guard('admin')->attempt(['email' => request('email'), 'password' => request('password')])){
$user = Auth::guard('admin')->user();
$success['token'] = $user->createToken('admin')->accessToken;
return response()->json(['success' => $success], $this->successStatus);
}
else{
return response()->json(['error'=>'Unauthorised'], 401);
}
}
并且在检查令牌时使用护照 driver 守卫 like
public function getDetails()
{
$user = Auth::guard('admin-api')->user();
return response()->json(['success' => $user], $this->successStatus);
}
在中间件中
Route::group(['middleware' => 'auth:api'], function(){
Route::post('admin/get-details', 'API\Admin\AdminController@getDetails');
});
我设法通过中间件做到了,希望这会对你有所帮助 -