Skip to content

Commit

Permalink
Merge pull request #142 from TripInfoWeb/dev_informations
Browse files Browse the repository at this point in the history
Feature: Next.js 미들웨어 사용
  • Loading branch information
HyunJinNo authored Jul 27, 2024
2 parents 85a56a5 + 24fa8fe commit 4533adc
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 4 deletions.
2 changes: 2 additions & 0 deletions src/components/informations/list/ChildCategoryList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const ChildCategoryList = ({
"rounded-full border-[0.0625rem] border-[#E9EBED] px-3 py-[0.375rem] text-sm font-medium hover:scale-105"
}
href={`/informations/list/parent-category/${parentCategoryId}?page=1`}
scroll={false}
>
전체
</Link>
Expand All @@ -34,6 +35,7 @@ const ChildCategoryList = ({
"rounded-full border-[0.0625rem] border-[#E9EBED] px-3 py-[0.375rem] text-sm font-medium hover:scale-105"
}
href={`/informations/list/child-category/${childCategory.id}?page=1`}
scroll={false}
>
{childCategory.name}
</Link>
Expand Down
3 changes: 3 additions & 0 deletions src/components/informations/list/InformationSearch.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,18 +61,21 @@ const InformationSearch = ({
<Link
className={`${order === "latest" && "text-main"} hover:text-main`}
href={`${pathname}?page=1&order=latest`}
scroll={false}
>
최신순
</Link>
<Link
className={`${order === "like-count" && "text-main"} hover:text-main`}
href={`${pathname}?page=1&order=like-count`}
scroll={false}
>
좋아요순
</Link>
<Link
className={`${order === "view-count" && "text-main"} hover:text-main`}
href={`${pathname}?page=1&order=view-count`}
scroll={false}
>
조회순
</Link>
Expand Down
1 change: 1 addition & 0 deletions src/components/informations/list/ParentCategoryList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const ParentCategoryList = ({ categories, parentCategoryId }: Props) => {
} ` + "pb-[0.375rem] hover:text-main"
}
href={`/informations/list/parent-category/${category.id}?page=1`}
scroll={false}
>
{category.name}
</Link>
Expand Down
11 changes: 8 additions & 3 deletions src/containers/common/HeaderContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,18 @@ const HeaderContainer = () => {

const logoutHandler = async () => {
// api로 로그아웃 요청해서 쿠키제거
const response = await fetch("/api/auth/logout", {
method: "POST",
});

if (!response.ok) {
throw new Error(response.statusText);
}

authStore.initialize();
authStore.setUser({
id: -1,
});
await fetch("/api/auth/logout", {
method: "POST",
});
};

useEffect(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const InformationFilterModalContainer = ({ closeModal }: Props) => {
const pathname = usePathname();

const onClick = () => {
router.push(`${pathname}?page=1&place=${place}`);
router.push(`${pathname}?page=1&place=${place}`, { scroll: false });
closeModal();
};

Expand Down
38 changes: 38 additions & 0 deletions src/middleware.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { NextRequest, NextResponse } from "next/server";

// This function can be marked "async" if using "await" inside
export function middleware(request: NextRequest) {
const { pathname } = request.nextUrl;

// 사용자가 인증되었는지 확인하고, 인증되지 않은 사용자는
// 로그인 페이지로 리다이렉트합니다.
if (
pathname.startsWith("/informations/write") ||
pathname.startsWith("/informations/edit")
) {
const token = request.cookies.get("access_token");

// 토큰이 없는 경우 로그인 페이지로 리다이렉트합니다.
if (!token) {
return NextResponse.redirect(new URL("/auth/signin", request.url));
}
}

// 이미 로그인된 사용자가 로그인 페이지에 접속하려고 하면
// 메인 페이지로 리다이렉트합니다.
if (pathname.startsWith("/auth")) {
const token = request.cookies.get("access_token");

// 토큰이 있는 경우 메인 페이지로 리다이렉트합니다.
if (token) {
return NextResponse.redirect(new URL("/", request.url));
}
}

return NextResponse.next();
}

// 미들웨어를 적용할 경로를 설정합니다.
export const config = {
matchers: ["/informations/write", "/informations/edit", "auth/:path*"],
};

0 comments on commit 4533adc

Please sign in to comment.