如上所述,devise_token_auth 具有三个用于重置密码的 API 调用。
1。发送密码重置电子邮件的 POST 调用
POST /auth/password
Params: 'email', 'redirect_url'
例如:
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 应用程序中的某个位置,请在 redirect_url 定义中使用该应用程序方案的 URL。例如。在 iOS 上手动执行此操作:
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": "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 和password_confirmation 表单的表单。如果密码重置电子邮件链接指向移动应用程序,则由该应用程序创建密码重置表单。
在此步骤中最重要的是知道发出请求的客户端将从 Rails 应用程序返回 Access-Token HEADER。
需要保存此访问令牌,因为客户端将在下一次请求中使用它来保持用户身份验证,同时用户更改密码。
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: foo@gmail.com' -H 'client: U9FIDbiDbYVulsi1dBpxOQ' -H 'access-token: JbGQi97FTAwsW4n6SZ9aYQ' -H 'Accept: application/json' -X PUT https://myapp.com/auth/password -d '{"password": "foobar", "password_confirmation": "foobar"}'