dsql-admin/dsql-app/.local_dist/server/pages/api/reAuthenticateUser.js
Benjamin Toby 748ff55092 Bug Fixes
2024-11-05 15:18:40 +01:00

247 lines
9.2 KiB
JavaScript

"use strict";
(() => {
var exports = {};
exports.id = 9662;
exports.ids = [9662];
exports.modules = {
/***/ 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");
/***/ }),
/***/ 2148:
/***/ ((module, __unused_webpack_exports, __webpack_require__) => {
// @ts-check
/**
* ==============================================================================
* Imports
* ==============================================================================
*/
const fs = __webpack_require__(7147);
////////////////////////////////////////
////////////////////////////////////////
////////////////////////////////////////
const serverError = __webpack_require__(2163);
const { ServerResponse } = __webpack_require__(3685);
const DB_HANDLER = __webpack_require__(2224);
const encrypt = __webpack_require__(7547);
/** ****************************************************************************** */ /** ****************************************************************************** */ /** ****************************************************************************** */ /** ****************************************************************************** */ /** ****************************************************************************** */ /** ****************************************************************************** */ /**
* ==============================================================================
* Main Function
* ==============================================================================
* @param {object} params
* @param {number} params.userId - user id
* @param {string} params.csrf
* @param {import("next").NextApiResponse} params.res - response object
*
* @returns {Promise<import("@/package-shared/types").UserType | null>} new user auth object payload
*/ module.exports = async function reAuthUser({ userId , res , csrf }) {
try {
const allowedAuthKeysPath = process.env.DSQL_USER_LOGIN_KEYS_PATH;
if (!allowedAuthKeysPath) {
console.log(`DSQL_USER_LOGIN_KEYS_PATH env variable not found. Please set this variable.`);
return null;
}
/**
* Fetch user
*
* @description Fetch user from db
*/ let refreshedUser = await DB_HANDLER(`SELECT * FROM users WHERE id=?`, [
userId,
]);
fs.unlinkSync(`${allowedAuthKeysPath}/${csrf}`);
/**
* Generate csrf
*
* @description Generate csrf
*/ let csrfKey = Math.random().toString(36).substring(2) + "-" + Math.random().toString(36).substring(2);
////////////////////////////////////////
////////////////////////////////////////
////////////////////////////////////////
/**
* Construct new user payload
*
* @description Construct new user payload
* @type {import("@/package-shared/types").UserType}
*/ let newUserPayload = {
id: refreshedUser[0].id,
stripe_id: refreshedUser[0].stripe_id || "",
first_name: refreshedUser[0].first_name,
last_name: refreshedUser[0].last_name,
email: refreshedUser[0].email,
social_id: refreshedUser[0].social_id,
image: refreshedUser[0].image,
image_thumbnail: refreshedUser[0].image_thumbnail,
verification_status: refreshedUser[0].verification_status,
social_login: refreshedUser[0].social_login,
social_platform: refreshedUser[0].social_platform,
csrf_k: csrfKey,
logged_in_status: true,
date: Date.now()
};
////////////////////////////////////////
////////////////////////////////////////
////////////////////////////////////////
/**
* Encrypt payload
*
* @description Encrypt new user payload
*/ let encryptedPayload = encrypt(JSON.stringify(newUserPayload));
fs.writeFileSync(`${allowedAuthKeysPath}/${csrfKey}`, JSON.stringify(newUserPayload));
/**
* Update header
*
* @description Update header response with new payload
*/ res.setHeader("Set-Cookie", [
`datasquirelAuthKey=${encryptedPayload};samesite=strict;path=/;HttpOnly=true;Secure=true`,
`csrf=${csrfKey};samesite=strict;path=/;HttpOnly=true;Secure=true`,
`user_refresh=1`,
]);
// ===============================================================================
// ============================ End User Refresh =================================
// ===============================================================================
/**
* Return payload
*
* @description return new user object payload
*/ return newUserPayload;
////////////////////////////////////////
////////////////////////////////////////
////////////////////////////////////////
} catch (/** @type {*} */ error) {
serverError({
component: "reAuthUser",
message: error.message,
user: {
id: userId
}
});
return null;
}
}; ////////////////////////////////////////
////////////////////////////////////////
////////////////////////////////////////
/***/ }),
/***/ 8251:
/***/ ((__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_userAuth__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(6825);
/* harmony import */ var _functions_backend_userAuth__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(_functions_backend_userAuth__WEBPACK_IMPORTED_MODULE_0__);
/* harmony import */ var _functions_backend_reAuthUser__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(2148);
/* harmony import */ var _functions_backend_reAuthUser__WEBPACK_IMPORTED_MODULE_1___default = /*#__PURE__*/__webpack_require__.n(_functions_backend_reAuthUser__WEBPACK_IMPORTED_MODULE_1__);
// @ts-check
/**
* ==============================================================================
* Imports
* ==============================================================================
*/
/** ****************************************************************************** */ /** ****************************************************************************** */ /** ****************************************************************************** */ /** ****************************************************************************** */ /** ****************************************************************************** */ /** ****************************************************************************** */ /**
* @typedef {object} reAuthenticateUserResponse
* @property {boolean} status - Response Status
* @property {string | null} [csrf] - CSRF String
* @property {import("@/package-shared/types").UserType | null} user - Authenticated User
*/ /**
* ==============================================================================
* 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") res.json({
msg: "Failed!"
});
/**
* User auth
*
* @description Authenticate user
*/ let user = await _functions_backend_userAuth__WEBPACK_IMPORTED_MODULE_0___default()(req, res, true);
if (!user) {
console.log("No user Found");
res.redirect(307, "/logout");
return;
}
/**
* Re Authenticate User
*
* @description re authenticate user
*/ let reAuthenticatedUser = await _functions_backend_reAuthUser__WEBPACK_IMPORTED_MODULE_1___default()({
userId: user.id,
res,
csrf: user.csrf_k
});
/**
* Send Response
*
* @description Send a boolean response
*/ /** @type {reAuthenticateUserResponse} */ const resObj = {
status: reAuthenticatedUser ? true : false,
csrf: reAuthenticatedUser?.csrf_k,
user: reAuthenticatedUser
};
res.json(resObj);
}
/***/ })
};
;
// 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,6825,3017,7547], () => (__webpack_exec__(8251)));
module.exports = __webpack_exports__;
})();