diff --git a/back/methods/vn-user/sign-in.js b/back/methods/vn-user/sign-in.js index 782046641..d4da10929 100644 --- a/back/methods/vn-user/sign-in.js +++ b/back/methods/vn-user/sign-in.js @@ -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, ''); diff --git a/back/models/authCode.js b/back/models/authCode.js new file mode 100644 index 000000000..47dd590f5 --- /dev/null +++ b/back/models/authCode.js @@ -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; +};