我在验证用户身份时遇到问题。这是我控制器中的Authentication方法:
public function authenticate(Request $request){
$user = User::where('username', $request->username)->first();
$user_model = new User;
$return_credentials = [
'message' => "User does not exist.",
'status' => false,
'user_details' => ""
];
if(!empty($user) && !is_null($user)){
$remember = Str::random(60);
if(\Auth::attempt(['username' => $request->username, 'password' => $request->password, 'verified' => '1'], $remember)){
$user = \Auth::user();
$token_result = $user->createToken('Personal Access Token');
$token = $token_result->token;
$token->expires_at = Carbon::now()->addWeeks(1);
$return_credentials['message'] = "User Successfully Logged in.";
$return_credentials['status'] = true;
$token->save();
}else{
$return_credentials['message'] = "User Failed to Log in.";
$return_credentials['status'] = false;
}
}
$return_credentials['user_details'] = $user;
return response()->json($return_credentials);
}
每次我使用此中间件访问路由时:
Route::group(['middleware' => 'auth:api'], function() {
Route::get('user', '\App\Http\Controllers\UserController@verifyUserLoggedIn');
});
它返回:
GET http://..../api/auth/user 401 (Unauthorized)
问:我错过了一步吗?
答案 0 :(得分:0)
在您的auth:api
中添加app\Http\kernel.php
:
'api' => [
'throttle:60,1',
'bindings',
'auth:api',
],