lib/tiny_auth/controller.rb in tiny_auth-1.0.0 vs lib/tiny_auth/controller.rb in tiny_auth-2.0.0
- old
+ new
@@ -1,9 +1,9 @@
require "active_support/core_ext/object/blank"
module TinyAuth
- module Controller
+ class Controller < Module
# Extract a token from a request
# @param request [ActionDispatch::HTTP::Request]
# @return [String,nil]
def self.token(request)
header = request.authorization.to_s
@@ -13,27 +13,26 @@
# Defines a before action that will authenticate the resource.
# It also defines methods for accessing the currently authenticated
# resource.
# @param model [ActiveRecord::Base]
# @param name [Symbol] Used to define methods like `current_user`
- # @param options [Hash] Additional arguments for `before_action`
#
# @example
# class ApplicationController < ActionController::Base
- # extend TinyAuth::Controller
+ # include TinyAuth::Controller.new(model: User)
#
- # authenticates model: User, only: :index
+ # before_action :authenticate_user
#
# def index
# if user_signed_in?
# render json: current_user
# else
# head :unauthorized
# end
# end
# end
- def authenticates(model:, name: model.model_name.singular, **options)
+ def initialize(model:, name: model.model_name.singular)
authenticate = :"authenticate_#{name}"
current = :"current_#{name}"
current_ivar = :"@current_#{name}"
signed_in = :"#{name}_signed_in?"
@@ -49,10 +48,8 @@
if token
resource = model.find_by_token(token)
instance_variable_set(current_ivar, resource)
end
end
-
- before_action(authenticate, **options)
end
end
end