【问题标题】:How do I get redirected to the right url with emailed confirmation and password reset links with ng-token-auth and devise-token-auth?如何使用带有 ng-token-auth 和 devise-token-auth 的电子邮件确认和密码重置链接重定向到正确的 URL?
【发布时间】:2017-06-22 20:23:58
【问题描述】:

我正在尝试在单独的前端使用 ng-token-auth (Angular 1),以允许在 Ruby on Rails 5.0 后端重置密码并确认帐户电子邮件地址。

点击到达身份验证 api 的链接后,我被重定向到此:

http://localhost:8080/?client_id=<client_id>&config=default&expiry=<expiry_stamp>reset_password=true&token=<token>&uid=<email address>#/reset-password

/#/reset-password 部分被放置在重置链接的末尾。

我认为要获得角度和我的功能正常工作,我需要类似的东西: http://localhost:8080/#/reset-password?client_id=&config=&expiry=&reset_password=&token=&uid=

app/views/devise/mailer/reset_password_instructions.html.erb我有以下模板代码:

    <p><%= t(:hello).capitalize %> <%= @resource.email %>!</p>

    <p><%= t '.request_reset_link_msg' %></p>

    <p><%= link_to t('.password_change_link'), edit_password_url(@resource, reset_password_token: @token, config: message['client-config'].to_s, redirect_url: message['redirect-url'].to_s).html_safe %></p>


    <p><%= t '.ignore_mail_msg' %></p>
    <p><%= t '.no_changes_msg' %></p>

在前端的 app.js 中,我正在使用以下内容配置 $authProvider

   function AuthSetup($authProvider) {
     $authProvider.configure({
      apiUrl: 'http://localhost:3000',
      passwordResetSuccessUrl: 'http://localhost:8080/#/reset-password'
    });
   }

如果有人能提供任何帮助或指导,我将不胜感激。谢谢。

【问题讨论】:

  • 您确定该链接应该是从网站而不是电子邮件中单击的吗?
  • 如果您使用的是 Rails 后端,那么让 Devise 为您处理这些事情。您可能应该只将 user_id/ User 对象发送到相应的操作,然后发送电子邮件..查看此处了解详细信息stackoverflow.com/a/41790443/2545197
  • @maxple 这是通过电子邮件发送的链接。
  • @Abhinay Devise 已经在发送电子邮件。它似乎没有发送正常工作的链接,因为它在哪里设置 /#/reset-password 部分。如果我加载了链接并正常工作,我相信它应该对单个短会话进行身份验证,以允许用户在表单上更新他们的密码。
  • @Robert 我不确定这是否是正确的处理方式,因为用户将访问您的角度前端,因此您可能必须编写一个自定义逻辑来接受来自该 url 的令牌并检查它是否匹配与否

标签: ruby-on-rails angularjs ruby devise


【解决方案1】:

抱歉回复晚了。我所做的只是覆盖 Rails 后端的Devise::ConfirmationController,它对我有用。

下面分享代码sn-p:

class User::ConfirmationsController < Devise::ConfirmationsController

  def show
    self.resource = resource_class.confirm_by_token(params[:confirmation_token])

    if resource.errors.empty?
      set_flash_message(:notice, :confirmed) if is_navigational_format?
      sign_in(resource_name, resource)
      respond_with_navigational(resource){ redirect_to after_confirmation_path }
    else
      redirect_to after_confirmation_path, notice: 'Not a valid token'
    end
  end

  protected

  def after_confirmation_path
    return 'http://localhost:4000' if Rails.env.dev?
    'http://example.com'
  end
end

如果令牌是有效的,那么它将登录用户并将他们重定向到运行您的 Angular 代码的前端站点,如果令牌无效,那么它会将他们重定向到站点以及一个 Flash 消息。如果您在理解这段代码方面需要任何帮助,请告诉我。

【讨论】:

    猜你喜欢
    • 2018-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-16
    • 2017-03-13
    • 1970-01-01
    • 2020-01-09
    相关资源
    最近更新 更多