datasquirel/package-shared/functions/backend/suSocketAuth.js

44 lines
1.4 KiB
JavaScript
Raw Permalink Normal View History

2024-11-06 06:26:23 +00:00
// @ts-check
const { IncomingMessage } = require("http");
2024-11-06 11:58:41 +00:00
const parseCookies = require("../../utils/backend/parseCookies");
2024-12-06 10:31:24 +00:00
const decrypt = require("../dsql/decrypt");
const getAuthCookieNames = require("./cookies/get-auth-cookie-names");
2024-11-06 06:26:23 +00:00
/**
* @async
* @param {IncomingMessage} req - https request object
*
* @returns {Promise<({ email: string, password: string, authKey: string, logged_in_status: boolean, date: number } | null)>}
*/
module.exports = async function (req) {
2024-12-06 10:31:24 +00:00
const { keyCookieName, csrfCookieName } = getAuthCookieNames();
const suKeyName = `${keyCookieName}_su`;
2024-11-06 06:26:23 +00:00
const cookies = parseCookies({ request: req });
2024-12-06 10:31:24 +00:00
if (!cookies?.[suKeyName]) {
2024-11-06 06:26:23 +00:00
return null;
}
/** ********************* Grab the payload */
2024-12-06 10:31:24 +00:00
let userPayload = decrypt({
encryptedString: cookies[suKeyName],
});
2024-11-06 06:26:23 +00:00
/** ********************* Return if no payload */
if (!userPayload) return null;
/** ********************* Parse the payload */
let userObject = JSON.parse(userPayload);
if (userObject.password !== process.env.DSQL_USER_KEY) return null;
if (userObject.authKey !== process.env.DSQL_SPECIAL_KEY) return null;
////////////////////////////////////////
////////////////////////////////////////
////////////////////////////////////////
/** ********************* return user object */
return userObject;
};