This commit is contained in:
parent
06ed466953
commit
fe3a97cd38
|
@ -1,41 +1,5 @@
|
|||
const UserError = require('vn-loopback/util/user-error');
|
||||
const authCode = require('../../models/authCode');
|
||||
const OTP_CHAR = ':';
|
||||
function original({id, phone}) {
|
||||
const total = parseInt(phone) + parseInt(id);
|
||||
const value = total.toString().slice(-6);
|
||||
|
||||
return parseInt(value); // Devolvemos un número entero, no una cadena
|
||||
}
|
||||
function reverse(params) {
|
||||
const _original = original(params);
|
||||
return parseInt(_original.toString().split('').reverse().join(''));
|
||||
}
|
||||
|
||||
function selectOTPMethod() {
|
||||
const otpIndex = Math.floor(Math.random() * Object.keys(OTP_TYPES).length);
|
||||
return Object.keys(OTP_TYPES)[otpIndex];
|
||||
}
|
||||
function generateOTP(params, _otpType, format = true) {
|
||||
const otpType = _otpType ?? selectOTPMethod();
|
||||
const otp = OTP_TYPES[otpType](params);
|
||||
if (format) return formatOTP({otpType, otp});
|
||||
return {otpType, otp};
|
||||
}
|
||||
|
||||
function formatOTP({otpType, otp}) {
|
||||
return `${otpType}${OTP_CHAR}${otp}`;
|
||||
}
|
||||
|
||||
function checkOTP(params, otp) {
|
||||
const [otpType, value] = otp.split(OTP_CHAR);
|
||||
return generateOTP(params, otpType) === formatOTP(otpType, value);
|
||||
}
|
||||
|
||||
const OTP_TYPES = {
|
||||
// 'A': original,
|
||||
'B': reverse,
|
||||
};
|
||||
module.exports = Self => {
|
||||
Self.remoteMethod('recoverPasswordSMS', {
|
||||
description: 'Send SMS to the user',
|
||||
|
@ -102,7 +66,7 @@ module.exports = Self => {
|
|||
|
||||
const code = await authCode(user, myOptions);
|
||||
|
||||
if (process.env.NODE_ENV != 'production')
|
||||
if (process.env.NODE_ENV === 'production')
|
||||
await Self.app.models.Sms.send({req: {accessToken: {userId: id}}}, +phone, code);
|
||||
return {otp: true};
|
||||
} catch (err) {
|
||||
|
|
Loading…
Reference in New Issue