【问题标题】:How to change password via API using Devise Token Auth? Rails 5如何使用 Devise Token Auth 通过 API 更改密码?导轨 5
【发布时间】:2017-10-19 21:24:06
【问题描述】:

我正在尝试使用 Devise Token Auth gem 更改用户的密码。但是有些参数我不知道好不好,因为我是从数据库中提取出来的。具体来说,来自我的 users 表的 Tokens 列。

根据文档告诉我的内容,这是我的请求及其各自的标头:

PUT /auth/password HTTP/1.1
Host: localhost
Content-Type: application/json;charset=utf-8
token-type: Bearer
access-token: $2a$10$d6b2GP5N3WT4/fR62S1VL.kiLx9w0YA6Rb5aVYQtXMBPImLx9ix2i
client: -Z2_wBWzH1GHiAjCAaHAKA
expiry: 1509654584
uid: my@email.com

password=123456789&password_confirmation=123456789

这是我的用户表的令牌列,对应于我的用户,电子邮件为my@email.com

{
    "oJ7t-1kXUFsde9J_euKuZA":{
        "token":"$2a$10$cYAP0ZVndFJz9JUK4tvoOuc96k/JBtgmSyn0cmwvWwy0o.J0XOtKW",
        "expiry":1509642442,
        "last_token":"$2a$10$Boy8Yp2Znb0uOS9tt/3KYum7PX/Jjmb.Igzo5GUs4tDRCejDU5bya",
        "updated_at":"2017-10-19T12:07:22.356-05:00"
    },
    "-Z2_wBWzH1GHiAjCAaHAKA":{
        "token":"$2a$10$d6b2GP5N3WT4/fR62S1VL.kiLx9w0YA6Rb5aVYQtXMBPImLx9ix2i",
        "expiry":1509654584,
        "last_token":"$2a$10$qFTq5JqGUBXayXODsKUSROjjw.TrFYVGtf.EEULCzRWIhMa79ycZS",
        "updated_at":"2017-10-19T15:29:44.204-05:00"
    }
}

但是,我的请求的结果总是我得到 401 Unauthorized

我正在使用第二个 json 对象,其中我将密钥作为客户端参数和令牌属性,我通过 PUT 将其作为我的 access_token 参数放在我的请求中。

我是否使用我设置的参数正确地执行我的请求? O 如何获取客户端和访问令牌参数?

【问题讨论】:

  • 那会发生什么?你是怎么解决的?
  • Dvex 有没有运气解决它?

标签: ruby-on-rails devise access-token


【解决方案1】:

它没有完全回答有关 401 Unauthorized 错误的问题,但这是我根据the documentation page 所做的。将config.check_current_password_before_update 设置为:password,这意味着如果用户想要更新他的密码,他也需要提供他当前的密码。

检查您的rails routes 以获取正确的路径。我的是:PUT /auth/password/

然后在您的请求正文中:

{"current_password":"old_password","password":"new_password","password_confirmation":"new_password"}

关于 401 Unauthorized 错误,as suggested here,可能是因为你的用户认证。

【讨论】:

    【解决方案2】:

    也许这可以帮助你,文档包括following infos

    帐户更新。此路由将更新现有用户的帐户设置。默认接受的参数是密码和密码确认,但这可以使用 devise_parameter_sanitizer 系统进行自定义。如果 config.check_current_password_before_update 设置为 :attributes,则在任何更新之前检查 current_password 参数,如果设置为 :password,则仅在请求更新用户密码时检查 current_password 参数。

    所以我想知道,您是否必须在 edit action 的设计 strong params 声明中包含此内容?

    但仍然没有解释它返回 401 Unauthorized

    401 Unauthorized (RFC 7235) Similar to 403 Forbidden, but specifically for use when authentication is required and has failed or has not yet been provided. The response must include a WWW-Authenticate header field containing a challenge applicable to the requested resource. See Basic access authentication and Digest access authentication.[33] 401 semantically means "unauthenticated",[34] i.e. the user does not have the necessary credentials. Note: Some sites issue HTTP 401 when an IP address is banned from the website (usually the website domain) and that specific address is refused permission to access a website.

    【讨论】:

      猜你喜欢
      • 2017-04-16
      • 2020-05-26
      • 2018-03-17
      • 2018-01-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-09
      • 1970-01-01
      相关资源
      最近更新 更多