Last active
June 20, 2018 17:03
-
-
Save renanregis/5a650a1fd63d1468f2933a7b629fe1b8 to your computer and use it in GitHub Desktop.
Authentication HTTP Token with Rails 5.x
This file contains hidden or 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
#Add this line in controller | |
include ActionController::HttpAuthentication::Token::ControllerMethods | |
before_action :authenticate | |
#Method to get the token via Header | |
#You can create some custom pattern, in this case I use Bearer. | |
private | |
def authenticate | |
authenticate_or_request_with_http_token do |token| | |
pattern = /^Bearer / | |
header = request.headers['Authorization'] | |
header.gsub(pattern, '') if header && header.match(pattern) | |
puts token | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment