salix/back/methods/account/recover-password.js

51 lines
1.2 KiB
JavaScript
Raw Normal View History

2022-10-10 13:16:56 +00:00
const {Email} = require('vn-print');
2022-09-20 13:21:01 +00:00
module.exports = Self => {
2022-09-21 13:15:19 +00:00
Self.remoteMethodCtx('recoverPassword', {
2022-09-20 13:21:01 +00:00
description: 'Send email to the user',
accepts: [
{
arg: 'email',
type: 'string',
description: 'The email of user',
2022-09-21 13:15:19 +00:00
required: true
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
}
});
2022-09-21 13:15:19 +00:00
Self.recoverPassword = async function(ctx, email) {
2022-09-20 13:21:01 +00:00
const models = Self.app.models;
2022-09-21 13:15:19 +00:00
const origin = ctx.req.headers.origin;
const ttl = 1209600;
const user = await models.Account.findOne({
fields: ['id', 'name', 'password'],
2022-09-20 13:21:01 +00:00
where: {
email: email
}
});
if (!user)
2022-09-28 10:20:29 +00:00
return;
2022-09-20 13:21:01 +00:00
2022-09-21 13:15:19 +00:00
const token = await models.AccessToken.create({
ttl: ttl,
userId: user.id
2022-09-20 13:21:01 +00:00
});
2022-10-10 13:16:56 +00:00
const url = `${origin}/#!/account/${user.id}/basic-data?access_token=${token.id}`;
const params = {
2022-10-11 08:06:25 +00:00
recipient: email,
2022-10-10 13:16:56 +00:00
url: url
};
const sendEmail = new Email('recover-password', params);
return sendEmail.send();
2022-09-20 13:21:01 +00:00
};
};