Skip to content

Commit

Permalink
feat: 미들웨어 및 fetch 헤더 설정 테스트
Browse files Browse the repository at this point in the history
  • Loading branch information
ghdtjgus76 committed Aug 13, 2024
1 parent f36bd29 commit 0134cc0
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 11 deletions.
27 changes: 26 additions & 1 deletion apps/client/app/(beforeLogin)/auth/page.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,33 @@
import { css } from "@styled-system/css";
import { fetcher } from "@wow-class/utils";
import LoginButton from "components/LoginButton";
import { apiPath } from "constants/apiPath";
import { tags } from "constants/tags";
import { cookies } from "next/headers";
import Image from "next/image";
import type { DashboardApiResponseDto } from "types/dtos/auth";

const AuthPage = async () => {
const cookieStore = cookies();
const accessToken = cookieStore.get("accessToken")?.value;

const response = await fetcher.get<DashboardApiResponseDto>(
apiPath.dashboard,
{
next: { tags: [tags.dashboard] },
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${accessToken}`,
},
}
);

const memberRole = response.data?.member.role;
const currentRecruitmentOpen =
response.data?.currentRecruitmentRound.period.open;

console.log(memberRole, currentRecruitmentOpen);

const AuthPage = () => {
return (
<main className={mainContentStyle}>
<section className={leftColStyle}>
Expand Down
8 changes: 8 additions & 0 deletions apps/client/app/(beforeLogin)/social-login/redirect/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,22 @@ import { fetcher } from "@wow-class/utils";
import { apiPath } from "constants/apiPath";
import { routePath } from "constants/routePath";
import { tags } from "constants/tags";
import { cookies } from "next/headers";
import { redirect } from "next/navigation";
import type { DashboardApiResponseDto } from "types/dtos/auth";

const AuthServerRedirectPage = async () => {
const cookieStore = cookies();
const accessToken = cookieStore.get("accessToken")?.value;

const response = await fetcher.get<DashboardApiResponseDto>(
apiPath.dashboard,
{
next: { tags: [tags.dashboard] },
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${accessToken}`,
},
}
);

Expand Down
26 changes: 16 additions & 10 deletions apps/client/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,25 @@ import { cookies } from "next/headers";
import type { NextRequest } from "next/server";
import { NextResponse } from "next/server";

export const config = {
matcher: ["/my-page/:path*", "/my-study/:path*", "/study-apply/:path*"],
};
export function middleware(request: NextRequest) {
const requestHeaders = new Headers(request.headers);

const middleware = (req: NextRequest) => {
const cookieStore = cookies();
const accessToken = cookieStore.get("accessToken");
const accessToken = cookieStore.get("accessToken")?.value;

if (!accessToken) {
return NextResponse.redirect(new URL("/auth", req.url));
if (accessToken) {
requestHeaders.set("Authorization", `Bearer ${accessToken}`);
}

return NextResponse.next();
};
requestHeaders.set("Content-Type", "application/json");

export default middleware;
return NextResponse.next({
request: {
headers: requestHeaders,
},
});
}

export const config = {
matcher: ["/my-page/:path*", "/my-study/:path*", "/study-apply/:path*"],
};

0 comments on commit 0134cc0

Please sign in to comment.