如上所述,devise_token_auth 具有三个 API 调用来重置密码。
1. 用于发送密码重置电子邮件的 POST 调用
POST /auth/password
Params: 'email', 'redirect_url'
E.g.:
curl -v -H 'Content-Type: application/json' -H 'Accept: application/json' -X POST https://myapp.com/auth/password -d '{"email":"foo@gmail.com", "redirect_url": "https://myapp.com/auth/sign_in"}'
请注意,redirect_url
给定的必须对应于您希望用户确认和重置密码的端点。
例如。如果想要重定向到 iOS 应用程序中的某个位置,请使用该应用程序方案中的 URLredirect_url
定义。例如。在 iOS 上手动执行此操作:
curl -v -H 'Content-Type: application/json' -H 'Accept: application/json' -X POST https://myapp.com/auth/password -d '{"email":"[email protected] /cdn-cgi/l/email-protection", "redirect_url": "myappStoreAppName://auth/password/edit"}'
2. 用于验证密码重置令牌的 GET 调用(在电子邮件中单击)
GET /auth/password/edit
Params: 'password_reset_token', 'redirect_url'
E.g. via our iOS app would produce an email link like this: https://myapp.com/auth/password/edit?config=default&redirect_url=myappStoreName%3A%2F%2Fauth%2Fpassword%2Fedit&reset_password_token=Qv6mkLuoy9zN-Y1pKghB
如果这是来自网络应用程序,则“redirect_to”链接应指向一个表单,其中password
and password_confirmation
可以填写表格。如果密码重置电子邮件链接指向移动应用程序,则由该应用程序创建密码重置表单。
此步骤中最重要的是知道发出请求的客户端将返回一个Access-Token
来自 Rails 应用程序的标头。
需要保存此访问令牌,因为客户端将在下一个请求中使用它来在用户更改密码时保持用户身份验证。
3. 更新用户密码的 PUT 调用
PUT /auth/password
Head: 'uid: VALUE', 'client: VALUE', 'access-token: VALUE', 'token-type: Bearer'
Params: 'password', 'password_confirmation'
请注意需要为此 PUT 调用提供的 HEAD 值。这些确保我们(现在已通过身份验证的用户)有权执行密码更改,并确保我们的用户即使在更改密码后也可以继续保持身份验证。
例如。通过卷曲:
curl -v -H 'Content-Type: application/json' -H 'uid: [email protected] /cdn-cgi/l/email-protection' -H 'client: U9FIDbiDbYVulsi1dBpxOQ' -H 'access-token: JbGQi97FTAwsW4n6SZ9aYQ' -H 'Accept: application/json' -X PUT https://myapp.com/auth/password -d '{"password": "foobar", "password_confirmation": "foobar"}'