I use Knock in Rails to authenticate users using JWT.
I get authentication out of the box, but there are cases where I need to get the user object when I have the jwt . So how do I do this using some function built inside of Knock
get_user(jwt) # should return user if valid or null if not
Doesn't require Knock
, this can be easily done with ruby-jwt
gem which Knock also uses
def get_user(jwt)
decoded_token = JWT.decode jwt, Rails.application.secrets.secret_key_base, true, { :algorithm => 'HS256' }
current_user = User.find((decoded_token[0])['sub']))
current_user
end
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.