我正在尝试在我的strapi应用程序上实现密码恢复功能。
遵循我调用的文档:
axios.post('http://localhost:1337/auth/forgot-password', {
email: <MY_EMAIL>,
})尽管网络调用被挂起了几分钟,然后以400 Bad Request错误响应。
有没有人经历过类似的事情?
我检查了用户权限,公共角色有权访问forgotpassword。我可以尝试任何其他的配置检查吗?
非常感谢,
我
发布于 2021-10-16 13:10:23
forgotPassword函数抛出Bad Request 400错误的原因可能有4个:
源代码
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 });
},您可以查看整个源代码here,以便更好地理解。此外,我建议您检查终端或浏览器响应,以获取有关错误响应的更多信息。响应通常包含message密钥,它将告诉您此请求失败的确切原因。
https://stackoverflow.com/questions/69587786
复制相似问题