Skip to content

Commit

Permalink
feat(disconnect): add disconnect logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Cali93 committed May 31, 2023
1 parent 8aa2e18 commit 5bda1e0
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
Expand Down Expand Up @@ -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!" });
Expand Down

0 comments on commit 5bda1e0

Please sign in to comment.