【问题标题】:Devise Token Auth / Angular2-Token, update password, Completed 401 UnauthorizedDevise Token Auth / Angular2-Token,更新密码,完成 401 Unauthorized
【发布时间】:2017-04-16 15:35:13
【问题描述】:

我在使用 devise_token_auth 恢复密码时遇到问题。和 Angular2 令牌。我已成功收到包含更新密码链接的电子邮件。但是我在提交新密码时收到 401 Unauthorized 响应。

前端。我从带有urlParams.get('token') 的URL 获取令牌

  onPasswordUpdate() { 
    let token = this.urlParams.get('token');
    var obj = Object.assign(this._updatePasswordData, { reset_password_token: token })
    this._tokenService.patch('auth/password/', obj ).subscribe(
      res =>    res,
      error =>  error
    );
  }

后端响应。

Started PATCH "/api/auth/password/" for 127.0.0.1 at 2016-12-01 21:17:48 +0100
Processing by DeviseTokenAuth::PasswordsController#update as JSON
  Parameters: {"reset_password_token"=>"[FILTERED]", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}
Completed 401 Unauthorized in 1ms (Views: 0.4ms | ActiveRecord: 0.0ms)

在电子邮件的链接中,我得到以下令牌:reset_password_token=HneZDoKTMCLF3_SLfnxy
当我访问该链接时,用户记录会更新为以下属性:

reset_password_token: "aa3cba76c7b1d8f78cde6856f43e1cce57f5fc8e5301842733de677eff909bc1"
tokens: {}

然后在浏览器 URL 中我得到以下token=agejaip2SqOp9nvwE1GAHQ&uid
然后使用以下属性更新用户记录:

...
reset_password_token: "HneZDoKTMCLF3_SLfnxy",
tokens: {"pv9i1BDTM29ezep0KSPzpA"=>{"token"=>"$2a$10$cS9gbe9UBICcgphZHRAENOMS6NlEe0Em1cNufY3LSRTPE.hRMabvi", "expiry"=>1481834221}}
...

在我看来,我在 URL 中返回的令牌不正确。
那些人有想法吗?

抱歉,有点难以解释。
非常感谢。

导轨 (4.2.4)
devise_token_auth (0.1.34)
设计 (= 3.5.1)
angular2-token: 0.2.0-beta.1

【问题讨论】:

    标签: authentication angular rails-api devise-recoverable


    【解决方案1】:

    我最近遇到了类似的挑战,我就是这样解决的。

    为您的后端公开“access-token”、“expiry”、“token-type”、“uid”、“client”。检查herehere

    config.middleware.use Rack::Cors do
        allow do
           origins '*'
           resource '*',
              :headers => :any,
              :expose => ['access-token', 'expiry', 'token-type', 'uid', 'client'],
              :methods => => [:get, :post, :options, :delete, :put, :patch]
        end
    end
    

    将您的 redirect_url 设置为 path: /password, method: POST。查看信息here

    我们需要修改reset_password_instructions.html.erb 使其指向api GET /auth/password/edit。更多信息提供here

    例如如果您的 API 在 api 命名空间下:

    <%= link_to 'Change my password', edit_api_user_password_url(reset_password_token: @token, config: message['client-config'].to_s, redirect_url: message['redirect-url'].to_s) %>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-08-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-05
      • 2019-02-21
      • 2017-10-22
      • 2013-07-18
      相关资源
      最近更新 更多