59 lines
2.0 KiB
JavaScript
59 lines
2.0 KiB
JavaScript
import varDatabaseDbHandler from "../../backend/varDatabaseDbHandler";
|
|
/**
|
|
* # Re-authenticate API user
|
|
*/
|
|
export default async function apiReauthUser({ existingUser, database, additionalFields, }) {
|
|
const dbAppend = global.DSQL_USE_LOCAL
|
|
? ""
|
|
: database
|
|
? `${database}.`
|
|
: "";
|
|
let foundUser = (existingUser === null || existingUser === void 0 ? void 0 : existingUser.id) && existingUser.id.toString().match(/./)
|
|
? await varDatabaseDbHandler({
|
|
queryString: `SELECT * FROM ${dbAppend}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);
|
|
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];
|
|
});
|
|
}
|
|
return {
|
|
success: true,
|
|
msg: "Login Successful",
|
|
payload: userPayload,
|
|
csrf: csrfKey,
|
|
};
|
|
}
|