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?
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 ;)
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);