0

我正在使用带有 React 前端的设计令牌身份验证,现在我正在通过电子邮件确认用户。正在发送电子邮件,但不知何故它生成了错误的 URL。

生成的 URL 是。

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

我的路线。

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

我的应用程序/views/devise/mailer/confirmation_instructions.html.erb

<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,我已经覆盖了 confirms_controller.rb 之类的。

# 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

在路线.rb

mount_devise_token_auth_for 'User', at: 'auth', controllers: { confirmations: 'users/confirmations' }
4

1 回答 1

0

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

<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

登录用户后,只需在您的应用中提供路径

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 了。

于 2020-04-10T16:15:56.760 回答