Skip to content

Instantly share code, notes, and snippets.

@alex-zige
Last active December 25, 2015 13:09
Show Gist options
  • Save alex-zige/6981762 to your computer and use it in GitHub Desktop.
Save alex-zige/6981762 to your computer and use it in GitHub Desktop.
Cookie based device stragtegy
#devise/cookie_token_auth_strategy
module CookieTokenAuthStrategy
Warden::Strategies.add(:token_cookie_strategy) do
def valid?
token_from_cookie
end
def authenticate!
if token_from_cookie.present? && (user = User.find_for_token_authentication(:auth_token => token_from_cookie)).present?
delete_token_from_cookie
success!(user)
else
fail!("could not log in")
end
end
def token_from_cookie
cookies[:authentication_token]
end
def delete_token_from_cookie
cookies.delete(:authentication_token)
end
end
end
# app/initializer/devise.rb
config.warden do |manager|
manager.default_strategies(:scope => :user).unshift :token_cookie_strategy
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment