Warden 回调应该放置在 Rails 应用程序中的什么位置?
我对 Rails 比较陌生。我已设置 Devise,并希望在用户登录后运行一些回调代码。
查看 Warden wiki 页面 ,我可以使用“after_set_user”回调来执行此逻辑,例如:
Warden::Manager.after_set_user do |user, auth, opts|
unless user.active?
auth.logout
throw(:warden, :message => "User not active")
end
end
但是,我不确定应该在哪里存储这些内容。我的第一个想法是我可以将它放在 config/initializers/devise.rb 中。这是正确的吗?将本质上是控制器代码的内容放在配置目录中感觉不对。
I'm relatively new to rails. I have Devise set up, and want to run some callback code after users sign in.
Looking at the Warden wiki page, I can use the "after_set_user" callback to perform this logic, for example:
Warden::Manager.after_set_user do |user, auth, opts|
unless user.active?
auth.logout
throw(:warden, :message => "User not active")
end
end
However, I'm not sure where I should be storing this stuff. My first thought is that I could put it in config/initializers/devise.rb. Is that correct? It doesn't feel right putting what is essentially controller code in the config directory.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您的应用程序启动时,需要使用 Warden 挂钩,因此位于 config/initializers/devise.rb 的 Devise 初始化程序内是一个不错的选择。
但是,使用此 Devise 功能可以更好地实现您想要实现的行为:
https://github.com/plataformatec/devise/wiki/How-To:-Customize-user-account-status-validation-when-logging-in
Warden hooks need to be required when your application is booting, so inside Devise's initializer at config/initializers/devise.rb is a good candidate.
However, the behavior you want to achieve will be better accomplished by using this Devise feature:
https://github.com/plataformatec/devise/wiki/How-To:-Customize-user-account-status-validation-when-logging-in