2024-12-06 13:24:26 +00:00
|
|
|
// @ts-check
|
|
|
|
|
|
|
|
const LOCAL_DB_HANDLER = require("../../../utils/backend/global-db/LOCAL_DB_HANDLER");
|
|
|
|
const varDatabaseDbHandler = require("../../backend/varDatabaseDbHandler");
|
|
|
|
const nodemailer = require("nodemailer");
|
|
|
|
|
|
|
|
/**
|
|
|
|
* # Re-authenticate API user
|
|
|
|
* @param {object} param
|
|
|
|
* @param {Object<string, any>} param.existingUser
|
|
|
|
* @param {string} param.database
|
|
|
|
* @param {string[]} [param.additionalFields]
|
|
|
|
* @param {boolean} [param.useLocal]
|
|
|
|
*
|
2024-12-08 08:57:48 +00:00
|
|
|
* @returns {Promise<import("../../../types").APILoginFunctionReturn>}
|
2024-12-06 13:24:26 +00:00
|
|
|
*/
|
|
|
|
module.exports = async function apiReauthUser({
|
|
|
|
existingUser,
|
|
|
|
database,
|
|
|
|
additionalFields,
|
|
|
|
useLocal,
|
|
|
|
}) {
|
|
|
|
let foundUser =
|
|
|
|
existingUser?.id && existingUser.id.toString().match(/./)
|
|
|
|
? useLocal
|
|
|
|
? await LOCAL_DB_HANDLER(`SELECT * FROM users WHERE id=?`, [
|
|
|
|
existingUser.id.toString(),
|
|
|
|
])
|
|
|
|
: await varDatabaseDbHandler({
|
|
|
|
queryString: `SELECT * FROM users WHERE id=?`,
|
|
|
|
queryValuesArray: [existingUser.id.toString()],
|
|
|
|
database,
|
|
|
|
})
|
|
|
|
: null;
|
|
|
|
|
|
|
|
////////////////////////////////////////
|
|
|
|
////////////////////////////////////////
|
|
|
|
////////////////////////////////////////
|
|
|
|
|
|
|
|
if (!foundUser || !foundUser[0])
|
|
|
|
return {
|
|
|
|
success: false,
|
|
|
|
payload: null,
|
|
|
|
msg: "No user found",
|
|
|
|
};
|
|
|
|
|
|
|
|
////////////////////////////////////////
|
|
|
|
////////////////////////////////////////
|
|
|
|
////////////////////////////////////////
|
|
|
|
|
|
|
|
let csrfKey =
|
|
|
|
Math.random().toString(36).substring(2) +
|
|
|
|
"-" +
|
|
|
|
Math.random().toString(36).substring(2);
|
|
|
|
|
2024-12-08 08:57:48 +00:00
|
|
|
/** @type {import("../../../types").DATASQUIREL_LoggedInUser} */
|
2024-12-06 13:24:26 +00:00
|
|
|
let userPayload = {
|
|
|
|
id: foundUser[0].id,
|
|
|
|
first_name: foundUser[0].first_name,
|
|
|
|
last_name: foundUser[0].last_name,
|
|
|
|
username: foundUser[0].username,
|
|
|
|
email: foundUser[0].email,
|
|
|
|
phone: foundUser[0].phone,
|
|
|
|
social_id: foundUser[0].social_id,
|
|
|
|
image: foundUser[0].image,
|
|
|
|
image_thumbnail: foundUser[0].image_thumbnail,
|
|
|
|
verification_status: foundUser[0].verification_status,
|
|
|
|
social_login: foundUser[0].social_login,
|
|
|
|
social_platform: foundUser[0].social_platform,
|
|
|
|
csrf_k: csrfKey,
|
|
|
|
more_data: foundUser[0].more_user_data,
|
|
|
|
logged_in_status: true,
|
|
|
|
date: Date.now(),
|
|
|
|
};
|
|
|
|
|
|
|
|
if (
|
|
|
|
additionalFields &&
|
|
|
|
Array.isArray(additionalFields) &&
|
|
|
|
additionalFields.length > 0
|
|
|
|
) {
|
|
|
|
additionalFields.forEach((key) => {
|
|
|
|
userPayload[key] = foundUser[0][key];
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////
|
|
|
|
////////////////////////////////////////
|
|
|
|
////////////////////////////////////////
|
|
|
|
|
|
|
|
/** ********************* Send Response */
|
|
|
|
return {
|
|
|
|
success: true,
|
|
|
|
msg: "Login Successful",
|
|
|
|
payload: userPayload,
|
2024-12-08 08:57:48 +00:00
|
|
|
csrf: csrfKey,
|
2024-12-06 13:24:26 +00:00
|
|
|
};
|
|
|
|
};
|