usefulteam / jwt-auth

WordPress JSON Web Token Authentication
https://wordpress.org/plugins/jwt-auth/
124 stars 50 forks source link

How to generate the token given just the user id? #90

Closed hassaannoor closed 1 year ago

hassaannoor commented 1 year ago

Hello team,

I need this because I'm authing a user using google SSO and then creating a user with wp_insert_user. But when the user with the email already exists, I need a way to create a token for them. I can provide the username but not the password. So, I need a way to generate a token for the user given just their user id. This is happening in the backend so it's secure.

Thanks for your continuous development efforts.

dominic-ks commented 1 year ago

Hey @hassaannoor, thanks for the question, you could do that like this:

$user = get_user_by( 'ID' , $user_id );
$auth = new \JWTAuth\Auth;
$token = $auth->generate_token( $user );

Cheers,

hassaannoor commented 1 year ago

@dominic-ks Can't thank you enough. You just saved me hours.

And right now I really wanna use emojis to express my dropped jaw at your response time. Salute.

Thank you so much.