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

46 lines
1.2 KiB
JavaScript

module.exports = Self => {
Self.remoteMethod('recoverPassword', {
description: 'Send email to the user',
accepts: [
{
arg: 'user',
type: 'string',
description: 'The user name or email',
required: true
},
{
arg: 'app',
type: 'string',
description: 'The directory for mail'
}
],
http: {
path: `/recoverPassword`,
verb: 'POST'
}
});
Self.recoverPassword = async function(user, app) {
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;
}
try {
await Self.resetPassword({email: user, emailTemplate: 'recover-password', app});
} catch (err) {
if (err.code === 'EMAIL_NOT_FOUND')
return;
else
throw err;
}
};
};