未定义的类JWTAuth

时间:2015-11-21 10:20:54

标签: laravel laravel-5 token jwt json-web-token

在使用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'));
}

因此,不会生成令牌。

请帮助。

1 个答案:

答案 0 :(得分:1)

所以参考关于主要问题的讨论,整个申请工作正常。您只需要在Angular和Laravel之间正确传递参数。