senlinyy commited on
Commit
69698fb
·
1 Parent(s): bfb06d5

fix: fix cookie

Browse files
frontend/app/api/admin/login/route.ts CHANGED
@@ -1,12 +1,10 @@
1
  import { type NextRequest, NextResponse } from "next/server";
 
2
  import { backendUrl } from "@/lib/backend-url";
3
 
4
  export const dynamic = "force-dynamic";
5
  export const runtime = "nodejs";
6
 
7
- const ADMIN_COOKIE = "admin_session";
8
- const SESSION_MAX_AGE = 60 * 60 * 12;
9
-
10
  function sessionValue(setCookie: string | null) {
11
  const pair = setCookie?.split(";")[0] ?? "";
12
  const [name, ...valueParts] = pair.split("=");
@@ -41,13 +39,7 @@ export async function POST(request: NextRequest) {
41
 
42
  const value = sessionValue(response.headers.get("set-cookie"));
43
  if (response.ok && value) {
44
- nextResponse.cookies.set(ADMIN_COOKIE, value, {
45
- httpOnly: true,
46
- sameSite: "lax",
47
- secure: request.nextUrl.protocol === "https:",
48
- maxAge: SESSION_MAX_AGE,
49
- path: "/",
50
- });
51
  }
52
 
53
  return nextResponse;
 
1
  import { type NextRequest, NextResponse } from "next/server";
2
+ import { setAdminSessionCookie, ADMIN_COOKIE } from "@/lib/admin-session-cookie";
3
  import { backendUrl } from "@/lib/backend-url";
4
 
5
  export const dynamic = "force-dynamic";
6
  export const runtime = "nodejs";
7
 
 
 
 
8
  function sessionValue(setCookie: string | null) {
9
  const pair = setCookie?.split(";")[0] ?? "";
10
  const [name, ...valueParts] = pair.split("=");
 
39
 
40
  const value = sessionValue(response.headers.get("set-cookie"));
41
  if (response.ok && value) {
42
+ setAdminSessionCookie(nextResponse.headers, value, request.nextUrl.protocol === "https:");
 
 
 
 
 
 
43
  }
44
 
45
  return nextResponse;
frontend/app/api/admin/logout/route.ts CHANGED
@@ -1,11 +1,10 @@
1
  import { type NextRequest, NextResponse } from "next/server";
 
2
  import { backendUrl } from "@/lib/backend-url";
3
 
4
  export const dynamic = "force-dynamic";
5
  export const runtime = "nodejs";
6
 
7
- const ADMIN_COOKIE = "admin_session";
8
-
9
  export async function POST(request: NextRequest) {
10
  let response: Response;
11
  try {
@@ -28,13 +27,7 @@ export async function POST(request: NextRequest) {
28
  "content-type": response.headers.get("content-type") ?? "application/json",
29
  },
30
  });
31
- nextResponse.cookies.set(ADMIN_COOKIE, "", {
32
- httpOnly: true,
33
- sameSite: "lax",
34
- secure: request.nextUrl.protocol === "https:",
35
- maxAge: 0,
36
- path: "/",
37
- });
38
 
39
  return nextResponse;
40
  }
 
1
  import { type NextRequest, NextResponse } from "next/server";
2
+ import { clearAdminSessionCookie } from "@/lib/admin-session-cookie";
3
  import { backendUrl } from "@/lib/backend-url";
4
 
5
  export const dynamic = "force-dynamic";
6
  export const runtime = "nodejs";
7
 
 
 
8
  export async function POST(request: NextRequest) {
9
  let response: Response;
10
  try {
 
27
  "content-type": response.headers.get("content-type") ?? "application/json",
28
  },
29
  });
30
+ clearAdminSessionCookie(nextResponse.headers, request.nextUrl.protocol === "https:");
 
 
 
 
 
 
31
 
32
  return nextResponse;
33
  }
frontend/lib/admin-session-cookie.ts ADDED
@@ -0,0 +1,24 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ export const ADMIN_COOKIE = "admin_session";
2
+ export const SESSION_MAX_AGE = 60 * 60 * 12;
3
+
4
+ function attributes(isHttps: boolean, maxAge: number) {
5
+ const parts = [`Path=/`, `Max-Age=${maxAge}`, "HttpOnly"];
6
+ if (isHttps) {
7
+ parts.push("Secure", "SameSite=None", "Partitioned");
8
+ } else {
9
+ parts.push("SameSite=Lax");
10
+ }
11
+ return parts.join("; ");
12
+ }
13
+
14
+ export function setAdminSessionCookie(headers: Headers, value: string, isHttps: boolean) {
15
+ headers.append(
16
+ "set-cookie",
17
+ `${ADMIN_COOKIE}=${encodeURIComponent(value)}; ${attributes(isHttps, SESSION_MAX_AGE)}`
18
+ );
19
+ }
20
+
21
+ export function clearAdminSessionCookie(headers: Headers, isHttps: boolean) {
22
+ headers.append("set-cookie", `${ADMIN_COOKIE}=; ${attributes(isHttps, 0)}`);
23
+ }
24
+