2024-03-26 07:19:01 +00:00
|
|
|
const models = require('vn-loopback/server/server').models;
|
2024-03-26 07:26:44 +00:00
|
|
|
const maxTTL = ((60 * 1000) * 5); // 5 min
|
2024-03-26 07:19:01 +00:00
|
|
|
|
|
|
|
module.exports = authCode = async(vnUser, options) => {
|
|
|
|
const myOptions = {};
|
|
|
|
|
|
|
|
if (typeof options == 'object')
|
|
|
|
Object.assign(myOptions, options);
|
|
|
|
|
|
|
|
const code = String(Math.floor(Math.random() * 999999));
|
|
|
|
await models.AuthCode.upsertWithWhere({userFk: vnUser.id}, {
|
|
|
|
userFk: vnUser.id,
|
2024-03-26 07:26:44 +00:00
|
|
|
code,
|
2024-03-26 07:19:01 +00:00
|
|
|
expires: Date.vnNow() + maxTTL
|
|
|
|
}, myOptions);
|
|
|
|
return code;
|
|
|
|
};
|