首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在设计令牌中如何重定向登录页面/或在确认电子邮件后使用户登录成功

在设计令牌中如何重定向登录页面/或在确认电子邮件后使用户登录成功
EN

Stack Overflow用户
提问于 2020-04-10 12:04:47
回答 1查看 753关注 0票数 0

我正在使用的设计令牌与反应前端,现在我是让用户确认通过电子邮件。电子邮件正在发送,但不知何故它生成了一个错误的URL。

生成的URL是。

http://localhost:3001/auth/confirmation.4?confirmation_token=AoWH2yYxuHHnBzJRF746

我的路线。

代码语言:javascript
复制
new_user_confirmation GET      /auth/confirmation/new(.:format)                                                         users/confirmations#new
                    user_confirmation GET      /auth/confirmation(.:format)                                                             users/confirmations#show
                                      POST     /auth/confirmation(.:format)                                                             users/confirmations#create

我的app/views/devise/mailer/confirmation_instructions.html.erb

代码语言:javascript
复制
<p>Welcome <%= @email %>!</p>

<p>You can confirm your account email through the link below:</p>

<p><%= link_to 'Confirm my account', user_confirmation_url(confirmation_token: @token) %></p>

单击“确认我的帐户”后,它会成功地将我带到我的应用程序,但使用错误的URL。如果我能做到以下其中之一,我会很高兴的。

  1. 用户在确认后自动登录。
  2. 在确认帐户后进入登录页面,然后他给您凭据和登录。

对于1,我重写了类似的confirmations_controller.rb。

代码语言:javascript
复制
# frozen_string_literal: true

class Users::ConfirmationsController < Devise::ConfirmationsController
  # The path used after confirmation.
   def after_confirmation_path_for(resource_name, resource)
     sign_in(resource) # In case you want to sign in the user
     root_path
   end
end

在routes.rb中

代码语言:javascript
复制
mount_devise_token_auth_for 'User', at: 'auth', controllers: { confirmations: 'users/confirmations' }
EN

回答 1

Stack Overflow用户

发布于 2020-04-10 16:15:56

首先,您在电子邮件中使用的url助手不需要接收@resource --这就是它使用.4生成url的原因。应该是

代码语言:javascript
复制
<p><%= link_to 'Confirm my account', user_confirmation_url(confirmation_token: @token) %></p

第二,根据本教程,您可以在确认后重定向用户,就像您已经尝试过的一样。https://github.com/heartcombo/devise/wiki/How-To:-Add-:confirmable-to-Users#redirecting-user

在用户登录后,只需在应用程序中给出一个路径即可。

代码语言:javascript
复制
def after_confirmation_path_for(resource_name, resource)
  sign_in(resource) # In case you want to sign in the user
  your_new_after_confirmation_path
end

因此,在登录后不需要再去session_new。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61139880

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档