salix/back/methods/vn-user/recover-password.js

46 lines
1.2 KiB
JavaScript
Raw Permalink Normal View History

2022-09-20 13:21:01 +00:00
module.exports = Self => {
2022-11-09 13:51:30 +00:00
Self.remoteMethod('recoverPassword', {
2022-09-20 13:21:01 +00:00
description: 'Send email to the user',
accepts: [
{
arg: 'user',
2022-09-20 13:21:01 +00:00
type: 'string',
description: 'The user name or email',
2022-09-21 13:15:19 +00:00
required: true
},
{
arg: 'app',
type: 'string',
description: 'The directory for mail'
2022-09-20 13:21:01 +00:00
}
],
http: {
path: `/recoverPassword`,
2022-09-21 13:15:19 +00:00
verb: 'POST'
2022-09-20 13:21:01 +00:00
}
});
Self.recoverPassword = async function(user, app) {
2022-09-20 13:21:01 +00:00
const models = Self.app.models;
const usesEmail = user.indexOf('@') !== -1;
if (!usesEmail) {
const account = await models.VnUser.findOne({
fields: ['email'],
where: {name: user}
});
if (!account) return;
user = account.email;
}
2022-11-09 13:51:30 +00:00
try {
await Self.resetPassword({email: user, emailTemplate: 'recover-password', app});
2022-11-10 12:32:07 +00:00
} catch (err) {
if (err.code === 'EMAIL_NOT_FOUND')
return;
else
throw err;
2022-11-09 13:51:30 +00:00
}
2022-09-20 13:21:01 +00:00
};
};