简体   繁体   English

raails devise_token_auth gem,如何设置密码重置链接?

[英]Rails devise_token_auth gem, how do I set password reset link?

I have a problem for using password reset function of this gem. 我有使用此gem的密码重置功能的问题。 https://github.com/lynndylanhurley/devise_token_auth https://github.com/lynndylanhurley/devise_token_auth

This is from the document. 这来自文件。

/password/edit     GET  

"Verify user by password reset token. This route is the destination URL for password reset confirmation. This route must contain reset_password_token and redirect_url params. These values will be set automatically by the confirmation email that is generated by the password reset request." “通过密码重置令牌验证用户。此路由是密码重置确认的目标URL。此路由必须包含reset_password_token和redirect_url参数。这些值将由密码重置请求生成的确认电子邮件自动设置。”

When users forget theirs passwords, they can enter their registered email, and they will receive a password reset link via email. 当用户忘记他们的密码时,他们可以输入他们的注册电子邮件,他们将通过电子邮件收到密码重置链接。

POST : https://example.com/api/auth/password POST: https//example.com/api/auth/password

params = email and redirect link ( https://example.com/api/auth/password/edit ) params =电子邮件和重定向链接( https://example.com/api/auth/password/edit

I can send a password reset link via email, but when I click the link or "Change my password" in the email, it jump to the redirect address with token. 我可以通过电子邮件发送密码重置链接,但当我点击电子邮件中的链接或“更改我的密码”时,它会跳转到带有令牌的重定向地址。

And it shows the "The page you were looking for doesn't exist." 它显示“您正在寻找的页面不存在。”

This might be routes errror or something, but I don't know. 这可能是路由错误或其他什么,但我不知道。 I am not even sure if I should set "/password/edit" for redirect link. 我甚至不确定是否应该为重定向链接设置“/ password / edit”。

This is the related link for the github https://github.com/lynndylanhurley/devise_token_auth/issues/604 这是github的相关链接https://github.com/lynndylanhurley/devise_token_auth/issues/604

Am I missing something, or should I set a different address for redirect link part? 我错过了什么,或者我应该为重定向链接部分设置不同的地址?

The flow of the devise_token_auth reset password feature is that, it has three API's devise_token_auth重置密码功能的流程是,它有三个API

  1. A post call for sending reset password token, 发送重置密码令牌的帖子调用,
  2. A get request for getting authentication headers 获取身份验证标头的get请求
  3. A patch call for changing the password 用于更改密码的补丁调用

In post you will send the email and redirect url, this will call create method in the DeviseTokenAuth::PasswordsController, which creates a reset password token and sends it in the email. 在帖子中,您将发送电子邮件和重定向网址,这将在DeviseTokenAuth :: PasswordsController中调用create方法,该方法创建重置密码令牌并将其发送到电子邮件中。

The link in the email will call to the edit method of DeviseTokenAuth::PasswordsController, where it generates the authentication header and redirects to the redirect url which u have sent in the previous request with these authentication headers as query string(url parameters) 电子邮件中的链接将调用DeviseTokenAuth :: PasswordsController的编辑方法,在该方法中它生成身份验证标头并重定向到您在上一个请求中发送的重定向URL,并将这些身份验证标头作为查询字符串(url参数)

Use these authentication headers to patch request to the update method in the DeviseTokenAuth::PasswordsController, with password and password_confirmation as attributes. 使用这些身份验证标头将请求修补到DeviseTokenAuth :: PasswordsController中的update方法,并使用password和password_confirmation作为属性。

the password will be changed. 密码将被更改。

As noted above, devise_token_auth has three API's calls to make for resetting a password. 如上所述,devise_token_auth有三个API调用来重置密码。

1. A POST call to send a Passsword Reset Email 1.发送密码重置电子邮件的POST呼叫

 POST /auth/password
 Params: 'email', 'redirect_url'

Eg: 例如:

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"}'

Note that the redirect_url given must correspond to the endpoint you want the user taken to for confirming and resetting their password. 请注意,给定的redirect_url必须与您希望用户用于确认和重置其密码的端点相对应。

Eg if wanting redirect to somewhere within an iOS app, use the URL for that app scheme in the redirect_url definition. 例如,如果想要重定向到iOS应用程序中的某个位置,请在redirect_url定义中使用该应用程序方案的URL。 Eg to manually do this on iOS: 例如,在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. A GET call to verify the password reset token (clicked in email) 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

If this is from a web app, the 'redirect_to' link should point to a form where a password and password_confirmation form can be filled out. 如果这是来自Web应用程序,则“redirect_to”链接应指向可填写passwordpassword_confirmation表单的表单。 If the password reset email link points to a mobile app, it's up to that app to create the password reset form. 如果密码重置电子邮件链接指向移动应用程序,则由该应用程序创建密码重置表单。

Most important in this step is knowing that the client making the request will get back an Access-Token HEADER from the Rails app. 在这一步中最重要的是知道发出请求的客户端将从Rails应用程序返回一个Access-Token HEADER。

This Access-Token needs to be saved, because it's what the client will use in the next request to keep the user authenticated while the user changes their password. 需要保存此访问令牌,因为它是客户端在下一个请求中将用户在用户更改密码时保持用户身份验证的内容。

3. A PUT call to update the user's password 3. PUT调用以更新用户的密码

PUT /auth/password
Head: 'uid: VALUE', 'client: VALUE', 'access-token: VALUE', 'token-type: Bearer'
Params: 'password', 'password_confirmation'

Note the HEAD values that need to be supplied for this PUT call. 请注意需要为此PUT调用提供的HEAD值。 These ensure our (now authenticated user) has permission to execute a change of password, and ensure that our user can continue to remain authenticated even after changing their password. 这些确保我们(现已通过身份验证的用户)有权执行密码更改,并确保即使在更改密码后我们的用户仍可继续进行身份验证。

Eg via curl: 例如通过卷曲:

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"}'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM