datasquirel/users/logout-user.ts

109 lines
3.2 KiB
TypeScript
Raw Normal View History

2025-01-10 19:10:28 +00:00
import http from "http";
import getAuthCookieNames from "../package-shared/functions/backend/cookies/get-auth-cookie-names";
import decrypt from "../package-shared/functions/dsql/decrypt";
import EJSON from "../package-shared/utils/ejson";
import { deleteAuthFile } from "../package-shared/functions/backend/auth/write-auth-files";
import parseCookies from "../package-shared/utils/backend/parseCookies";
import { DATASQUIREL_LoggedInUser } from "../package-shared/types";
2023-09-21 14:00:04 +00:00
2025-01-10 19:10:28 +00:00
type Param = {
encryptedUserString?: string;
request?: http.IncomingMessage & { [s: string]: any };
response?: http.ServerResponse & { [s: string]: any };
cookieString?: string;
database?: string;
dsqlUserId?: string | number;
};
type Return = {
success: boolean;
msg: string;
cookieNames?: any;
};
2023-09-21 14:00:04 +00:00
/**
2025-01-10 19:10:28 +00:00
* # Logout user
2023-09-21 14:00:04 +00:00
*/
2025-01-10 19:10:28 +00:00
export default function logoutUser({
2024-12-10 18:05:05 +00:00
response,
database,
dsqlUserId,
encryptedUserString,
request,
2024-12-13 13:08:41 +00:00
cookieString,
2025-01-10 19:10:28 +00:00
}: Param): Return {
2023-09-21 14:00:04 +00:00
/**
* Check Encryption Keys
*
* @description Check Encryption Keys
*/
try {
2024-12-10 18:05:05 +00:00
const cookieNames = getAuthCookieNames({
database,
userId: dsqlUserId || process.env.DSQL_API_USER_ID,
2024-12-08 08:58:57 +00:00
});
2024-12-10 18:05:05 +00:00
const authKeyName = cookieNames.keyCookieName;
const csrfName = cookieNames.csrfCookieName;
2024-12-10 18:19:12 +00:00
const oneTimeCodeName = getAuthCookieNames().oneTimeCodeName;
2024-12-10 18:05:05 +00:00
2025-01-10 19:10:28 +00:00
const decryptedUserJSON: string | undefined = (() => {
2024-12-10 18:05:05 +00:00
try {
if (request) {
2024-12-13 13:08:41 +00:00
const cookiesObject = parseCookies({
request,
cookieString,
});
2024-12-10 18:05:05 +00:00
return decrypt({
encryptedString: cookiesObject[authKeyName],
});
} else if (encryptedUserString) {
return decrypt({
encryptedString: encryptedUserString,
});
} else {
return undefined;
}
2025-01-10 19:10:28 +00:00
} catch (/** @type {any} */ error: any) {
2024-12-10 18:05:05 +00:00
console.log(
"Error getting decrypted User JSON to logout:",
error.message
);
return undefined;
}
})();
if (!decryptedUserJSON) throw new Error("Invalid User");
2025-01-10 19:10:28 +00:00
const userObject = EJSON.parse(
decryptedUserJSON
) as DATASQUIREL_LoggedInUser;
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
response?.setHeader("Set-Cookie", [
`${authKeyName}=null;max-age=0`,
`${csrfName}=null;max-age=0`,
2024-12-10 18:19:12 +00:00
`${oneTimeCodeName}=null;max-age=0`,
2024-12-08 08:58:57 +00:00
]);
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,
2024-12-10 18:14:00 +00:00
msg: "User Logged Out",
2024-12-08 08:58:57 +00:00
cookieNames,
2023-09-21 14:00:04 +00:00
};
2025-01-10 19:10:28 +00:00
} catch (/** @type {any} */ error: any) {
2024-12-10 18:14:00 +00:00
console.log("Logout Error:", error.message);
2023-09-21 14:00:04 +00:00
return {
success: false,
2024-12-10 18:14:00 +00:00
msg: "Logout Failed",
2023-09-21 14:00:04 +00:00
};
}
}
module.exports = logoutUser;