#6427 - SMS Recover Password #2037

Open
jsegarra wants to merge 72 commits from 6427_sms_resetPassword into dev
2 changed files with 20 additions and 10 deletions
Showing only changes of commit 2ae298cd9e - Show all commits

View File

@ -1,5 +1,6 @@
const ForbiddenError = require('vn-loopback/util/forbiddenError'); const ForbiddenError = require('vn-loopback/util/forbiddenError');
const UserError = require('vn-loopback/util/user-error'); const UserError = require('vn-loopback/util/user-error');
const authCode = require('../../models/authCode');
module.exports = Self => { module.exports = Self => {
Self.remoteMethodCtx('signIn', { Self.remoteMethodCtx('signIn', {
@ -65,16 +66,7 @@ module.exports = Self => {
Self.sendTwoFactor = async(ctx, vnUser, myOptions) => { Self.sendTwoFactor = async(ctx, vnUser, myOptions) => {
if (vnUser.twoFactor === 'email') { if (vnUser.twoFactor === 'email') {
const $ = Self.app.models; const code = await authCode(vnUser, myOptions);
const code = String(Math.floor(Math.random() * 999999));
const maxTTL = ((60 * 1000) * 5); // 5 min
await $.AuthCode.upsertWithWhere({userFk: vnUser.id}, {
userFk: vnUser.id,
code: code,
expires: Date.vnNow() + maxTTL
}, myOptions);
const headers = ctx.req.headers; const headers = ctx.req.headers;
const platform = headers['sec-ch-ua-platform']?.replace(/['"=]+/g, ''); const platform = headers['sec-ch-ua-platform']?.replace(/['"=]+/g, '');
const browser = headers['sec-ch-ua']?.replace(/['"=]+/g, ''); const browser = headers['sec-ch-ua']?.replace(/['"=]+/g, '');

18
back/models/authCode.js Normal file
View File

@ -0,0 +1,18 @@
const models = require('vn-loopback/server/server').models;
module.exports = authCode = async(vnUser, options) => {
const myOptions = {};
if (typeof options == 'object')
Object.assign(myOptions, options);
const code = String(Math.floor(Math.random() * 999999));
console.log(code);
const maxTTL = ((60 * 1000) * 5); // 5 min
await models.AuthCode.upsertWithWhere({userFk: vnUser.id}, {
userFk: vnUser.id,
code: code,
expires: Date.vnNow() + maxTTL
}, myOptions);
return code;
};