Skip to content

Instantly share code, notes, and snippets.

@natebird
Forked from wkrsz/devise.rb
Last active December 19, 2015 21:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save natebird/6021627 to your computer and use it in GitHub Desktop.
Save natebird/6021627 to your computer and use it in GitHub Desktop.
#config/initializers/devise.rb
config.warden do |manager|
manager.strategies.add :token_header_authenticatable, TokenHeaderAuthenticatable
manager.default_strategies(:scope => :user).unshift :token_header_authenticatable
end
#lib/token_header_authenticable.rb
class TokenHeaderAuthenticatable < ::Devise::Strategies::Base
def valid?
token_value.present?
end
def authenticate!
resource = mapping.to.find_for_token_authentication(auth_token: token_value)
if resource
success!(resource)
else
fail!
end
end
private
def token_value
if header && header =~ /^Token token="(.+)"$/
$~[1]
end
end
def header
request.headers["Authorization"]
end
end
@natebird
Copy link
Author

The changes were to rename things to be consistent with Devise and simplify the authenticate method.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment