Skip to content

Instantly share code, notes, and snippets.

@hchood
Created April 15, 2016 17:33
Show Gist options
  • Save hchood/54f56f065eec597fa508b1013f361eaf to your computer and use it in GitHub Desktop.
Save hchood/54f56f065eec597fa508b1013f361eaf to your computer and use it in GitHub Desktop.
# 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