"use strict";
(() => {
var exports = {};
exports.id = 862;
exports.ids = [862];
exports.modules = {

/***/ 9538:
/***/ ((module) => {

module.exports = require("datasquirel");

/***/ }),

/***/ 3785:
/***/ ((module) => {

module.exports = require("generate-password");

/***/ }),

/***/ 6781:
/***/ ((module) => {

module.exports = require("google-auth-library");

/***/ }),

/***/ 6517:
/***/ ((module) => {

module.exports = require("lodash");

/***/ }),

/***/ 5184:
/***/ ((module) => {

module.exports = require("nodemailer");

/***/ }),

/***/ 6109:
/***/ ((module) => {

module.exports = require("sanitize-html");

/***/ }),

/***/ 2261:
/***/ ((module) => {

module.exports = require("serverless-mysql");

/***/ }),

/***/ 4300:
/***/ ((module) => {

module.exports = require("buffer");

/***/ }),

/***/ 6113:
/***/ ((module) => {

module.exports = require("crypto");

/***/ }),

/***/ 7147:
/***/ ((module) => {

module.exports = require("fs");

/***/ }),

/***/ 3685:
/***/ ((module) => {

module.exports = require("http");

/***/ }),

/***/ 1017:
/***/ ((module) => {

module.exports = require("path");

/***/ }),

/***/ 3364:
/***/ ((__unused_webpack_module, __webpack_exports__, __webpack_require__) => {

__webpack_require__.r(__webpack_exports__);
/* harmony export */ __webpack_require__.d(__webpack_exports__, {
/* harmony export */   "default": () => (/* binding */ handler)
/* harmony export */ });
/* harmony import */ var _package_shared_functions_backend_decrypt__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(5425);
/* harmony import */ var _package_shared_functions_backend_decrypt__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(_package_shared_functions_backend_decrypt__WEBPACK_IMPORTED_MODULE_0__);
/* harmony import */ var _functions_backend_serverError__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(2163);
/* harmony import */ var _functions_backend_serverError__WEBPACK_IMPORTED_MODULE_1___default = /*#__PURE__*/__webpack_require__.n(_functions_backend_serverError__WEBPACK_IMPORTED_MODULE_1__);
/* harmony import */ var _functions_backend_social_login_handleSocialDb__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(7839);
/* harmony import */ var _functions_backend_social_login_handleSocialDb__WEBPACK_IMPORTED_MODULE_2___default = /*#__PURE__*/__webpack_require__.n(_functions_backend_social_login_handleSocialDb__WEBPACK_IMPORTED_MODULE_2__);
/* harmony import */ var _package_shared_functions_backend_api_cred__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__(1007);
/* harmony import */ var _package_shared_functions_backend_api_cred__WEBPACK_IMPORTED_MODULE_3___default = /*#__PURE__*/__webpack_require__.n(_package_shared_functions_backend_api_cred__WEBPACK_IMPORTED_MODULE_3__);
// @ts-check
/** # MODULE TRACE 
======================================================================
 * No imports found for this Module
==== MODULE TRACE END ==== */ /**
 * ==============================================================================
 * Imports
 * ==============================================================================
 */ const fs = __webpack_require__(7147);
const { OAuth2Client  } = __webpack_require__(6781);




/** ****************************************************************************** */ /** ****************************************************************************** */ /** ****************************************************************************** */ /** ****************************************************************************** */ /** ****************************************************************************** */ /** ****************************************************************************** */ /**
 * API handler
 * ==============================================================================
 * @type {import("next").NextApiHandler}
 */ async function handler(req, res) {
    /**
     * Check method
     *
     * @description Check request method and return if invalid
     */ if (req.method !== "POST") return res.json({
        success: false,
        msg: "Failed!"
    });
    /**
     * Send Response
     *
     * @description Send a boolean response
     */ try {
        const { token , clientId , database , additionalFields  } = req.body;
        const authorization = req.headers.authorization;
        const apiCred = _package_shared_functions_backend_api_cred__WEBPACK_IMPORTED_MODULE_3___default()({
            key: authorization,
            database
        });
        if (!apiCred?.user_id) {
            throw new Error("Api Credentials invalid!");
        }
        if (!apiCred.full_access || !apiCred.sign) return res.json({
            success: false,
            msg: "Unauthorized"
        });
        ////////////////////////////////////////
        ////////////////////////////////////////
        ////////////////////////////////////////
        /**
         * Grab User data
         *
         * @description Grab User data
         */ const client = new OAuth2Client(clientId);
        const ticket = await client.verifyIdToken({
            idToken: token,
            audience: clientId
        });
        if (!ticket?.getPayload()?.email_verified) {
            return res.json({
                success: false,
                user: null
            });
        }
        const payload = ticket.getPayload();
        if (!payload) throw new Error("No Payload");
        ////////////////////////////////////////
        ////////////////////////////////////////
        ////////////////////////////////////////
        if (!database || typeof database != "string" || database?.match(/ /)) {
            return res.json({
                success: false,
                user: undefined,
                msg: "Please provide a database slug(database name in lowercase with no spaces)"
            });
        }
        ////////////////////////////////////////
        ////////////////////////////////////////
        ////////////////////////////////////////
        /**
         * Create new user folder and file
         *
         * @description Create new user folder and file
         */ const targetDbName = `datasquirel_user_${apiCred.user_id}_${database}`;
        const { given_name , family_name , email , sub , picture , email_verified  } = payload;
        const payloadObject = {
            email: email,
            first_name: given_name,
            last_name: family_name,
            social_id: sub,
            social_platform: "google",
            image: picture,
            image_thumbnail: picture,
            username: `google-user-${sub}`
        };
        if (additionalFields && Object.keys(additionalFields).length > 0) {
            Object.keys(additionalFields).forEach((key)=>{
                // @ts-ignore
                payloadObject[key] = additionalFields[key];
            });
        }
        const loggedInGoogleUser = await _functions_backend_social_login_handleSocialDb__WEBPACK_IMPORTED_MODULE_2___default()({
            database: targetDbName,
            email: email || "",
            payload: payloadObject,
            social_platform: "google",
            res: res,
            social_id: sub,
            additionalFields
        });
        ////////////////////////////////////////
        ////////////////////////////////////////
        ////////////////////////////////////////
        res.json({
            ...loggedInGoogleUser,
            dsqlUserId: apiCred.user_id
        });
    ////////////////////////////////////////
    } catch (/** @type {any} */ error) {
        ////////////////////////////////////////
        _functions_backend_serverError__WEBPACK_IMPORTED_MODULE_1___default()({
            component: "/api/user/google-login/main-catch-error",
            message: error.message,
            user: {}
        });
        res.json({
            success: false,
            msg: "User fetch Error"
        });
    ////////////////////////////////////////
    }
}


/***/ })

};
;

// load runtime
var __webpack_require__ = require("../../../webpack-api-runtime.js");
__webpack_require__.C(exports);
var __webpack_exec__ = (moduleId) => (__webpack_require__(__webpack_require__.s = moduleId))
var __webpack_exports__ = __webpack_require__.X(0, [2224,2163,3017,3403,7547,5886,5338,8326,1311,1007,6926,7487,613,4294,6968], () => (__webpack_exec__(3364)));
module.exports = __webpack_exports__;

})();