"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = googleLogin; const google_auth_library_1 = require("google-auth-library"); const serverError_1 = __importDefault(require("../../backend/serverError")); const DB_HANDLER_1 = __importDefault(require("../../../utils/backend/global-db/DB_HANDLER")); const hashPassword_1 = __importDefault(require("../../dsql/hashPassword")); /** * # Google Login */ function googleLogin(_a) { return __awaiter(this, arguments, void 0, function* ({ usertype, foundUser, isSocialValidated, isUserValid, reqBody, serverRes, loginFailureReason, }) { var _b; const client = new google_auth_library_1.OAuth2Client(process.env.NEXT_PUBLIC_DSQL_GOOGLE_CLIENT_ID); let isGoogleAuthValid = false; let newFoundUser = null; //////////////////////////////////////////////// //////////////////////////////////////////////// //////////////////////////////////////////////// try { const ticket = yield client.verifyIdToken({ idToken: reqBody.token, audience: process.env.NEXT_PUBLIC_DSQL_GOOGLE_CLIENT_ID, // Specify the CLIENT_ID of the app that accesses the backend // Or, if multiple clients access the backend: //[CLIENT_ID_1, CLIENT_ID_2, CLIENT_ID_3] }); const payload = ticket.getPayload(); const userid = payload === null || payload === void 0 ? void 0 : payload["sub"]; if (!payload) throw new Error("Google login failed. Credentials invalid"); isUserValid = Boolean(payload.email_verified); if (!isUserValid || !payload || !payload.email_verified) return; serverRes.isUserValid = payload.email_verified; isSocialValidated = payload.email_verified; isGoogleAuthValid = payload.email_verified; ////// If request specified a G Suite domain: ////// const domain = payload['hd']; let socialHashedPassword = (0, hashPassword_1.default)({ password: payload.at_hash || "", }); //////////////////////////////////////////////// //////////////////////////////////////////////// //////////////////////////////////////////////// let existinEmail = yield (0, DB_HANDLER_1.default)(`SELECT * FROM ${usertype} WHERE email='${payload.email}' AND social_login!='1' AND social_platform!='google'`); if (existinEmail && existinEmail[0]) { loginFailureReason = "Email Exists Already"; isGoogleAuthValid = false; return { isGoogleAuthValid: isGoogleAuthValid, newFoundUser: newFoundUser, loginFailureReason: loginFailureReason, }; } //////////////////////////////////////// foundUser = yield (0, DB_HANDLER_1.default)(`SELECT * FROM ${usertype} WHERE email='${payload.email}' AND social_login='1' AND social_platform='google'`); if (foundUser && foundUser[0]) { newFoundUser = foundUser; return { isGoogleAuthValid: isGoogleAuthValid, newFoundUser: newFoundUser, }; } //////////////////////////////////////////////// //////////////////////////////////////////////// //////////////////////////////////////////////// let newUser = yield (0, DB_HANDLER_1.default)(`INSERT INTO ${usertype} ( first_name, last_name, social_platform, social_name, social_id, email, image, image_thumbnail, password, verification_status, social_login, terms_agreement, date_created, date_code ) VALUES ( '${payload.given_name}', '${payload.family_name}', 'google', 'google_${(_b = payload.email) === null || _b === void 0 ? void 0 : _b.replace(/@.*/, "")}', '${payload.sub}', '${payload.email}', '${payload.picture}', '${payload.picture}', '${socialHashedPassword}', '1', '1', '1', '${Date()}', '${Date.now()}' )`); newFoundUser = yield (0, DB_HANDLER_1.default)(`SELECT * FROM ${usertype} WHERE id='${newUser.insertId}'`); } catch (error) { (0, serverError_1.default)({ component: "googleLogin", message: error.message, }); loginFailureReason = error; isUserValid = false; isSocialValidated = false; } return { isGoogleAuthValid: isGoogleAuthValid, newFoundUser: newFoundUser }; }); }