gpt4 book ai didi

laravel - RequestGuard::attempt 不存在

转载 作者:行者123 更新时间:2023-12-04 16:04:08 31 4
gpt4 key购买 nike

当我登录管理员时,我正在尝试对 api 进行多重保护身份验证,我得到了跟随错误

BadMethodCallException Method Illuminate\Auth\Req uestGuard::attempt does not exist.

这是我在 Controller 中的登录方法

 public function login(Request $request){
if(Auth::guard('admin-api')->attempt(['email' => request('email'), 'password' => request('password')]))
{

// if successful, then redirect to their intended location


$user = Auth::guard('admin-api');
$success['token'] = $user->createToken('admin')->accessToken;
return response()->json(['success' => $success], $this->successStatus);
}
else{
return response()->json(['error'=>'Unauthorised'], 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');
});


});

我的管理模型

<?php

namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Laravel\Passport\HasApiTokens;

class Admin extends Authenticatable
{
use HasApiTokens, Notifiable;
protected $table = 'admin';
protected $guard = 'admin-api';
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name', 'email', 'password',
];

/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token',
];

请告诉我你想要的任何其他输入

最佳答案

更好的技巧是这样

   $credentials = ['email' => $request->username, 'password' => $request->password];

//Since we are not using guard web in API request, we have to add it here (
// guard('web') ) to access the Auth::attempt function,
// the Auth::attempt needs web guard and crsf token, but using it here bypasses
// the post with crsf.
if (Auth::guard('web')->attempt($credentials, false, false)) {
dd('user is OK');
}else{
dd('user is NOT OK');
}

关于laravel - RequestGuard::attempt 不存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49443023/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com