我正在尝试获取经过身份验证的用户数据,以便显示用户的名称。我正在使用AngularJS和Laravel。
当我这样做时:
public function getUser(){
if(Auth::check()){
$user = Auth::user();
$token = JWTAuth::fromUser($user);
dd($user);
return response()->success(compact('user','token'));
}
}
Auth为空,但我已登录。
这是我的登录方式
public function login(Request $request)
{
$this->validate($request, [
'email' => 'required|email',
'password' => 'required|min:8',
]);
$credentials = $request->only('email', 'password');
try {
// verify the credentials and create a token for the user
if (! $token = JWTAuth::attempt($credentials)) {
return response()->error('Invalid credentials', 401);
}
} catch (\JWTException $e) {
return response()->error('Could not create token', 500);
}
$user = Auth::user();
return response()->success(compact('user', 'token'));
}
谢谢!