Skip to content

Instantly share code, notes, and snippets.

@renanregis
Last active June 20, 2018 17:03
Show Gist options
  • Save renanregis/5a650a1fd63d1468f2933a7b629fe1b8 to your computer and use it in GitHub Desktop.
Save renanregis/5a650a1fd63d1468f2933a7b629fe1b8 to your computer and use it in GitHub Desktop.
Authentication HTTP Token with Rails 5.x
#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