Get user given jwt Knock Rails JWT

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

Upvotes: 2

Views: 969

Answers (1)

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

Upvotes: 4

Related Questions