forked from FRONT-END-BOOTCAMP-PLUS-3/Rolly
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleware.ts
More file actions
49 lines (38 loc) ยท 1.45 KB
/
middleware.ts
File metadata and controls
49 lines (38 loc) ยท 1.45 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
import { NextResponse } from "next/server";
import type { NextRequest } from "next/server";
import { createClient } from "@supabase/supabase-js";
export async function middleware(req: NextRequest) {
const token = req.cookies.get("supabase_auth_token")?.value || "";
const { pathname } = req.nextUrl;
let isLoggedIn = false;
if (token) {
const supabase = createClient(
process.env.NEXT_PUBLIC_SUPABASE_URL!,
process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY!,
{ global: { headers: { Authorization: `Bearer ${token}` } } }
);
const { data } = await supabase.auth.getUser();
isLoggedIn = !!data.user;
}
// ๊ฒฝ๋ก์ ํ์ฅ์๊ฐ ์์ผ๋ฉด `true` ์์ผ๋ฉด `null`
const isFileRequest = pathname.match(/\.\w+$/);
// ๊ฒฝ๋ก์ ํ์ฅ์๊ฐ ์๋ค๋ฉด ๋ฏธ๋ค์จ์ด ๋ก์ง ์คํต
if (isFileRequest) {
return NextResponse.next();
}
// ๋ก๊ทธ์ธํ์ง ์์ ์ฌ์ฉ์๊ฐ ๋ณดํธ๋ ํ์ด์ง ์ ๊ทผ ์ ์ฐจ๋จ
if (!isLoggedIn && pathname.startsWith("/member")) {
return NextResponse.redirect(new URL("/", req.url));
}
// ๋ก๊ทธ์ธ๋ ์ฌ์ฉ์๊ฐ / ๊ฒฝ๋ก์ ์ ๊ทผํ๋ฉด /member๋ก ๋ฆฌ๋๋ ์
if (isLoggedIn && pathname === "/") {
return NextResponse.redirect(new URL("/member", req.url));
}
if (isLoggedIn && pathname.startsWith("/rollies")) {
return NextResponse.redirect(new URL(`/member${pathname}`, req.url));
}
return NextResponse.next();
}
export const config = {
matcher: ["/:path*"],
};