The forgotPassword function throws Bad Request 400 errors for 4 possible reasons:
- Invalid email format
- If no user exists with such email in backend
- If the user exists but is marked as blocked in backend
- If it's unable to send a mail to the user via the email service
Source Code
async forgotPassword(ctx) {
let { email } = ctx.request.body;
// Check if the provided email is valid or not.
const isEmail = emailRegExp.test(email);
if (isEmail) {
email = email.toLowerCase();
} else {
return ctx.badRequest(
null,
formatError({
id: 'Auth.form.error.email.format',
message: 'Please provide a valid email address.',
})
);
}
const pluginStore = await strapi.store({
environment: '',
type: 'plugin',
name: 'users-permissions',
});
// Find the user by email.
const user = await strapi
.query('user', 'users-permissions')
.findOne({ email: email.toLowerCase() });
// User not found.
if (!user) {
return ctx.badRequest(
null,
formatError({
id: 'Auth.form.error.user.not-exist',
message: 'This email does not exist.',
})
);
}
// User blocked
if (user.blocked) {
return ctx.badRequest(
null,
formatError({
id: 'Auth.form.error.user.blocked',
message: 'This user is disabled.',
})
);
}
// Generate random token.
const resetPasswordToken = crypto.randomBytes(64).toString('hex');
const settings = await pluginStore.get({ key: 'email' }).then(storeEmail => {
try {
return storeEmail['reset_password'].options;
} catch (error) {
return {};
}
});
const advanced = await pluginStore.get({
key: 'advanced',
});
const userInfo = sanitizeEntity(user, {
model: strapi.query('user', 'users-permissions').model,
});
settings.message = await strapi.plugins['users-permissions'].services.userspermissions.template(
settings.message,
{
URL: advanced.email_reset_password,
USER: userInfo,
TOKEN: resetPasswordToken,
}
);
settings.object = await strapi.plugins['users-permissions'].services.userspermissions.template(
settings.object,
{
USER: userInfo,
}
);
try {
// Send an email to the user.
await strapi.plugins['email'].services.email.send({
to: user.email,
from:
settings.from.email || settings.from.name
? `${settings.from.name} <${settings.from.email}>`
: undefined,
replyTo: settings.response_email,
subject: settings.object,
text: settings.message,
html: settings.message,
});
} catch (err) {
return ctx.badRequest(null, err);
}
// Update the user.
await strapi.query('user', 'users-permissions').update({ id: user.id }, { resetPasswordToken });
ctx.send({ ok: true });
},
You can check the entire source code here to understand even better. Additionally, I would suggest you to check your terminal or browser response to get more information on the error response. The response will usually contain the message key which will tell you why exactly this request failed.