how to implement multiauth in laravel passport(如何在 laravel 护照中实现多身份验证)
问题描述
我有两个用户 admin/user 我想验证这两个用户的 api,它适用于一个用户,但不适用于管理员
I have two users admin/user i want to authenticate this two users for api, it is working for one user but its not working for admin
看看我尝试了什么在管理控制器中
see what i have tried in admin controller
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 中
and in api.php
Route::prefix('admin')->group(function () {
Route::post('login', 'APIAdminAdminController@login')->name('admin.login');
Route::post('register', 'APIAdminAdminController@register')->name('admin.register');
Route::group(['middleware' => 'auth:admin-api'], function(){
Route::post('get-details', 'APIAdminAdminController@getDetails');
});
});
当我尝试为管理员调用尝试函数时,它给了我错误
When i try to call attempt function for admin its giving me error
BadMethodCallException方法 IlluminateAuthRequestGuard::attempt 不存在.
BadMethodCallException Method IlluminateAuthRequestGuard::attempt does not exist.
您能否分享您对如何在 laravel/passport 中进行多身份验证的想法
can you please share your ideas over how to do multiauth in laravel/passport
推荐答案
您可以在 laravel 护照中实现多身份验证,您必须按照以下步骤操作
you can implement multiauth in laravel passport you have to follow below steps
1) 在我创建的情况下创建 4 个警卫
1) create 4 guards in my situation i created like
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'api' => [
'driver' => 'passport',
'provider' => 'users',
],
'admin-api' => [
'driver' => 'passport',
'provider' => 'admin',
],
'admin' => [
'driver' => 'session',
'provider' => 'admin',
],
],
然后在控制器中记录该类型的用户时,请确保您使用的是 sessio 驱动程序保护,例如
then when logging that type of user in controller make sure you are using sessio driver guards like
用户控制器.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 中
in 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);
}
}
并且在检查令牌时使用护照司机警卫,例如
and when checking the token use the passport driver guard 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', 'APIAdminAdminController@getDetails');
});
这篇关于如何在 laravel 护照中实现多身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在 laravel 护照中实现多身份验证
基础教程推荐
- 在多维数组中查找最大值 2021-01-01
- 超薄框架REST服务两次获得输出 2022-01-01
- 在 Woocommerce 中根据运输方式和付款方式添加费用 2021-01-01
- WooCommerce 中选定产品类别的自定义产品价格后缀 2021-01-01
- Libpuzzle 索引数百万张图片? 2022-01-01
- mysqli_insert_id 是否有可能在高流量应用程序中返回 2021-01-01
- XAMPP 服务器不加载 CSS 文件 2022-01-01
- 在 PHP 中强制下载文件 - 在 Joomla 框架内 2022-01-01
- 通过 PHP SoapClient 请求发送原始 XML 2021-01-01
- 如何在 PHP 中的请求之间持久化对象 2022-01-01