Last active
December 25, 2015 13:09
-
-
Save alex-zige/6981762 to your computer and use it in GitHub Desktop.
Cookie based device stragtegy
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
#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