1

我包括devise_token_auth从其他地方托管的网络应用程序登录。但我也希望能够直接登录到我的 rails 应用程序。

我的 routes.rb 看起来像这样:

#...
devise_for :users

namespace :api, defaults: {format: :json} do
  mount_devise_token_auth_for 'User', at: 'auth'
  #...

为了重置密码,webapp 发送一个POST/api/auth/password. 使用上面的配置,电子邮件中重置密码的链接使用了错误的控制器(打开的那个users/password)。redirect_url 没有被应用,用户看到的是 rails 应用程序的登录表单,而不是 web 应用程序:

http://localhost:3000/users/password/edit?redirect_url=http://localhost:8080/reset_password&reset_password_token=...

如果我评论该行devise_for :users,则电子邮件链接是正确的,请使用api/auth/password/edit

http://localhost:3000/api/auth/password/edit?redirect_url=http://localhost:8080/reset_password&reset_password_token=...

但是,当然,通过评论devise_for :users我不能只使用 rails 应用程序(on http://localhost:3000/users/sign_in)登录。

4

1 回答 1

0

在其邮件模板上设计以重置密码(reset_password_instructions.html.erb)调用模型以获取 url:

<p><%= link_to 'Change my password', edit_password_url(@resource, reset_password_token: @token) %></p>

尽管最初处理客户端重置密码意图的控制器是DeviseTokenAuth::PasswordsController,但函数edit_password_urlonUser解析为由Devise::PasswordsController( users/password/editvs api/auth/password/edit) 处理的路径。

在这种情况下,解决方案是使用不同的模型:

路线.rb

#...
devise_for :users

namespace :api, defaults: {format: :json} do
  mount_devise_token_auth_for 'Api::User', at: 'auth'
  #...

api/user.rb

class Api::User < ActiveRecord::Base
   devise :database_authenticatable, :recoverable,
     :rememberable, :trackable, :validatable
   include DeviseTokenAuth::Concerns::User
   #...
end
于 2018-01-17T15:53:44.067 回答