mirror of
https://github.com/fosrl/pangolin.git
synced 2025-08-09 20:35:28 +02:00
added logout endpoint
This commit is contained in:
parent
2b0e55c0bf
commit
cfd0a15e2c
3 changed files with 37 additions and 0 deletions
|
@ -1,2 +1,3 @@
|
|||
export * from "./login";
|
||||
export * from "./signup";
|
||||
export * from "./logout";
|
||||
|
|
35
server/routers/auth/logout.ts
Normal file
35
server/routers/auth/logout.ts
Normal file
|
@ -0,0 +1,35 @@
|
|||
import { Request, Response, NextFunction } from "express";
|
||||
import { lucia } from "@server/auth";
|
||||
import createHttpError from "http-errors";
|
||||
import HttpCode from "@server/types/HttpCode";
|
||||
import response from "@server/utils/response";
|
||||
|
||||
export async function logout(
|
||||
req: Request,
|
||||
res: Response,
|
||||
next: NextFunction,
|
||||
): Promise<any> {
|
||||
const sessionId = req.cookies[lucia.sessionCookieName];
|
||||
|
||||
if (!sessionId) {
|
||||
return next(
|
||||
createHttpError(
|
||||
HttpCode.BAD_REQUEST,
|
||||
"You must be logged in to sign out",
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
await lucia.invalidateSession(sessionId);
|
||||
res.setHeader("Set-Cookie", lucia.createBlankSessionCookie().serialize());
|
||||
|
||||
return res.status(HttpCode.OK).send(
|
||||
response<null>({
|
||||
data: null,
|
||||
success: true,
|
||||
error: false,
|
||||
message: "Logged out successfully",
|
||||
status: HttpCode.OK,
|
||||
}),
|
||||
);
|
||||
}
|
|
@ -46,3 +46,4 @@ unauthenticated.use("/auth", authRouter);
|
|||
|
||||
authRouter.put("/signup", auth.signup);
|
||||
authRouter.post("/login", auth.login);
|
||||
authRouter.post("/logout", auth.logout);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue