Skip to content

Instantly share code, notes, and snippets.

@richmolj
Last active August 6, 2018 23:21
Show Gist options
  • Save richmolj/b05f12ab89e4ca7d27a67ff1d8c8743f to your computer and use it in GitHub Desktop.
Save richmolj/b05f12ab89e4ca7d27a67ff1d8c8743f to your computer and use it in GitHub Desktop.
Devise JWT Auth
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