Created
April 15, 2016 17:33
-
-
Save hchood/54f56f065eec597fa508b1013f361eaf to your computer and use it in GitHub Desktop.
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
# app/strategies/token_authentication_strategy.rb | |
class TokenAuthenticationStrategy < Warden::Strategies::Base | |
def valid? | |
env['HTTP_AUTHORIZATION'].present? | |
end | |
def authenticate! | |
if user | |
success!(user) | |
else | |
fail!(I18n.t('warden.messages.failure')) | |
end | |
end | |
def store? | |
false | |
end | |
def token | |
env['HTTP_AUTHORIZATION'].sub('Token token=', '') | |
end | |
def user | |
@user ||= User. | |
where(authentication_token: token). | |
where('authentication_token_expires_at > ?', Time.current). | |
first | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment