dsql-admin/dsql-app/.local_dist/server/pages/api/addSocialLogin.js
2024-11-05 12:12:42 +01:00

225 lines
8.2 KiB
JavaScript

"use strict";
(() => {
var exports = {};
exports.id = 5012;
exports.ids = [5012];
exports.modules = {
/***/ 2261:
/***/ ((module) => {
module.exports = require("serverless-mysql");
/***/ }),
/***/ 4300:
/***/ ((module) => {
module.exports = require("buffer");
/***/ }),
/***/ 2081:
/***/ ((module) => {
module.exports = require("child_process");
/***/ }),
/***/ 6113:
/***/ ((module) => {
module.exports = require("crypto");
/***/ }),
/***/ 7147:
/***/ ((module) => {
module.exports = require("fs");
/***/ }),
/***/ 3685:
/***/ ((module) => {
module.exports = require("http");
/***/ }),
/***/ 1017:
/***/ ((module) => {
module.exports = require("path");
/***/ }),
/***/ 1541:
/***/ ((__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 _functions_backend_serverError__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(2163);
/* harmony import */ var _functions_backend_serverError__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(_functions_backend_serverError__WEBPACK_IMPORTED_MODULE_0__);
/* harmony import */ var _functions_backend_userAuth__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(6825);
/* harmony import */ var _functions_backend_userAuth__WEBPACK_IMPORTED_MODULE_1___default = /*#__PURE__*/__webpack_require__.n(_functions_backend_userAuth__WEBPACK_IMPORTED_MODULE_1__);
// @ts-check
/**
* ==============================================================================
* Imports
* ==============================================================================
*/ const fs = __webpack_require__(7147);
const path = __webpack_require__(1017);
const { execSync } = __webpack_require__(2081);
/** ****************************************************************************** */ /** ****************************************************************************** */ /** ****************************************************************************** */ /** ****************************************************************************** */ /** ****************************************************************************** */ /** ****************************************************************************** */ // import { get, set } from "lodash";
// const rateLimit = 10; // Number of allowed requests per minute
// const rateLimiter = {};
// const rateLimiterMiddleware = (ip) => {
// const now = Date.now();
// const windowStart = now - 60 * 1000; // 1 minute ago
// const requestTimestamps = get(rateLimiter, ip, []).filter((timestamp) => timestamp > windowStart);
// requestTimestamps.push(now);
// set(rateLimiter, ip, requestTimestamps);
// return requestTimestamps.length <= rateLimit;
// };
/** ****************************************************************************** */ /** ****************************************************************************** */ /** ****************************************************************************** */ /** ****************************************************************************** */ /** ****************************************************************************** */ /** ****************************************************************************** */ /**
* 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 ip = req.headers["x-real-ip"] || req.connection.remoteAddress;
// if (!rateLimiterMiddleware(ip)) {
// // res.status(429).json({ message: "Too Many Requests" });
// return res.status(429).json({ msg: "Too Many Requests" });
// }
/**
* User auth
*
* @description Authenticate user
*/ const user = await _functions_backend_userAuth__WEBPACK_IMPORTED_MODULE_1___default()(req, res, true);
if (!user) {
return res.json({
success: false,
msg: "Unauthorized"
});
}
/**
* User auth
*
* @description Authenticate user
*/ const sanitizedReqBody = req.body;
const { dbFullName , dbSlug , paradigm , platforms } = sanitizedReqBody;
/**
* Send Response
*
* @description Send a boolean response
*/ try {
/**
* Create new user folder and file
*
* @description Create new user folder and file
*/ const userEnvFilePath = `./jsonData/dbSchemas/users/user-${user.id}/env.json`;
if (!fs.existsSync(userEnvFilePath)) {
fs.writeFileSync(userEnvFilePath, "{}", "utf8");
}
const envObject = JSON.parse(fs.readFileSync(userEnvFilePath, "utf8"));
let databseObject = envObject[dbFullName];
if (!databseObject) {
databseObject = {
social: []
};
envObject[dbFullName] = databseObject;
}
/** @type {object[]} */ let socialLogins = databseObject.social;
socialLogins = [];
for(let i = 0; i < platforms.length; i++){
const platform = platforms[i];
const { clientId , clientSecret , domain1 , domain2 , callbackUrl } = platform;
if (paradigm?.match(/personal/) && !clientId?.match(/./)) {
throw new Error("Client Id Must be present");
}
if (paradigm?.match(/personal/) && !clientSecret?.match(/./)) {
throw new Error("Client Secret Must be present");
}
if (paradigm?.match(/datasquirel/) && !domain1?.match(/./)) {
throw new Error("Atleast one Domain Must be present");
}
// const existingPlatformIndex = socialLogins.findIndex((scObj) => scObj.platform === platform.platform);
// console.log(existingPlatformIndex);
// if (existingPlatformIndex >= 0) {
// if (paradigm?.match(/personal/)) {
// socialLogins[existingPlatformIndex] = { ...platform, paradigm };
// } else {
// socialLogins[existingPlatformIndex] = { paradigm, platform: platform.platform };
// }
// } else {
// socialLogins.push({ paradigm, ...platform });
// }
if (paradigm?.match(/personal/)) {
socialLogins.push({
platform: platform.platform,
paradigm,
clientId,
clientSecret,
callbackUrl
});
} else {
socialLogins.push({
platform: platform.platform,
paradigm,
domain1,
domain2
});
}
}
databseObject.social = socialLogins;
fs.writeFileSync(userEnvFilePath, JSON.stringify(envObject, null, 4), "utf-8");
res.json({
success: true
});
////////////////////////////////////////
} catch (/** @type {any} */ error) {
console.log(error.message);
////////////////////////////////////////
_functions_backend_serverError__WEBPACK_IMPORTED_MODULE_0___default()({
component: "/api/addSocialLogin/catch-error",
message: error.message,
user: user
});
res.json({
success: false,
msg: error.message
});
////////////////////////////////////////
}
}
/***/ })
};
;
// 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, [5425,2224,2163,6825], () => (__webpack_exec__(1541)));
module.exports = __webpack_exports__;
})();