Last active
February 22, 2016 15:51
-
-
Save jordifierro/b97c16d4acb6461fac89 to your computer and use it in GitHub Desktop.
Example of a Rails api authentication module using ActiveSupport::Concern
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
module Api::V1::Concerns | |
module Authenticator | |
extend ActiveSupport::Concern | |
included do | |
before_action :auth_with_token! | |
end | |
def current_user | |
@current_user ||= User.find_by(auth_token: request.headers['Authorization']) | |
end | |
def user_signed_in? | |
current_user.present? | |
end | |
def auth_with_token! | |
head :unauthorized unless user_signed_in? | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment