设计 authenticate_user 的代码是什么!为 :user 生成后
What is code for devise authenticate_user! after generated for :user
我意识到 authenticate_user!
在 gem 文件中是 not explicitly defined,但我想知道对于典型的应用程序(在名为 User
的模型上进行身份验证),什么会该方法看起来像。我需要知道以便我可以稍微修改它。
我相信你链接到你自己的答案,它定义的方法是
def authenticate_#{mapping}!(opts={})
opts[:scope] = :#{mapping}
warden.authenticate!(opts) if !devise_controller? || opts.delete(:force)
end
如果我们用真实的 class 代替,在您的情况下 User
,它看起来像:
def authenticate_user!(opts={})
opts[:scope] = :user
warden.authenticate!(opts) if !devise_controller? || opts.delete(:force)
end
所以它真的呼唤监狱长,这就是大部分身份验证逻辑所在。
对于典型的 Rails 应用程序,authenticate_user!
方法将被定义为 ApplicationController
上的 instance_method
。
Devise
使用 Warden 进行身份验证。为了使用它 Devise
提供了自己的身份验证策略来实现 authenticate!
方法。这就是你所需要的。您已经有了代码的第一部分(来自您问题中的 link),即:
def authenticate_user!(opts={})
opts[:scope] = :user
warden.authenticate!(opts) if !devise_controller? || opts.delete(:force)
end
在上面的代码中,warden.authenticate!
根据选择的 Devise
策略使用来自 Devise
的方法(由 Devise
实现)。
比如实现DatabaseAuthenticatable
策略的方法在这里:https://github.com/plataformatec/devise/blob/master/lib/devise/strategies/database_authenticatable.rb
实现Rememberable
策略的方法在这里:https://github.com/plataformatec/devise/blob/master/lib/devise/strategies/rememberable.rb
我意识到 authenticate_user!
在 gem 文件中是 not explicitly defined,但我想知道对于典型的应用程序(在名为 User
的模型上进行身份验证),什么会该方法看起来像。我需要知道以便我可以稍微修改它。
我相信你链接到你自己的答案,它定义的方法是
def authenticate_#{mapping}!(opts={})
opts[:scope] = :#{mapping}
warden.authenticate!(opts) if !devise_controller? || opts.delete(:force)
end
如果我们用真实的 class 代替,在您的情况下 User
,它看起来像:
def authenticate_user!(opts={})
opts[:scope] = :user
warden.authenticate!(opts) if !devise_controller? || opts.delete(:force)
end
所以它真的呼唤监狱长,这就是大部分身份验证逻辑所在。
对于典型的 Rails 应用程序,authenticate_user!
方法将被定义为 ApplicationController
上的 instance_method
。
Devise
使用 Warden 进行身份验证。为了使用它 Devise
提供了自己的身份验证策略来实现 authenticate!
方法。这就是你所需要的。您已经有了代码的第一部分(来自您问题中的 link),即:
def authenticate_user!(opts={})
opts[:scope] = :user
warden.authenticate!(opts) if !devise_controller? || opts.delete(:force)
end
在上面的代码中,warden.authenticate!
根据选择的 Devise
策略使用来自 Devise
的方法(由 Devise
实现)。
比如实现DatabaseAuthenticatable
策略的方法在这里:https://github.com/plataformatec/devise/blob/master/lib/devise/strategies/database_authenticatable.rb
实现Rememberable
策略的方法在这里:https://github.com/plataformatec/devise/blob/master/lib/devise/strategies/rememberable.rb