Add basic logout
This commit is contained in:
38
express/basic/logout.ts
Normal file
38
express/basic/logout.ts
Normal 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 };
|
||||||
@@ -3,6 +3,7 @@ import { services } from "../services";
|
|||||||
import type { Call, Result, Route } from "../types";
|
import type { Call, Result, Route } from "../types";
|
||||||
import { html, render } from "../util";
|
import { html, render } from "../util";
|
||||||
import { loginRoute } from "./login";
|
import { loginRoute } from "./login";
|
||||||
|
import { logoutRoute } from "./logout";
|
||||||
|
|
||||||
const routes: Record<string, Route> = {
|
const routes: Record<string, Route> = {
|
||||||
hello: {
|
hello: {
|
||||||
@@ -29,6 +30,7 @@ const routes: Record<string, Route> = {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
login: loginRoute,
|
login: loginRoute,
|
||||||
|
logout: logoutRoute,
|
||||||
};
|
};
|
||||||
|
|
||||||
export { routes };
|
export { routes };
|
||||||
|
|||||||
@@ -28,6 +28,7 @@ const routes: Route[] = [
|
|||||||
basicRoutes.home,
|
basicRoutes.home,
|
||||||
basicRoutes.hello,
|
basicRoutes.hello,
|
||||||
basicRoutes.login,
|
basicRoutes.login,
|
||||||
|
basicRoutes.logout,
|
||||||
{
|
{
|
||||||
path: "/slow",
|
path: "/slow",
|
||||||
methods: ["GET"],
|
methods: ["GET"],
|
||||||
|
|||||||
@@ -5,7 +5,9 @@
|
|||||||
home
|
home
|
||||||
</p>
|
</p>
|
||||||
<p>
|
<p>
|
||||||
|
|
||||||
{{ email }}
|
{{ email }}
|
||||||
</p>
|
</p>
|
||||||
|
<a href="/logout">logout</a>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|||||||
Reference in New Issue
Block a user