Files
dsql-admin/dsql-app/.local_dist/server/pages/api/social-login/google-auth.js
T
2024-11-05 15:18:40 +01:00

265 lines
7.5 KiB
JavaScript

(() => {
var exports = {};
exports.id = 5288;
exports.ids = [5288];
exports.modules = {
/***/ 9538:
/***/ ((module) => {
"use strict";
module.exports = require("datasquirel");
/***/ }),
/***/ 3785:
/***/ ((module) => {
"use strict";
module.exports = require("generate-password");
/***/ }),
/***/ 6781:
/***/ ((module) => {
"use strict";
module.exports = require("google-auth-library");
/***/ }),
/***/ 6517:
/***/ ((module) => {
"use strict";
module.exports = require("lodash");
/***/ }),
/***/ 5184:
/***/ ((module) => {
"use strict";
module.exports = require("nodemailer");
/***/ }),
/***/ 6109:
/***/ ((module) => {
"use strict";
module.exports = require("sanitize-html");
/***/ }),
/***/ 2261:
/***/ ((module) => {
"use strict";
module.exports = require("serverless-mysql");
/***/ }),
/***/ 4300:
/***/ ((module) => {
"use strict";
module.exports = require("buffer");
/***/ }),
/***/ 6113:
/***/ ((module) => {
"use strict";
module.exports = require("crypto");
/***/ }),
/***/ 7147:
/***/ ((module) => {
"use strict";
module.exports = require("fs");
/***/ }),
/***/ 3685:
/***/ ((module) => {
"use strict";
module.exports = require("http");
/***/ }),
/***/ 1017:
/***/ ((module) => {
"use strict";
module.exports = require("path");
/***/ }),
/***/ 5382:
/***/ ((__unused_webpack_module, exports, __webpack_require__) => {
// @ts-check
const { createHmac } = __webpack_require__(6113);
//
/**
* # Password Hash function
* @param {string} password
* @returns
*/ function hashPassword(password) {
const hmac = createHmac("sha512", process.env.DSQL_ENCRYPTION_PASSWORD || "");
hmac.update(password);
let hashed = hmac.digest("base64");
return hashed;
}
exports.hashPassword = hashPassword; // export const comparePasswords = async (password) => {
// const hmac = createHmac("sha512", process.env.DSQL_ENCRYPTION_PASSWORD);
// hmac.update(password);
// let hashed = hmac.digest("base64");
// let dbPass = await global.DB_HANDLER(`SELECT * FROM users WHERE password = '${hashed}'`);
// console.log(dbPass);
// return dbPass;
// };
/***/ }),
/***/ 2666:
/***/ ((__unused_webpack_module, __webpack_exports__, __webpack_require__) => {
"use strict";
__webpack_require__.r(__webpack_exports__);
/* harmony export */ __webpack_require__.d(__webpack_exports__, {
/* harmony export */ "default": () => (/* binding */ handler)
/* harmony export */ });
/* harmony import */ var _functions_backend_social_login_handleSocialDb__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(7839);
/* harmony import */ var _functions_backend_social_login_handleSocialDb__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(_functions_backend_social_login_handleSocialDb__WEBPACK_IMPORTED_MODULE_0__);
// @ts-check
/**
* ==============================================================================
* Imports
* ==============================================================================
*/ const { OAuth2Client } = __webpack_require__(6781);
const { hashPassword , } = __webpack_require__(5382);
const serverError = __webpack_require__(2163);
//////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////
/**
* 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({
msg: "Failed!"
});
////////////////////////////////////////////////
////////////////////////////////////////////////
////////////////////////////////////////////////
/**
* User auth
*
* @description Authenticate user
*/ const reqBody = req.body;
const { invite } = req.query;
const { token } = reqBody;
////////////////////////////////////////////////
////////////////////////////////////////////////
////////////////////////////////////////////////
/**
* Send Response
*
* @description Send a boolean response
*/ try {
const client = new OAuth2Client("392696781563-imb0ddojfn6m4bdokjk5v80jn546t9tq.apps.googleusercontent.com");
const ticket = await client.verifyIdToken({
idToken: token,
audience: "392696781563-imb0ddojfn6m4bdokjk5v80jn546t9tq.apps.googleusercontent.com"
});
const payload = ticket.getPayload();
if (!payload || !payload.email_verified) {
res.json({
user: null,
msg: "Invalid Credentials"
});
return;
}
const { given_name , family_name , email , sub , picture , email_verified } = payload;
if (!email) throw new Error("No Email");
////////////////////////////////////////////////
////////////////////////////////////////////////
////////////////////////////////////////////////
/**
* Create new user folder and file
*
* @description Create new user folder and file
*/ const loggedInGoogleUser = await _functions_backend_social_login_handleSocialDb__WEBPACK_IMPORTED_MODULE_0___default()({
email: email,
payload: {
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}`
},
social_platform: "google",
res: res,
social_id: sub,
invitation: invite
});
////////////////////////////////////////////////
////////////////////////////////////////////////
////////////////////////////////////////////////
res.json(loggedInGoogleUser);
////////////////////////////////////////////////
////////////////////////////////////////////////
////////////////////////////////////////////////
} catch (/** @type {any} */ error) {
////////////////////////////////////////////////
////////////////////////////////////////////////
////////////////////////////////////////////////
console.log(error.message);
// serverError({
// component: "/api/social-login/github-auth/catch-error",
// message: error.message,
// user: user,
// });
res.json({
success: false,
msg: "Failed!"
});
////////////////////////////////////////////////
////////////////////////////////////////////////
////////////////////////////////////////////////
}
}
/***/ })
};
;
// 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,6926,7487,613,4294,6968], () => (__webpack_exec__(2666)));
module.exports = __webpack_exports__;
})();