65 lines
2.5 KiB
JavaScript
65 lines
2.5 KiB
JavaScript
import addAdminUserOnLogin from "../../backend/addAdminUserOnLogin";
|
|
import varDatabaseDbHandler from "../../backend/varDatabaseDbHandler";
|
|
/**
|
|
* Function to login social user
|
|
* ==============================================================================
|
|
* @description This function logs in the user after 'handleSocialDb' function finishes
|
|
* the user creation or confirmation process
|
|
*/
|
|
export default async function loginSocialUser({ user, social_platform, invitation, database, additionalFields, debug, }) {
|
|
const finalDbName = database ? database : "datasquirel";
|
|
const dbAppend = database ? `\`${finalDbName}\`.` : "";
|
|
const foundUserQuery = `SELECT * FROM ${dbAppend}\`users\` WHERE email=?`;
|
|
const foundUserValues = [user.email];
|
|
const foundUser = await varDatabaseDbHandler({
|
|
database: finalDbName,
|
|
queryString: foundUserQuery,
|
|
queryValuesArray: foundUserValues,
|
|
debug,
|
|
});
|
|
if (!(foundUser === null || foundUser === void 0 ? void 0 : foundUser[0]))
|
|
return {
|
|
success: false,
|
|
payload: null,
|
|
msg: "Couldn't find Social User.",
|
|
};
|
|
let csrfKey = Math.random().toString(36).substring(2) +
|
|
"-" +
|
|
Math.random().toString(36).substring(2);
|
|
let userPayload = {
|
|
id: foundUser[0].id,
|
|
uuid: foundUser[0].uuid,
|
|
first_name: foundUser[0].first_name,
|
|
last_name: foundUser[0].last_name,
|
|
username: foundUser[0].username,
|
|
user_type: foundUser[0].user_type,
|
|
email: foundUser[0].email,
|
|
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,
|
|
logged_in_status: true,
|
|
date: Date.now(),
|
|
};
|
|
if (additionalFields === null || additionalFields === void 0 ? void 0 : additionalFields[0]) {
|
|
additionalFields.forEach((key) => {
|
|
userPayload[key] = foundUser[0][key];
|
|
});
|
|
}
|
|
if (invitation && (!database || (database === null || database === void 0 ? void 0 : database.match(/^datasquirel$/)))) {
|
|
addAdminUserOnLogin({
|
|
query: invitation,
|
|
user: userPayload,
|
|
});
|
|
}
|
|
let result = {
|
|
success: true,
|
|
payload: userPayload,
|
|
csrf: csrfKey,
|
|
};
|
|
return result;
|
|
}
|