-
-
Save regedarek/5940403 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
#config/initializers/devise.rb | |
config.warden do |manager| | |
manager.strategies.add :token_header_authenticable, TokenHeaderAuthenticable | |
manager.default_strategies(:scope => :user).unshift :token_header_authenticable | |
end |
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
#lib/token_header_authenticable.rb | |
class TokenHeaderAuthenticable < ::Devise::Strategies::Base | |
def valid? | |
token_value.present? | |
end | |
def authenticate! | |
resource_scope = mapping.to | |
resource = resource_scope.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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment