Gre*_*reg 67 ruby-on-rails oauth devise oauth-2.0
设计authenticate_user!
方法在哪里设计?
我一直在寻找它,到目前为止还没有找到它.
jup*_*p0r 70
它在lib/devise/controllers/helpers.rb
1中并且是动态生成的(用户只是可能的后缀之一):
def self.define_helpers(mapping) #:nodoc:
mapping = mapping.name
class_eval <<-METHODS, __FILE__, __LINE__ + 1
def authenticate_#{mapping}!(opts={})
opts[:scope] = :#{mapping}
warden.authenticate!(opts) if !devise_controller? || opts.delete(:force)
end
def #{mapping}_signed_in?
!!current_#{mapping}
end
def current_#{mapping}
@current_#{mapping} ||= warden.authenticate(:scope => :#{mapping})
end
def #{mapping}_session
current_#{mapping} && warden.session(:#{mapping})
end
METHODS
ActiveSupport.on_load(:action_controller) do
helper_method "current_#{mapping}", "#{mapping}_signed_in?", "#{mapping}_session"
end
end
Run Code Online (Sandbox Code Playgroud)
Ult*_*rus 21
当您将设计添加到rails时,通常会添加config/routes.rb
:
devise_for :user
Run Code Online (Sandbox Code Playgroud)
这是在Devise Mapper类中定义的.
它要求Devise.add_mapping
每个资源都传递给它devise_for
此处定义了Devise模块的add_mapping方法,该方法随后调用define_helpers
,其定义authenticate
如其他答案中所述.
它在这里使用了一些元编程声明 - https://github.com/plataformatec/devise/blob/master/lib/devise/controllers/helpers.rb#L46-49
class_eval <<-METHODS, __FILE__, __LINE__ + 1
def authenticate_#{mapping}!(opts={})
opts[:scope] = :#{mapping}
warden.authenticate!(opts) if !devise_controller? || opts.delete(:force)
end
...
end
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
43525 次 |
最近记录: |