Skip to content

Commit df6b84f

Browse files
committed
middleware 도입
1 parent 2394803 commit df6b84f

File tree

2 files changed

+30
-14
lines changed

2 files changed

+30
-14
lines changed

frontend/zicdding-class.com/app/(user)/my/_components/MyInfo.tsx

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,6 @@ import { useRouter } from 'next/navigation';
77
import { useEffect } from 'react';
88
import { useForm } from 'react-hook-form';
99

10-
function useLoginPageGuard(nextPage: string) {
11-
const { isLogged } = useUser();
12-
const router = useRouter();
13-
14-
useEffect(() => {
15-
if (!isLogged) {
16-
router.push(nextPage);
17-
}
18-
}, [isLogged, router, nextPage]);
19-
}
20-
2110
export function MyInfo({ mode }: { mode: 'view' | 'modify' }) {
2211
const router = useRouter();
2312
const { user } = useUser();
@@ -38,8 +27,6 @@ export function MyInfo({ mode }: { mode: 'view' | 'modify' }) {
3827
setValue('phoneNumber', user.phone_num);
3928
}, [user, setValue]);
4029

41-
useLoginPageGuard('/login');
42-
4330
return (
4431
<div className="space-y-4 w-[420px] mx-auto my-0">
4532
<div className="w-[200px] flex justify-center ml-auto mr-auto mb-8 flex-col">
@@ -100,7 +87,12 @@ export function MyInfo({ mode }: { mode: 'view' | 'modify' }) {
10087
</Button>
10188
</div>
10289
) : (
103-
<Button className="w-full rounded-[20px]" onClick={() => router.push('/my/modify')}>
90+
<Button
91+
className="w-full rounded-[20px]"
92+
onClick={() => {
93+
router.push('/my/modify');
94+
}}
95+
>
10496
개인정보 수정
10597
</Button>
10698
)}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { NextResponse } from 'next/server';
2+
import type { NextRequest } from 'next/server';
3+
import { apiV1 } from './app/_remotes';
4+
5+
async function isAuth() {
6+
try {
7+
await apiV1.users.getMe();
8+
return true;
9+
} catch {
10+
return false;
11+
}
12+
}
13+
export async function middleware(request: NextRequest) {
14+
if (!(await isAuth())) {
15+
return NextResponse.redirect(new URL('/login', request.url));
16+
}
17+
18+
return NextResponse.next();
19+
}
20+
21+
// 미들웨어를 적용할 경로를 지정
22+
export const config = {
23+
matcher: ['/my/:path*'],
24+
};

0 commit comments

Comments
 (0)