module RedTokenAuth::Controllers::Authentication

Public Instance Methods

authenticate!(resource, options = {}) click to toggle source

class UserController < ApplicationController

before_action only: [:index] { authenticate! :user }

end

# File lib/red_token_auth/controllers/authentication.rb, line 11
def authenticate!(resource, options = {})
  klass = resource.to_s.capitalize.constantize

  #TODO: make this query configurable.
  @resource = klass.where(uid: request.headers["uid"]).first

  unless @resource && @resource.authenticate_token(request.headers["access-token"])
    render_unauthorized
  end

  define_methods(klass)
end
render_unauthorized() click to toggle source
# File lib/red_token_auth/controllers/authentication.rb, line 28
def render_unauthorized
  render json: I18n.t("red_token_auth.messages.unauthorized"), status: :unauthorized
end
resource_name(klass) click to toggle source
# File lib/red_token_auth/controllers/authentication.rb, line 24
def resource_name(klass)
  klass.to_s.downcase.to_sym
end

Private Instance Methods

define_methods(klass) click to toggle source
# File lib/red_token_auth/controllers/authentication.rb, line 35
def define_methods(klass)
  define_singleton_method(:"current_#{resource_name(klass)}") do
    @resource
  end
end