Add basic logout

This commit is contained in:
2026-01-11 15:31:59 -06:00
parent 4a4dc11aa4
commit 096a1235b5
4 changed files with 43 additions and 0 deletions

38
express/basic/logout.ts Normal file
View File

@@ -0,0 +1,38 @@
import { SESSION_COOKIE_NAME } from "../auth/token";
import { services } from "../services";
import type { Call, Result, Route } from "../types";
import { redirect } from "../util";
const logoutHandler = async (call: Call): Promise<Result> => {
// Extract token from cookie and invalidate the session
const token = services.auth.extractToken(call.request);
if (token) {
await services.auth.logout(token);
}
// Clear the cookie and redirect to login
const redirectResult = redirect("/login");
redirectResult.cookies = [
{
name: SESSION_COOKIE_NAME,
value: "",
options: {
httpOnly: true,
secure: false,
sameSite: "lax",
maxAge: 0,
path: "/",
},
},
];
return redirectResult;
};
const logoutRoute: Route = {
path: "/logout",
methods: ["GET", "POST"],
handler: logoutHandler,
};
export { logoutRoute };

View File

@@ -3,6 +3,7 @@ import { services } from "../services";
import type { Call, Result, Route } from "../types";
import { html, render } from "../util";
import { loginRoute } from "./login";
import { logoutRoute } from "./logout";
const routes: Record<string, Route> = {
hello: {
@@ -29,6 +30,7 @@ const routes: Record<string, Route> = {
},
},
login: loginRoute,
logout: logoutRoute,
};
export { routes };

View File

@@ -28,6 +28,7 @@ const routes: Route[] = [
basicRoutes.home,
basicRoutes.hello,
basicRoutes.login,
basicRoutes.logout,
{
path: "/slow",
methods: ["GET"],

View File

@@ -5,7 +5,9 @@
home
</p>
<p>
{{ email }}
</p>
<a href="/logout">logout</a>
</body>
</html>