Initial commit
This commit is contained in:
@@ -0,0 +1,36 @@
|
||||
const redis = require("../Miscellaneous/redis");
|
||||
|
||||
async function isAuthenticated(res, req) {
|
||||
const cookieHeader = req.getHeader("cookie");
|
||||
const cookie = cookieHeader.split("ps=")[1];
|
||||
if (!cookie) {
|
||||
res.cork(() => res
|
||||
.writeStatus("400")
|
||||
.end("Unauthenticated"));
|
||||
|
||||
return false;
|
||||
};
|
||||
|
||||
const formattedCookie = cookie.split(".")[0].slice(4);
|
||||
const cookieInDB = await redis.get(`sess:${formattedCookie}`);
|
||||
if (!cookieInDB) {
|
||||
res.cork(() => res
|
||||
.writeStatus("400")
|
||||
.end("Unauthenticated"));
|
||||
|
||||
return false;
|
||||
};
|
||||
|
||||
const parsedCookie = JSON.parse(cookieInDB);
|
||||
if (!parsedCookie.isAdmin) {
|
||||
res.cork(() => res
|
||||
.writeStatus("400")
|
||||
.end("Unauthorized"));
|
||||
|
||||
return false;
|
||||
};
|
||||
|
||||
return true;
|
||||
};
|
||||
|
||||
module.exports = isAuthenticated;
|
||||
Reference in New Issue
Block a user