Skip to content

Commit

Permalink
Merge pull request #162 from Donggrina/feature/middleware
Browse files Browse the repository at this point in the history
Feat: middleware 작업 완료
  • Loading branch information
DHyeon98 authored Jun 21, 2024
2 parents f192856 + 8b945ae commit d794c26
Show file tree
Hide file tree
Showing 8 changed files with 22 additions and 12 deletions.
8 changes: 4 additions & 4 deletions api/my/groups/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,18 @@ export default class MyFamilyApi {
}

async myFamilyDelete() {
return axiosInstance.delete(`/my/groups`);
return await axiosInstance.delete(`/my/groups`);
}

async myFamilyModify(formData: NameModifyType) {
return axiosInstance.put(`/my/groups`, formData);
return await axiosInstance.put(`/my/groups`, formData);
}

async myFamilyAddMember(formData: FieldValues) {
return axiosInstance.post(`/my/groups/members`, formData);
return await axiosInstance.post(`/my/groups/members`, formData);
}

async myFamilyDeleteMember(targetId: number) {
return axiosInstance.post(`/my/groups/members/${targetId}`);
return await axiosInstance.post(`/my/groups/members/${targetId}`);
}
}
2 changes: 1 addition & 1 deletion components/common/nav/nav-list/nav-list-item.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { NavListProps } from '../types/nav';
export default function NavListItem({ href, text, RenderSvgComponent }: NavListProps) {
const { pathname } = useRouter();
return (
<li className={pathname.includes(href) ? `${styles.on} ${styles.navList}` : styles.navList}>
<li className={pathname === href ? `${styles.on} ${styles.navList}` : styles.navList}>
<Link href={href}>
<div className={styles.imgBox} aria-label={text}>
<RenderSvgComponent />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export default function DeleteModal({ Modal, handleModal }: DeleteModalType) {
await myFamilyApi.myFamilyDelete();
deleteCookie('accessToken');
deleteCookie('refreshToken');
router.push('/login');
router.push('/');
} catch {
console.log('에러');
}
Expand Down
9 changes: 5 additions & 4 deletions middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ export default function middleware(request: NextRequest) {
const { cookies, nextUrl } = request;
const path = nextUrl.pathname;
const hasCookie = cookies.has('accessToken');
const hasFamily = cookies.has('isFamily');
const hasFamily = cookies.get('isFamily')?.value === 'true';
const isProtectedPage = PROTECTED_PAGES.some((page) => path.startsWith(page));
const isPublicPage = PUBLIC_PAGES.includes(path);
const isNonFamilyPage = NON_FAMILY_PAGES.includes(path);
const isFamilyPage = FAMILY_PAGES.includes(path);
const isFamilyPage = FAMILY_PAGES.some((page) => path.startsWith(page));

// 정적 파일 요청 필터링
if (
Expand Down Expand Up @@ -47,12 +47,13 @@ export default function middleware(request: NextRequest) {
}

// 가족이 있을 때 접근 불가
if (hasFamily && isNonFamilyPage) {
if (hasCookie && hasFamily && isNonFamilyPage) {
return NextResponse.redirect(new URL('/family', request.nextUrl));
}
console.log(isFamilyPage, !hasFamily, hasCookie);

// 가족이 없을 때 접근 불가
if (!hasFamily && isFamilyPage) {
if (hasCookie && !hasFamily && isFamilyPage) {
return NextResponse.redirect(new URL('/start-family', request.nextUrl));
}
return NextResponse.next();
Expand Down
File renamed without changes.
File renamed without changes.
1 change: 1 addition & 0 deletions utils/constants/middleware-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@ export class MiddlewareData {
'/calendar/:path*',
'/growth/:path*',
'/story/:path*',
'/mypage/:path*',
];
}
12 changes: 10 additions & 2 deletions utils/is-nav.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
import { NextRouter } from 'next/router';

export const isNav = (router: NextRouter) => {
const navPaths = ['/calendar', '/family', '/diaries', '/growth', '/story'];
console.log(navPaths.some((path) => router.pathname === path));
const navPaths = [
'/calendar',
'/family',
'/diaries',
'/growth',
'/story',
'/mypage',
'/mypage/pet',
'/mypage/family-management',
];
return navPaths.some((path) => router.pathname === path);
};

0 comments on commit d794c26

Please sign in to comment.