datasquirel/users/logout-user.js

72 lines
3.0 KiB
JavaScript
Raw Normal View History

2023-06-24 12:09:26 +00:00
const parseCookies = require("../utils/functions/parseCookies");
2023-05-06 11:14:09 +00:00
/**
2023-06-24 12:09:26 +00:00
* Logout user
2023-05-06 11:14:09 +00:00
* ==============================================================================
2023-05-18 11:43:52 +00:00
* @param {object} params - Single Param object containing params
* @param {object} params.request - Http request object
* @param {object} params.response - Http response object
2023-06-24 15:21:31 +00:00
* @param {string} [params.database] - Target database name(slug): optional => If you don't
* include this you will be logged out of all datasquirel websites instead of just the target
* database
2023-06-24 12:09:26 +00:00
*
* @returns {{success: boolean, payload: string}}
2023-05-06 11:14:09 +00:00
*/
2023-07-07 19:13:13 +00:00
function logoutUser({ request, response, database }) {
2023-05-06 11:14:09 +00:00
/**
* Check Encryption Keys
*
* @description Check Encryption Keys
*/
2023-05-09 14:20:53 +00:00
try {
2023-06-24 15:05:14 +00:00
const cookies = parseCookies({ request });
const cookiesKeys = Object.keys(cookies);
2023-05-09 14:30:08 +00:00
2023-06-24 15:05:14 +00:00
const dbUid = cookies.dsqluid;
const keyRegexp = new RegExp(`datasquirel_${dbUid}_${database}_auth_key`);
const csrfRegexp = new RegExp(`datasquirel_${dbUid}_${database}_csrf`);
const authKeyName = cookiesKeys.filter((cookieKey) => cookieKey.match(keyRegexp))[0];
const csrfName = cookiesKeys.filter((cookieKey) => cookieKey.match(csrfRegexp))[0];
2023-05-09 14:20:53 +00:00
2023-06-24 15:21:31 +00:00
if (authKeyName && csrfName) {
response.setHeader("Set-Cookie", [`${authKeyName}=null;samesite=strict;path=/;HttpOnly=true;Secure=true`, `${csrfName}=null;samesite=strict;path=/;HttpOnly=true`, `dsqluid=null;samesite=strict;path=/;HttpOnly=true`]);
} else {
const allKeys = cookiesKeys.filter((cookieKey) => cookieKey.match(/datasquirel_.*_auth_key/));
const allCsrfs = cookiesKeys.filter((cookieKey) => cookieKey.match(/datasquirel_.*_csrf/));
response.setHeader("Set-Cookie", [...allKeys.map((key) => `${key}=null;samesite=strict;path=/;HttpOnly=true;Secure=true`), ...allCsrfs.map((csrf) => `${csrf}=null;samesite=strict;path=/;HttpOnly=true`), `dsqluid=null;samesite=strict;path=/;HttpOnly=true`]);
}
2023-05-09 14:20:53 +00:00
/** ********************************************** */
/** ********************************************** */
/** ********************************************** */
return {
success: true,
payload: "User Logged Out",
};
/** ********************************************** */
/** ********************************************** */
/** ********************************************** */
} catch (error) {
console.log(error.message);
return {
success: false,
payload: "Logout Failed",
};
}
2023-05-06 11:14:09 +00:00
/** ********************************************** */
/** ********************************************** */
/** ********************************************** */
2023-07-07 19:13:13 +00:00
}
2023-05-06 11:14:09 +00:00
/** ********************************************** */
/** ********************************************** */
/** ********************************************** */
2023-07-07 19:13:13 +00:00
module.exports = logoutUser;