2023-09-21 14:00:04 +00:00
|
|
|
// @ts-check
|
|
|
|
|
|
|
|
const http = require("http");
|
2024-12-06 11:55:03 +00:00
|
|
|
const getAuthCookieNames = require("../package-shared/functions/backend/cookies/get-auth-cookie-names");
|
2024-12-08 08:58:57 +00:00
|
|
|
const decrypt = require("../package-shared/functions/dsql/decrypt");
|
|
|
|
const EJSON = require("../package-shared/utils/ejson");
|
|
|
|
const {
|
|
|
|
deleteAuthFile,
|
|
|
|
} = require("../package-shared/functions/backend/auth/write-auth-files");
|
2023-09-21 14:00:04 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Logout user
|
|
|
|
* ==============================================================================
|
|
|
|
* @param {object} params - Single Param object containing params
|
2024-12-08 08:58:57 +00:00
|
|
|
* @param {string} params.encryptedUserString - Encrypted User String
|
|
|
|
* @param {http.ServerResponse & Object<string, any>} [params.response] - Http response object
|
|
|
|
* @param {string} [params.database] - Target database name(slug): optional
|
|
|
|
* @param {string | number} [params.dsqlUserId]
|
2023-09-21 14:00:04 +00:00
|
|
|
*
|
2024-12-08 08:58:57 +00:00
|
|
|
* @returns {{success: boolean, payload: string, cookieNames?: any}}
|
2023-09-21 14:00:04 +00:00
|
|
|
*/
|
2024-12-08 08:58:57 +00:00
|
|
|
function logoutUser({ response, database, dsqlUserId, encryptedUserString }) {
|
2023-09-21 14:00:04 +00:00
|
|
|
/**
|
|
|
|
* Check Encryption Keys
|
|
|
|
*
|
|
|
|
* @description Check Encryption Keys
|
|
|
|
*/
|
|
|
|
try {
|
2024-12-08 08:58:57 +00:00
|
|
|
const decryptedUserJSON = decrypt({
|
|
|
|
encryptedString: encryptedUserString,
|
|
|
|
});
|
|
|
|
const userObject =
|
|
|
|
/** @type {import("../package-shared/types").DATASQUIREL_LoggedInUser | undefined} */ (
|
|
|
|
EJSON.parse(decryptedUserJSON)
|
|
|
|
);
|
2023-09-21 14:00:04 +00:00
|
|
|
|
2024-12-08 08:58:57 +00:00
|
|
|
if (!userObject?.csrf_k)
|
|
|
|
throw new Error("Invalid User. Please check key");
|
2024-12-06 11:55:03 +00:00
|
|
|
|
2024-12-08 08:58:57 +00:00
|
|
|
const cookieNames = getAuthCookieNames({
|
|
|
|
database,
|
|
|
|
userId: dsqlUserId || process.env.DSQL_API_USER_ID,
|
|
|
|
});
|
|
|
|
const authKeyName = cookieNames.keyCookieName;
|
|
|
|
const csrfName = cookieNames.csrfCookieName;
|
2023-09-21 14:00:04 +00:00
|
|
|
|
2024-12-08 08:58:57 +00:00
|
|
|
response?.setHeader("Set-Cookie", [
|
|
|
|
`${authKeyName}=null;max-age=0`,
|
|
|
|
`${csrfName}=null;max-age=0`,
|
|
|
|
]);
|
2023-09-21 14:00:04 +00:00
|
|
|
|
2024-12-08 08:58:57 +00:00
|
|
|
const csrf = userObject.csrf_k;
|
|
|
|
deleteAuthFile(csrf);
|
2023-09-21 14:00:04 +00:00
|
|
|
|
|
|
|
return {
|
|
|
|
success: true,
|
|
|
|
payload: "User Logged Out",
|
2024-12-08 08:58:57 +00:00
|
|
|
cookieNames,
|
2023-09-21 14:00:04 +00:00
|
|
|
};
|
|
|
|
} catch (error) {
|
|
|
|
console.log(error);
|
|
|
|
|
|
|
|
return {
|
|
|
|
success: false,
|
|
|
|
payload: "Logout Failed",
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/** ********************************************** */
|
|
|
|
/** ********************************************** */
|
|
|
|
/** ********************************************** */
|
|
|
|
}
|
|
|
|
|
|
|
|
/** ********************************************** */
|
|
|
|
/** ********************************************** */
|
|
|
|
/** ********************************************** */
|
|
|
|
|
|
|
|
module.exports = logoutUser;
|