fix: added variables to the constants file.
fix: updated sql queries with constants
This commit is contained in:
parent
3c2cbac23f
commit
aa7252dfce
5 changed files with 35 additions and 16 deletions
11
lib/users.js
11
lib/users.js
|
|
@ -4,6 +4,7 @@ const crypto = require('crypto')
|
|||
const argon2 = require('argon2')
|
||||
const uuid = require('uuid')
|
||||
|
||||
const constants = require('./constants')
|
||||
const db = require('./db')
|
||||
|
||||
/**
|
||||
|
|
@ -97,13 +98,6 @@ function reset2FASecret (token, id, secret) {
|
|||
})
|
||||
}
|
||||
|
||||
function createAuthToken (userID, type) {
|
||||
const token = crypto.randomBytes(32).toString('hex')
|
||||
const sql = `INSERT INTO auth_tokens (token, type, user_id) VALUES ($1, $2, $3) ON CONFLICT (user_id, type) DO UPDATE SET token=$1, expire=now() + interval '30 minutes' RETURNING *`
|
||||
|
||||
return db.one(sql, [token, type, userID])
|
||||
}
|
||||
|
||||
function updatePassword (token, id, password) {
|
||||
return validateAuthToken(token, 'reset_password').then(res => {
|
||||
if (!res.success) throw new Error('Failed to verify password reset token')
|
||||
|
|
@ -121,7 +115,7 @@ function updatePassword (token, id, password) {
|
|||
function createUserRegistrationToken (username, role) {
|
||||
const token = crypto.randomBytes(32).toString('hex')
|
||||
const sql = `INSERT INTO user_register_tokens (token, username, role) VALUES ($1, $2, $3) ON CONFLICT (username)
|
||||
DO UPDATE SET token=$1, expire=now() + interval '30 minutes' RETURNING *`
|
||||
DO UPDATE SET token=$1, expire=now() + interval '${constants.REGISTRATION_TOKEN_EXPIRATION_TIME}' RETURNING *`
|
||||
|
||||
return db.one(sql, [token, username, role])
|
||||
}
|
||||
|
|
@ -176,7 +170,6 @@ module.exports = {
|
|||
save2FASecret,
|
||||
reset2FASecret,
|
||||
validateAuthToken,
|
||||
createAuthToken,
|
||||
createUserRegistrationToken,
|
||||
validateUserRegistrationToken,
|
||||
register,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue