[英]Get user given jwt Knock Rails JWT
I use Knock in Rails to authenticate users using JWT. 我使用Knock in Rails使用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 . 我可以立即获得身份验证,但是在有些情况下, 当我拥有jwt时 ,需要获取用户对象 。 So how do I do this using some function built inside of Knock 所以我该如何使用内置在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 不需要Knock
,这可以通过Knock也使用的ruby-jwt
gem轻松完成
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.