在将 JWT 与 laravel 一起使用时,我在编辑器中收到一条消息 -
Undefined class JWTAuth
即使添加了以下几行:-
use JWTAuth;
use Tymon\JWTAuth\Exceptions\JWTException;
我按照本教程https://www.codetutorial.io/laravel-and-angularjs-token-based-auth-part1/进行了更改,但仍然
app/config.php 有:-
Tymon\JWTAuth\Providers\JWTAuthServiceProvider::class,
和
'JWTAuth' => Tymon\JWTAuth\Facades\JWTAuth::class,
'JWTFactory' => Tymon\JWTAuth\Facades\JWTFactory::class
我的 TokenAuthController -
public function authenticate(Request $request)
{
$credentials = $request->only('email', 'password');
try {
if (! $token = JWTAuth::attempt($credentials)) {
return response()->json(['error' => 'invalid_credentials'], 401);
}
} catch (JWTException $e) {
return response()->json(['error' => 'could_not_create_token'], 500);
}
// if no errors are encountered we can return a JWT
return response()->json(compact('token'));
}
public function getAuthenticatedUser()
{
try {
if (! $user = JWTAuth::parseToken()->authenticate()) {
return response()->json(['user_not_found'], 404);
}
} catch (Tymon\JWTAuth\Exceptions\TokenExpiredException $e) {
return response()->json(['token_expired'], $e->getStatusCode());
} catch (Tymon\JWTAuth\Exceptions\TokenInvalidException $e) {
return response()->json(['token_invalid'], $e->getStatusCode());
} catch (Tymon\JWTAuth\Exceptions\JWTException $e) {
return response()->json(['token_absent'], $e->getStatusCode());
}
return response()->json(compact('user'));
}
因此,不会生成令牌。
请帮忙。