2024-02-14 07:08:47 +00:00
|
|
|
|
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: [
|
|
|
|
{
|
2023-02-01 14:13:38 +00:00
|
|
|
arg: 'user',
|
2022-09-20 13:21:01 +00:00
|
|
|
type: 'string',
|
2023-02-01 14:13:38 +00:00
|
|
|
description: 'The user name or email',
|
2022-09-21 13:15:19 +00:00
|
|
|
required: true
|
2023-07-17 12:24:50 +00:00
|
|
|
},
|
|
|
|
{
|
2023-08-04 12:33:38 +00:00
|
|
|
arg: 'app',
|
2023-07-17 12:24:50 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2023-08-04 12:33:38 +00:00
|
|
|
Self.recoverPassword = async function(user, app) {
|
2024-02-14 07:08:47 +00:00
|
|
|
const usesPhone = new RegExp(/([+]\d{2})?\d{9}/, 'g').test(user);
|
|
|
|
const account = await Self.app.models.Application.rawSql(
|
|
|
|
`SELECT c.id, c.phone, u.email from account.user u, vn.client c
|
|
|
|
where c.id=u.id and( u.email = ? or u.name = ? or c.phone = ?)`,
|
|
|
|
[user, user, user]);
|
2024-02-14 07:09:51 +00:00
|
|
|
|
2024-02-14 07:08:47 +00:00
|
|
|
if (!account || account.length > 1) return;
|
|
|
|
const {email, phone} = account[0];
|
2024-02-14 07:09:51 +00:00
|
|
|
|
2022-11-09 13:51:30 +00:00
|
|
|
try {
|
2024-02-14 07:08:47 +00:00
|
|
|
await Self.resetPassword({email, phone, emailTemplate: 'recover-password', app, usesPhone});
|
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
|
|
|
};
|
|
|
|
};
|