From 5bda1e0b7607122cb30ce2856398f9ad81c60fa1 Mon Sep 17 00:00:00 2001 From: Cali93 Date: Wed, 31 May 2023 14:37:45 +0300 Subject: [PATCH] feat(disconnect): add disconnect logic --- src/index.ts | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/index.ts b/src/index.ts index bbefe90..2fecdbd 100644 --- a/src/index.ts +++ b/src/index.ts @@ -16,6 +16,9 @@ declare module "express-session" { } const { PORT, COOKIE_SECRET, COOKIE_NAME } = process.env; +if (!COOKIE_NAME) { + throw new ReferenceError("COOKIE_NAME missing in environment variables"); +} if (!COOKIE_SECRET) { throw new ReferenceError("COOKIE_SECRET missing in environment variables"); } @@ -89,6 +92,16 @@ app.get("/nonce", async function (req, res) { app.post("/connect", verifyAndSignIn); +app.post("/disconnect", async function (req, res) { + res.clearCookie(COOKIE_NAME); + return req.session.destroy((err) => { + if (err) { + return res.status(500).json({ error: "Failed to destroy session" }); + } + return res.status(200).json({ status: "Disconnected" }); + }); +}); + // custom 404 app.use((req, res, next) => { return res.status(404).json({ error: "Sorry can't find that!" });