blob: 80f8f9fa5527cf3b69dd3793560fd3e719ac8cf1 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
import cookie from "cookie";
import db from "../db";
import type { Middleware } from "waku/config";
const cookieMiddleware: Middleware = () => {
return async (ctx, next) => {
// Parse incoming cookies
const cookies = cookie.parse(ctx.req.headers.cookie || "");
const coki = cookies.sorlang;
// If cookie exists, fetch user data and set in context
if (coki) {
const userRow = db.fetchCookie(coki);
if (userRow) ctx.data.user = { id: userRow.id, name: userRow.name };
// console.log("User authenticated:", userRow.name);
}
// Uncomment to enable redirection for unauthenticated users
/*
if (!ctx.data.user && ctx.req.url.pathname !== "/login") {
ctx.res.status = 302;
ctx.res.headers = {
Location: "/login",
};
return;
}
*/
await next();
// Cookie setting is now handled by setCookieMiddleware
};
};
export default cookieMiddleware;
|