Last active
August 6, 2018 23:21
-
-
Save richmolj/b05f12ab89e4ca7d27a67ff1d8c8743f to your computer and use it in GitHub Desktop.
Devise JWT Auth
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
before_action :authenticate_user_from_token! | |
before_action :authenticate_user! | |
# Ripped from ember-simple-auth-devise | |
def authenticate_user_from_token! | |
if user = user_via_token | |
sign_in(user, store: false) | |
else | |
raise NotAuthorized, 'Access is denied due to invalid token.' | |
end | |
end | |
def user_via_token | |
authd_user = nil | |
authenticate_with_http_token do |token, options| | |
decoded = JsonWebToken.decode(token) | |
username = options[:username].presence | |
if decoded and username | |
authd_user = User.find_by(id: decoded['user_id'], username: username) | |
end | |
end | |
authd_user | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment