Jwt-auth: Get user after auth

Created on 10 Sep 2015  路  12Comments  路  Source: tymondesigns/jwt-auth

Hello Folks,

after authenticating the user successfully in my application with:

$token = JWTAuth::attempt($credentials)

I want to get the User object. I tried it the following way:

$token = JWTAuth::attempt($credentials)
JWTAuth::setToken($token);
$user = JWTAuth::parseToken()->authenticate();

But it does not work. Any ideas?

Most helpful comment

I solved it using JWTAuth::setToken($token) before calling JWTAuth::toUser

$token=($request->token ? $request->token : $request->bearerToken() );
JWTAuth::setToken($token);
$user = JWTAuth::toUser($token);

All 12 comments

JWTAuth::parseToken() retrieves the token from the request (either the header or the URL parameters). Since you don't seem to have a request going on, that's just going to throw an exception.

Once you've set the token, JWTAuth::authenticate() should work alone. Or you can explicitly pass it a token with JWTAuth::authenticate($token); then you wouldn't even need to set the class internal copy with setToken, though depending on if/when you are going to use it again, your way might make more sense.

You could use the toUser method:

$token = JWTAuth::attempt($credentials);
$user = JWTAuth::toUser($token);

I've tried every combination of possible token retrieval to get a user and it always comes up false. I'm using it in a constructor function for a parent Endpoint class in an API like so...

    /**
     * APIController constructor.
     */
    public function __construct(Request $request)
    {
        $this->manager = new Manager();
        $this->user = \JWTAuth::authenticate(JWTAuth::getToken());
        dd($this->user);
        $this->account = Account::find($this->user->account_id);
    }

I've also tried: JWTAuth::toUser(JWTAuth::getToken())

As well as a few others I'm forgetting at this point. :)

The token returns fine. When I dump the contents of the getToken call or the parseToken call, I get the token but the second I pass it into any method to get a user it comes back false.

Am I doing something wrong here? Is there something special about a controller that is a parent class?

My business goal is simply to set a property on all my controllers that need it which carries the logged in user.

@webkenny What version of this package are you using ?

Same issues with 0.5.9 Its giving me false when i try to get user from token But Token returns fine...
Please help

The above answer from @pespantelis no longer seems to work. I get a JWTException saying A token is required.

Currently using the develop branch.

To overcome this I've simply done

$token = JWTAuth::attempt($credentials)
$user = Auth::user();

@rohan-deshpande same problem here, but $user = Auth::user(); is safe to use? it will retrive the correct autenticated user ?

@renanpro03 Yes it should since you've just made an auth attempt with their credentials.

I stayed on this error for a day which took most of my development time. Just like @rohan-deshpande , I'm using the dev-develop version .

I kept on using
$user = JWTAuth::toUser($token);
Which kept throwing error "A token is required" Until I used @rohan-deshpande solution.

@tymondesigns You should try to provide recent documentation both the dev-develop version and the other versions.

Thanks All

I am facing same issue. I tried every possible way but all the time getting same error.

$user = $this->jwt->toUser($token); and $user = $this->jwt->authenticate($token);

Both throws an same error A token is required

Although $user = Auth::user(); is works fine.

I solved it using JWTAuth::setToken($token) before calling JWTAuth::toUser

$token=($request->token ? $request->token : $request->bearerToken() );
JWTAuth::setToken($token);
$user = JWTAuth::toUser($token);

Solved it by implementing @omarnas ' answer ;)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Rasoul-Karimi picture Rasoul-Karimi  路  3Comments

loic-lopez picture loic-lopez  路  3Comments

hfalucas picture hfalucas  路  3Comments

kofi1995 picture kofi1995  路  3Comments

marciomansur picture marciomansur  路  3Comments