-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy.ts
More file actions
57 lines (49 loc) · 2.09 KB
/
Copy pathproxy.ts
File metadata and controls
57 lines (49 loc) · 2.09 KB
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import { NextResponse, type NextRequest } from "next/server";
import { SESSION_COOKIE, verifySessionToken } from "@/lib/auth/session";
import {
LAST_SPOT_TYPE_COOKIE,
LAST_SPOT_TYPE_MAX_AGE,
SPOT_TYPE_PATH_PATTERN,
} from "@/lib/last-spot-type";
export async function proxy(request: NextRequest) {
const token = request.cookies.get(SESSION_COOKIE)?.value;
const session = await verifySessionToken(token);
const isLoginPage = request.nextUrl.pathname.startsWith("/login");
if (!session && !isLoginPage) {
const redirectUrl = request.nextUrl.clone();
redirectUrl.pathname = "/login";
return NextResponse.redirect(redirectUrl);
}
if (session && isLoginPage) {
const redirectUrl = request.nextUrl.clone();
redirectUrl.pathname = "/";
return NextResponse.redirect(redirectUrl);
}
const response = NextResponse.next();
// 最後に開いていたスポット種別を覚えておき、次回起動時(ルート`/`)の
// リダイレクト先に使う(lib/last-spot-type.ts参照)。値が同じ間は再セットしない
const typeKey = request.nextUrl.pathname.match(SPOT_TYPE_PATH_PATTERN)?.[1];
if (
session &&
typeKey &&
request.cookies.get(LAST_SPOT_TYPE_COOKIE)?.value !== typeKey
) {
response.cookies.set(LAST_SPOT_TYPE_COOKIE, typeKey, {
path: "/",
maxAge: LAST_SPOT_TYPE_MAX_AGE,
httpOnly: true,
sameSite: "lax",
});
}
return response;
}
export const config = {
matcher: [
// manifest.webmanifest(app/manifest.ts)を除外しているのは、ブラウザのmanifest取得が
// 既定でCookieなしで行われるため。ガード対象のままだと/loginリダイレクトが返り
// PWAとしてインストールできなくなる。
// .mjsはpublic/maplibre-gl/のワーカースクリプト(lib/maplibre.ts参照)。リダイレクトが
// 返るとHTMLをJSモジュールとして読むことになり地図が動かないため、静的配信に任せる
"/((?!api|_next/static|_next/image|favicon.ico|manifest.webmanifest|.*\\.(?:svg|png|jpg|jpeg|gif|webp|mjs)$).*)",
],
};