I'm using laravel as my backend.
Once the user has logged into my app, all I have is just a token without any information regards the username or any other user details.
Once I retrive the JWT Token and store it in my frontend (angular2), how I can get the user details?
For the record, i'm using the following laravel library:
https://github.com/tymondesigns/jwt-auth
To get the user information
First get the token from JWTAuth like this:
$token = JWTAuth::getToken();
Then get the user from the token as follows:
$user = JWTAuth::toUser($token);
you can get user information like this way :
$user = JWTAuth::toUser(your_token_here);
I have tested just
$user = JWTAuth::toUser();
Without any parameter and it works as well
I am using Laravel 7.3 with tymon/jwt-auth 1.0
JWTAuth::parseToken()->authenticate();
Related
For my project I have a set of users that should only be able to login by requesting a Magic Link. So they have an email address but no password. To avoid security issues, my goal was to get this working without having to save an authentication token in LocalStorage.
I've tried setting this up the following way with Laravel Sanctum:
When requested, I create a token for the user and email them the plaintext version.
The user would open the link containing the token in the querystring.
I would attach the (Bearer) token with the Authorization Header.
The next step (I assumed) would be to call a custom /api/login endpoint that uses the 'auth:sanctum' middleware. The Bearer token would authenticate the user and then I would manually login the user with Auth::login(). After this the active Session would be used to authenticate the user, thus avoiding having to save the token in localStorage.
But I can't call the Auth::login() method manually without getting an error (BadMethodCallException: Method Illuminate\Auth\RequestGuard::login does not exist.).
I can't figure out why this isn't working, or maybe I am going at this all wrong?
if you sending Sanctum token to user via email so in 1st request you will get token from url and you can use that token to login to application like this
use Laravel\Sanctum\PersonalAccessToken;
public function login(Request $request)
{
$personalAccessToken = PersonalAccessToken::findToken($request->token);
$user = $personalAccessToken->tokenable;
auth()->login($user);
return redirect('/');
}
Helo,
I would like to know how get token by user with laravel passeport ?
I don't want a user to access another user's resource.
I would like to verify that the user's token in the request corresponds to the user's token.
i get the request token with:
$token = $request->bearerToken();
i don't know how to get user token.
Thank you
If you've set up correctly then passport will keep track of the tokens of each user in each device. However, if you want to retrieve the token of the authenticated user simply use Auth::user()->token();
I have the following lines of code
// Creates new password reminder token and deletes old one.
$token = app('auth.reminder.repository')->create($user);
The $token is then used to send an email and allow users to change their password.
In Laravel 5.8 the auth.reminder.repository does not exists.
In Laravel 4.x in was aliased to Auth\Reminders\ReminderServiceProvider
Need help figuring out how to update the code for Laravel 5.8.
$password_broker = app(\Illuminate\Auth\Passwords\PasswordBroker::class);
// Create reset password token.
$token = $password_broker->createToken($user);
$token = $this->jwt->getToken();
$this->jwt->user();
$data = $this->jwt->setToken($token)->toUser();
I am trying to get the user detail based on token in laravel/Lumen but its always return the first user details. I already cross check the login detail which is fine.
Is there a simple way to get the passport client that granted an access token from the auth()->user(). If not get the client from the access token?
Thanks in advance
You can get client id from Laravel Auth object:
$client_id = auth()->user()->Token()->getAttribute('client_id');