如何使用 Ruby on Rails 和 Devise Gem 重定向未经确认的用户
我想更改设备的用户登录行为,以便在未确认用户时将用户重定向到不同的页面。
我的 app/controllers/sessions_controller.rb
中有以下代码:
# POST /user/sign_in
def create
@user = warden.authenticate!(:scope => :user)
sign_in @user
respond_with @user, :location => after_sign_in_path_for(@user)
end
当用户输入正确的用户名/密码但未经确认时,Warden 将引发错误,将用户重定向到登录页面(代码 302)。同时这会将 flash[:alert] 置于“未确认”状态。
有没有办法将未经确认的用户重定向到特定页面?
我解决这个问题的想法是读取 flash[:alert] 值,如果该值“未经确认”但典狱长没有发送有关用户的附加数据,则重定向到适当的页面。
I would like to change the user sign-in behaviour of devise in a way to redirect a user to a different page when that user is not confirmed.
I have the following code in my app/controllers/sessions_controller.rb
:
# POST /user/sign_in
def create
@user = warden.authenticate!(:scope => :user)
sign_in @user
respond_with @user, :location => after_sign_in_path_for(@user)
end
When the user has put correct username/password and is not confirmed Warden will raise an error that will redirect the user to the sign in page (code 302). At the same time this will put flash[:alert] to "unconfirmed".
Is there a way to redirect an unconfirmed user to a specific page?
My idea to work around this was reading the flash[:alert] value and redirecting to the appropriate page if the value is "unconfirmed" but warden is not sending additional data about the user.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您需要一个自定义的 FailureApp 来执行此操作(并且您需要在路线中定义 /unconfirmed 等,就像任何其他 Rails 操作一样):
lib/custom_failure.rb
config/initializers/devise.rb:
You need a custom FailureApp to do this (and you need to define /unconfirmed in your routes, etc., like any other Rails action):
lib/custom_failure.rb
config/initializers/devise.rb:
我认为要使其正常工作,您需要将 Brian 的答案与您的routes.rb 文件中的添加内容结合起来。
我现在要对其进行测试,如果/当我让它工作时我会给你更新。 (编辑:我已经在本地工作了)。
您需要执行以下操作:
在
your_rails_app/app/controllers
中创建一个名为sessions_controller.rb
的新文件,该文件应如下所示:将以下行添加到你的routes.rb文件(或者修改你的routes.rb文件中包含
devise_for :users
的行......或者你的模型的名称):您需要将其添加到您的
User
模型中:<前><代码>受保护
def 需要确认吗?
错误的
结尾
I think that to get this working you need to combine Brian's answer with an addition to your routes.rb file.
I'm going to test it out right now so I'll give you an update if/when I get it working. (EDIT: I've got this working locally).
Here's what you need to do:
Create an new file in
your_rails_app/app/controllers
calledsessions_controller.rb
which should look like this:Add the following line to your routes.rb file (or modify the line in your routes.rb file that contains
devise_for :users
...or whatever your model is called):You need to add this to your
User
model:如果您不想允许未经确认的用户登录,请将此行放入 devise.rb 中
注册成功后。
Please put this line into devise.rb, if you do not want to allow unconfirmed user to sign in
after successful registration.