0

How can I reset my password using Magento2 Rest API

I used the below API to get password link but it doesnot send reset token to used in password reset api

URL: store_url/rest/V1/customers/password

Now in Password Reset Api we need to pass a param resetToken from where i can get this param i didn't get it from my email sent in the above API

URL: store_url/rest/V1/customers/resetPassword

{
  "email": "[email protected]",
  **"resetToken": " how to get this "**,
  "newPassword":"test@123"
}

Reference : Meetanshi

2 Answers 2

0

You can reset a customer's password in Magento 2 using the REST API by making a POST request to the V1/customers/resetPassword endpoint. Here's an example of how you can use the REST API to reset a customer's password:

First, you need to get an access token by making a POST request to the V1/integration/customer/token endpoint. You'll need to provide the customer's email and password in the request body.

POST /V1/integration/customer/token
Content-Type: application/json

{
    "username": "[email protected]",
    "password": "password"
}

Once you have the access token, you can use it to make a POST request to the V1/customers/resetPassword endpoint. You'll need to provide the customer's email and the new password in the request body.

POST /V1/customers/resetPassword
Content-Type: application/json
Authorization: Bearer YOUR_ACCESS_TOKEN

{
    "email": "[email protected]",
    "newPassword": "newpassword"
}

If the request is successful, the customer's password will be reset to the new password provided in the request body.

UPDATED ANSWER

Step 1: Send a reset token to customer mail by using below API. By default magento send a reset link password link in mail. For that customer will redirected to web view to reset password. Although little modification of email template we can send the reset token through mail as shwn below.

Api details:

url : http://example.com/rest/V1/customers/password    
Http method : PUT
Http header  : Content-Type application/json
Json Body : {"email":"[email protected]","template":"email_reset"} // email should be a valid email address as token will be sent there.

Email template modification [YourPackage/YourTheme/Magento_Customer/email/password_reset_confirmation.html]

<!--
    /**
    * Copyright © Magento, Inc. All rights reserved.
    * See COPYING.txt for license details.
    */
    -->
    <!--@subject {{trans "Password Reset Confirmation for %name" name=$customer.name}} @-->
    <!--@vars {
    "var customer.name":"Customer Name",
    "var this.getUrl($store, 'customer/account/createPassword/', [_query:[id:$customer.id, token:$customer.rp_token]])":"Reset Password URL"
    } @-->
    {{template config_path="design/email/header_template"}}

    <p class="greeting" style="font-weight: bold;
    font-size: 25px;
    margin-bottom: 25px;
    margin-top: 5px;
    line-height: 28px;">{{trans "%name," name=$customer.name}}</p>
    <p>{{trans "There was recently a request to change the password for your account."}}</p>
    <p>{{trans "If you requested this change, your password reset token is:"}}</p>

    <table class="button" width="100%" border="0" cellspacing="0" cellpadding="0">
    <tr>
        <td>
            <table class="inner-wrapper" border="0" cellspacing="0" cellpadding="0" align="center">
                <tr>
                    <td align="center">
                        {{trans "%token" token=$customer.rp_token}}
                    </td>
                </tr>
            </table>
        </td>
    </tr>
    </table>

    <p>{{trans "If you did not make this request, you can ignore this email and your password will remain the same."}}</p>

    {{template config_path="design/email/footer_template"}}

Step2: By the use of reset token and with help below API:

Api details:

url : http://example.com/rest/V1/customers/resetPassword    
Http method : POST
Http header  : Content-Type application/json
Json Body : {"email":"[email protected]","resetToken":"token received in mail","newPassword":"new password"}

Let me know if any query.

THANKS.

2
  • Thanks for your response, but what if customer forget it password and want to reset it ? because in case of token you need to provide a valid email and password Jan 12 at 7:59
  • I have UPDATED ANSWER. please check let me know if any query. Jan 12 at 11:17
0

You can load the helper \Magento\User\Helper\Data, it's located on vendor/magento/module-user/Helper/Data.php. Then find the function generateResetPasswordLinkToken() on line 50.

Per user request, I added an example to get token using DI

class XXXX{
    protected $_userHelper;

    /* Other DIs come here */

    public function __construct(
        \Magento\User\Helper\Data $user_helper,
        /* Other DIs come here */
    ) {
        $this->_userHelper= $user_helper;
        /* Other DIs come here */
    }

    /* The function name is up to you */
    public function getToken(){
        return $this->_userHelper->generateResetPasswordLinkToken();
    }
    /* other functions */
}

And One another way to get reset password token go to reset password mail then inspect element on Set a New Password and you can get the token on href link.

2
  • Response is " The consumer isn't authorized to access %resources. " , Magento_Customer::manage Jan 19 at 6:53
  • Set the header "Authorization" to "Bearer TOKEN" Jan 19 at 7:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.