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": "customer@example.com",
"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": "customer@example.com",
"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":"test@gmail.com","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":"test@gmail.com","resetToken":"token received in mail","newPassword":"new password"}
Let me know if any query.
THANKS.