46 lines
1.2 KiB
JavaScript
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: 'directory',
|
|
type: 'string',
|
|
description: 'The directory for mail'
|
|
}
|
|
],
|
|
http: {
|
|
path: `/recoverPassword`,
|
|
verb: 'POST'
|
|
}
|
|
});
|
|
|
|
Self.recoverPassword = async function(user, directory) {
|
|
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', directory});
|
|
} catch (err) {
|
|
if (err.code === 'EMAIL_NOT_FOUND')
|
|
return;
|
|
else
|
|
throw err;
|
|
}
|
|
};
|
|
};
|