99 lines
4.2 KiB
JavaScript
99 lines
4.2 KiB
JavaScript
|
"use strict";
|
||
|
exports.id = 5116;
|
||
|
exports.ids = [5116];
|
||
|
exports.modules = {
|
||
|
|
||
|
/***/ 5116:
|
||
|
/***/ ((module, __unused_webpack_exports, __webpack_require__) => {
|
||
|
|
||
|
// @ts-check
|
||
|
/**
|
||
|
* ==============================================================================
|
||
|
* Imports
|
||
|
* ==============================================================================
|
||
|
*/
|
||
|
const http = __webpack_require__(3685);
|
||
|
const decrypt = __webpack_require__(5304);
|
||
|
/** ****************************************************************************** */ /** ****************************************************************************** */ /** ****************************************************************************** */ /** ****************************************************************************** */ /** ****************************************************************************** */ /** ****************************************************************************** */ /**
|
||
|
* @typedef {object} grabDelegatedUserFromCookieReturn
|
||
|
* @property {number} dbUserId
|
||
|
* @property {number} [dbUserId]
|
||
|
* @property {number} [rootUserId]
|
||
|
* @property {string} [rootUserName]
|
||
|
* @property {string} [rootUserEmail]
|
||
|
* @property {string} [rootUserImage]
|
||
|
* @property {string} [databaseFullName]
|
||
|
* @property {string} [databaseSlug]
|
||
|
* @property {string[]} [allowedTables]
|
||
|
* @property {string} [priviledges]
|
||
|
* @property {string} [database]
|
||
|
* @property {boolean} [delegated]
|
||
|
*/ /**
|
||
|
* @param {object} params - user id
|
||
|
* @param {import("next").NextApiRequest | http.IncomingMessage & { cookies: Partial<{ [key: string]: string; }>}} params.request - HTTPS request object
|
||
|
* @param {string | string[]} params.databaseSlug - Database name slug
|
||
|
* @param {{ id: number, first_name: string, last_name: string }} params.user
|
||
|
* @param {any} params.query - query params
|
||
|
*
|
||
|
* @returns {Promise<grabDelegatedUserFromCookieReturn | null>} new user auth object payload
|
||
|
*/ module.exports = async function grabDelegatedUserFromCookie({ request , databaseSlug , user , query , }) {
|
||
|
try {
|
||
|
/**
|
||
|
* Fetch user
|
||
|
*
|
||
|
* @description Fetch user from db
|
||
|
*/ let dbUserId = user.id;
|
||
|
let delegatedUserObject = null;
|
||
|
if (!query?.delegated) return {
|
||
|
dbUserId
|
||
|
};
|
||
|
const rootUserId = query.dbUserId;
|
||
|
const dbFullName = `${process.env.DSQL_USER_DB_PREFIX}${rootUserId}_${databaseSlug}`;
|
||
|
const tokenName = `${process.env.DSQL_USER_DELEGATED_DB_COOKIE_PREFIX}${dbFullName}`;
|
||
|
try {
|
||
|
if (!request.cookies?.[tokenName]) throw new Error("Cookie not present");
|
||
|
// @ts-ignore
|
||
|
const decryptedToken = decrypt(request.cookies[tokenName]);
|
||
|
if (!decryptedToken) throw new Error("Invalid Token");
|
||
|
delegatedUserObject = JSON.parse(decryptedToken);
|
||
|
if (delegatedUserObject.databaseSlug === databaseSlug) {
|
||
|
dbUserId = delegatedUserObject.rootUserId;
|
||
|
return {
|
||
|
dbUserId: dbUserId,
|
||
|
rootUserId: delegatedUserObject.rootUserId,
|
||
|
rootUserName: delegatedUserObject.rootUserName,
|
||
|
rootUserEmail: delegatedUserObject.rootUserEmail,
|
||
|
rootUserImage: delegatedUserObject.rootUserImage,
|
||
|
databaseFullName: delegatedUserObject.databaseFullName,
|
||
|
databaseSlug: delegatedUserObject.databaseSlug,
|
||
|
allowedTables: delegatedUserObject.allowedTables,
|
||
|
priviledges: delegatedUserObject.priviledges,
|
||
|
database: delegatedUserObject.databaseSlug,
|
||
|
delegated: true
|
||
|
};
|
||
|
}
|
||
|
} catch (error) {
|
||
|
// serverError({
|
||
|
// component: "grabDelegatedUserFromCookie",
|
||
|
// message: error.message,
|
||
|
// user: user,
|
||
|
// });
|
||
|
}
|
||
|
////////////////////////////////////////
|
||
|
////////////////////////////////////////
|
||
|
////////////////////////////////////////
|
||
|
return {
|
||
|
dbUserId
|
||
|
};
|
||
|
} catch (error1) {
|
||
|
return null;
|
||
|
}
|
||
|
}; ////////////////////////////////////////
|
||
|
////////////////////////////////////////
|
||
|
////////////////////////////////////////
|
||
|
|
||
|
|
||
|
/***/ })
|
||
|
|
||
|
};
|
||
|
;
|