Skip to content

Commit d5687e7

Browse files
authored
Merge pull request #84 from ASAP-Lettering/feat/#83
[Feat] middleware로 토큰 유무에 따른 리다이렉트 설정
2 parents c3802fc + 14b7bd1 commit d5687e7

4 files changed

Lines changed: 94 additions & 18 deletions

File tree

src/api/client.tsx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -83,12 +83,12 @@ authClient.interceptors.response.use(
8383
return Promise.reject(refreshError);
8484
}
8585
} else {
86-
const accessToken = getAccessToken();
87-
if (!accessToken) {
88-
window.location.href = "/login";
89-
} else {
90-
//window.location.href = "/error";
91-
}
86+
// const accessToken = getAccessToken();
87+
// if (!accessToken) {
88+
// window.location.href = "/login";
89+
// } else {
90+
//window.location.href = "/error";
91+
// }
9292
}
9393
return Promise.reject(error);
9494
}

src/app/page.tsx

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@ export default function Home() {
1919
const router = useRouter();
2020
const accessToken = getAccessToken();
2121

22-
useEffect(() => {
23-
if (!accessToken) {
24-
router.push("/login");
25-
} else {
26-
router.push("/planet");
27-
}
28-
}, []);
22+
// useEffect(() => {
23+
// if (!accessToken) {
24+
// router.push("/login");
25+
// } else {
26+
// router.push("/planet");
27+
// }
28+
// }, []);
2929

3030
return (
3131
<Container>

src/app/planet/page.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -428,11 +428,11 @@ const PlanetPage = () => {
428428
};
429429

430430
//토큰 유효한지 확인
431-
useEffect(() => {
432-
if (!accessToken) {
433-
router.push("/login");
434-
}
435-
}, []);
431+
// useEffect(() => {
432+
// if (!accessToken) {
433+
// router.push("/login");
434+
// }
435+
// }, []);
436436

437437
return (
438438
<>

src/middleware.ts

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import { NextResponse, type NextRequest } from "next/server";
2+
import { getAccessToken } from "./utils/storage";
3+
4+
export default function middleware(request: NextRequest) {
5+
const { nextUrl, cookies } = request;
6+
const accessToken = cookies.get("lettering-access")?.value;
7+
const { pathname, searchParams } = request.nextUrl;
8+
9+
/* 정적 파일 요청인지 확인 */
10+
if (
11+
request.nextUrl.pathname.startsWith("/images") ||
12+
request.nextUrl.pathname.endsWith(".svg") ||
13+
request.nextUrl.pathname.endsWith(".png") ||
14+
request.nextUrl.pathname === "/manifest.json"
15+
) {
16+
return NextResponse.next();
17+
}
18+
19+
/* /verify/letter 페이지 접근 시 토큰 유무에 따라 처리 */
20+
if (pathname === "/verify/letter") {
21+
const urlParam = searchParams.get("url");
22+
23+
// 액세스 토큰이 없는 경우
24+
if (!accessToken) {
25+
// `url` 파라미터가 있으면 해당 URL로, 없으면 기본 `/login`으로 이동
26+
const redirectUrl = urlParam ? `/login?url=${urlParam}` : "/login";
27+
return NextResponse.redirect(new URL(redirectUrl, request.url));
28+
}
29+
}
30+
31+
if (
32+
request.nextUrl.pathname.startsWith("/error") ||
33+
request.nextUrl.pathname.startsWith("/verify")
34+
) {
35+
return NextResponse.next();
36+
}
37+
38+
/* 로그인 필요 없는 페이지 */
39+
if (
40+
request.nextUrl.pathname === "/login" ||
41+
request.nextUrl.pathname.startsWith("/signup") ||
42+
request.nextUrl.pathname.startsWith("/kakao")
43+
) {
44+
// 로그인 되어 있는 경우 메인 페이지로 리다이렉트
45+
if (accessToken) {
46+
const redirectUrl = "/planet";
47+
return NextResponse.redirect(new URL(redirectUrl, request.url));
48+
} else {
49+
// 로그인이 필요 없는 페이지는 그냥 다음 요청으로 진행
50+
return NextResponse.next();
51+
}
52+
}
53+
54+
/* 로그인 필요한 페이지 */
55+
if (!accessToken) {
56+
// 로그인 페이지로 리다이렉트
57+
return NextResponse.redirect(new URL("/login", request.url));
58+
}
59+
60+
// 로그인 되어 있는 경우 요청 페이지로 진행
61+
return NextResponse.next();
62+
}
63+
64+
export const config = {
65+
matcher: [
66+
/*
67+
* Match all request paths except for the ones starting with:
68+
* - api (API routes)
69+
* - _next/static (static files)
70+
* - _next/image (image optimization files)
71+
* - favicon.ico (favicon file)
72+
* - robots.txt (robots file)
73+
*/
74+
"/((?!api|_next/static|_next/image|favicon.ico|robots.txt|images|manifest.json).*)",
75+
],
76+
};

0 commit comments

Comments
 (0)