refs #6427 feat: move authCode method to file

This commit is contained in:
Javier Segarra 2024-03-26 08:19:01 +01:00
parent f70b663ac0
commit 2ae298cd9e
2 changed files with 20 additions and 10 deletions

View File

@ -1,5 +1,6 @@
const ForbiddenError = require('vn-loopback/util/forbiddenError');
const UserError = require('vn-loopback/util/user-error');
const authCode = require('../../models/authCode');
module.exports = Self => {
Self.remoteMethodCtx('signIn', {
@ -65,16 +66,7 @@ module.exports = Self => {
Self.sendTwoFactor = async(ctx, vnUser, myOptions) => {
if (vnUser.twoFactor === 'email') {
const $ = Self.app.models;
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 code = await authCode(vnUser, myOptions);
const headers = ctx.req.headers;
const platform = headers['sec-ch-ua-platform']?.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;
};