Initial commit
This commit is contained in:
@@ -0,0 +1,33 @@
|
||||
function redirectUnauthorized(res, req) {
|
||||
res.cork(() => res.writeStatus("302").writeHeader("Location", "/").end());
|
||||
};
|
||||
|
||||
module.exports = async (res, req) => {
|
||||
const cookieHeader = req.getHeader("cookie");
|
||||
const cookie = cookieHeader.split("ps=")[1];
|
||||
|
||||
res.onAborted(() => res.isAborted = true);
|
||||
|
||||
if (!cookie) {
|
||||
console.log("no cookie - logout");
|
||||
return redirectUnauthorized(res, req);
|
||||
};
|
||||
|
||||
const formattedCookie = cookie.split(".")[0].slice(4);
|
||||
const cookieInDB = await redis.get(`sess:${formattedCookie}`);
|
||||
|
||||
if (!cookieInDB) {
|
||||
console.log("no db - logout");
|
||||
return redirectUnauthorized(res, req);
|
||||
};
|
||||
|
||||
const parsedCookie = JSON.parse(cookieInDB);
|
||||
if (!parsedCookie.accountID) {
|
||||
console.log("no accountid - logout");
|
||||
return redirectUnauthorized(res, req);
|
||||
};
|
||||
|
||||
await redis.del(`sess:${formattedCookie}`);
|
||||
|
||||
return res.cork(() => res.writeStatus("302").writeHeader("Set-Cookie", "ps=; Domain=.pirate-corsaire.com").writeHeader("Location", "/").end());
|
||||
};
|
||||
Reference in New Issue
Block a user