2024-05-23 14:45:58 +00:00
|
|
|
const isProduction = require('vn-loopback/server/boot/isProduction');
|
2024-03-20 12:49:49 +00:00
|
|
|
const UserError = require('vn-loopback/util/user-error');
|
2024-03-26 07:18:20 +00:00
|
|
|
const authCode = require('../../models/authCode');
|
2024-03-20 12:49:49 +00:00
|
|
|
module.exports = Self => {
|
|
|
|
Self.remoteMethod('recoverPasswordSMS', {
|
|
|
|
description: 'Send SMS to the user',
|
|
|
|
accepts: [
|
2024-04-27 19:28:01 +00:00
|
|
|
|
2024-03-20 12:49:49 +00:00
|
|
|
{
|
2024-05-24 09:11:22 +00:00
|
|
|
arg: 'user',
|
2024-03-20 12:49:49 +00:00
|
|
|
type: 'string',
|
2024-04-27 19:28:01 +00:00
|
|
|
description: 'The recoveryPhone user\'s',
|
2024-03-20 12:49:49 +00:00
|
|
|
required: true
|
|
|
|
},
|
|
|
|
{
|
2024-05-23 12:33:33 +00:00
|
|
|
arg: 'verificationCode',
|
2024-03-20 12:49:49 +00:00
|
|
|
type: 'string',
|
2024-03-26 07:28:57 +00:00
|
|
|
description: 'Code tovalidate operation'
|
2024-03-20 12:49:49 +00:00
|
|
|
}
|
|
|
|
],
|
|
|
|
returns: {
|
|
|
|
type: 'Object',
|
|
|
|
root: true
|
|
|
|
},
|
|
|
|
http: {
|
|
|
|
path: `/recoverPasswordSMS`,
|
|
|
|
verb: 'POST'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2024-05-24 09:11:22 +00:00
|
|
|
Self.recoverPasswordSMS = async function(user, verificationCode, options) {
|
|
|
|
const models = Self.app.models;
|
2024-03-26 07:18:20 +00:00
|
|
|
const myOptions = {};
|
|
|
|
|
|
|
|
if (typeof options == 'object')
|
|
|
|
Object.assign(myOptions, options);
|
|
|
|
|
2024-05-24 09:11:22 +00:00
|
|
|
const usesEmail = user.indexOf('@') !== -1;
|
|
|
|
const filter = usesEmail ? {email: user} : {name: user};
|
|
|
|
// if (!usesEmail) {
|
|
|
|
const account = await models.VnUser.findOne({
|
|
|
|
fields: ['id', 'name', 'recoveryPhone'],
|
|
|
|
where: filter
|
2024-04-22 05:57:23 +00:00
|
|
|
});
|
2024-05-24 09:11:22 +00:00
|
|
|
if (!account) return;
|
|
|
|
user = account;
|
|
|
|
// }
|
2024-03-20 12:49:49 +00:00
|
|
|
|
2024-05-24 09:11:22 +00:00
|
|
|
if (verificationCode) {
|
|
|
|
await Self.validateCode(user.name, verificationCode);
|
2024-03-26 07:18:20 +00:00
|
|
|
|
2024-05-24 09:11:22 +00:00
|
|
|
return {
|
|
|
|
token: await user.accessTokens.create({})
|
|
|
|
};
|
|
|
|
}
|
2024-03-20 13:11:05 +00:00
|
|
|
|
2024-05-24 09:11:22 +00:00
|
|
|
const code = await authCode(user, myOptions);
|
2024-03-26 07:18:20 +00:00
|
|
|
|
2024-05-24 09:11:22 +00:00
|
|
|
if (isProduction(true))
|
|
|
|
return {code};
|
|
|
|
await Self.app.models.Sms.send({req: {accessToken: {user: user.id}}}, +user.recoveryPhone, code);
|
2024-03-20 12:49:49 +00:00
|
|
|
};
|
|
|
|
};
|