0

While using JWT with laravel, I am getting a message in my editior -

Undefined class JWTAuth

even after adding the following lines :-

use JWTAuth;
use Tymon\JWTAuth\Exceptions\JWTException;

I followed this tutorial https://www.codetutorial.io/laravel-and-angularjs-token-based-auth-part1/ and made the changes but still

app/config.php has :-

Tymon\JWTAuth\Providers\JWTAuthServiceProvider::class,

and

'JWTAuth'   => Tymon\JWTAuth\Facades\JWTAuth::class,
'JWTFactory' => Tymon\JWTAuth\Facades\JWTFactory::class

My 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'));
}

Because of this, tokens are not being generated.

Pls Help.

Stacy J
  • 2,721
  • 15
  • 58
  • 92
  • Is the error in your editor only, or it really throws error in runtime? – Mina Abadir Nov 21 '15 at 18:24
  • @MinaYoussef - only in the editor, and because of it I think token is not generated. – Stacy J Nov 22 '15 at 07:49
  • If no errors are thrown then Laravel can resolve the dependencies. Would you share the code that generates the token? – Mina Abadir Nov 22 '15 at 07:51
  • @MinaYoussef - i edited the post, added TokenAuthController.php code. This is the tutorial I am following - https://www.codetutorial.io/laravel-and-angularjs-token-based-auth-part1/ – Stacy J Nov 22 '15 at 07:56
  • What is the output of the authenticate action? – Mina Abadir Nov 22 '15 at 08:05
  • when i give $credentials manually, and test it on Postman, I get a token. But when authenticate() is called from getAuthenticatedUser(),, i get a error saying, token could not be parsed – Stacy J Nov 22 '15 at 08:10
  • So, the tokens are actually generated. That's the first step. How do you pass the token back to getAuthenticatedUser()? – Mina Abadir Nov 22 '15 at 08:12
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/95828/discussion-between-stacy-j-and-mina-youssef). – Stacy J Nov 22 '15 at 08:14

1 Answers1

1

So reference to the discussion on the main question, the whole application is working fine. You just need to pass the parameters correctly between Angular and Laravel.

Mina Abadir
  • 2,951
  • 2
  • 15
  • 20