From c5989fb6ccfd4640fa6a61e037c4a79423674869 Mon Sep 17 00:00:00 2001 From: dongree Date: Sat, 9 Dec 2023 21:50:58 +0900 Subject: [PATCH] =?UTF-8?q?Fix:=20=EC=83=81=EC=84=B8=ED=8E=98=EC=9D=B4?= =?UTF-8?q?=EC=A7=80=20notFound=20=EC=B2=98=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/app/me/[slug]/page.tsx | 16 ++++++++---- src/app/memos/[slug]/page.tsx | 41 ++++++++++++++++++------------- src/app/questions/[slug]/page.tsx | 17 ++++++++++--- src/app/series/[slug]/page.tsx | 7 ++++++ src/service/auth.ts | 28 ++++++++++++--------- src/service/memos.ts | 5 +--- 6 files changed, 73 insertions(+), 41 deletions(-) diff --git a/src/app/me/[slug]/page.tsx b/src/app/me/[slug]/page.tsx index 38175b57..cddea564 100644 --- a/src/app/me/[slug]/page.tsx +++ b/src/app/me/[slug]/page.tsx @@ -22,6 +22,7 @@ import { getCoverletterInfoByUserId, getCoverletterVisibleMode, } from '@/service/coverletter'; +import { notFound } from 'next/navigation'; type Props = { params: { @@ -35,8 +36,12 @@ export default async function MePage({ params: { slug } }: Props) { const cookie = `${ACCESS_TOKEN}=${accessToken}; ${REFRESH_TOKEN}=${refreshToken}`; const userId = Number(slug); + const userInfo = await getUserInfo(userId, cookie); + if (userInfo.userId === undefined) { + notFound(); + } + const memosData = getMemosByUserId(userId); - const userData = getUserInfo(userId, cookie); const historyData = getHistory( userId, new Date().getFullYear(), @@ -54,7 +59,6 @@ export default async function MePage({ params: { slug } }: Props) { const [ memos, - userInfo, history, { id, isLogin, authority }, tags, @@ -66,7 +70,6 @@ export default async function MePage({ params: { slug } }: Props) { coverletterIsVisible, ] = await Promise.all([ memosData, - userData, historyData, myData, tagData, @@ -137,9 +140,12 @@ export default async function MePage({ params: { slug } }: Props) { ); } -export async function generateMetadata({ params }: Props) { - const userId = Number(params.slug); +export async function generateMetadata({ params: { slug } }: Props) { + const userId = Number(slug); const { nickname } = await getUserInfo(userId); + if (nickname === undefined) { + notFound(); + } return { title: `${nickname} - 인포럼`, diff --git a/src/app/memos/[slug]/page.tsx b/src/app/memos/[slug]/page.tsx index 4b1ba2a4..ad6832cd 100644 --- a/src/app/memos/[slug]/page.tsx +++ b/src/app/memos/[slug]/page.tsx @@ -10,6 +10,7 @@ import { ACCESS_TOKEN, REFRESH_TOKEN } from '@/utils/const'; import { checkUser, getUserInfo } from '@/service/auth'; import { getQuestionsByMemoId } from '@/service/questions'; import { getNotificationsTop30 } from '@/service/notification'; +import { notFound } from 'next/navigation'; type Props = { params: { @@ -23,7 +24,26 @@ export default async function MemoPage({ params: { slug } }: Props) { const cookie = `${ACCESS_TOKEN}=${accessToken}; ${REFRESH_TOKEN}=${refreshToken}`; const memoId = Number(slug); - const memoData = getMemoById(memoId, cookie); + const { + memoTitle, + memoColor, + memoText, + authorId, + likes, + authorName, + authorProfileImagePath, + memoTags, + isMine, + createdDate, + seriesId, + seriesTitle, + authorRank, + } = await getMemoById(memoId, cookie); + + if (memoTitle === undefined) { + notFound(); + } + const likeData = getIsLike('memos', memoId, cookie); const commentData = getCommentsByPostTypeAndPostId('memo', memoId, cookie); const myData = checkUser(cookie); @@ -31,28 +51,12 @@ export default async function MemoPage({ params: { slug } }: Props) { const recommendationsData = getMemoRecommendationsById(memoId); const [ - { - memoTitle, - memoColor, - memoText, - authorId, - likes, - authorName, - authorProfileImagePath, - memoTags, - isMine, - createdDate, - seriesId, - seriesTitle, - authorRank, - }, { isLike }, comments, { id, isLogin, authority }, recommendations, questions, ] = await Promise.all([ - memoData, likeData, commentData, myData, @@ -121,6 +125,9 @@ export default async function MemoPage({ params: { slug } }: Props) { export async function generateMetadata({ params: { slug } }: Props) { const memoId = Number(slug); const { memoTitle, memoDescription } = await getMemoById(memoId); + if (memoTitle === undefined) { + notFound(); + } return { title: memoTitle, diff --git a/src/app/questions/[slug]/page.tsx b/src/app/questions/[slug]/page.tsx index 1c92547c..1b5de97e 100644 --- a/src/app/questions/[slug]/page.tsx +++ b/src/app/questions/[slug]/page.tsx @@ -9,6 +9,7 @@ import { getNotificationsTop30 } from '@/service/notification'; import { getQuestionById } from '@/service/questions'; import { ACCESS_TOKEN, REFRESH_TOKEN } from '@/utils/const'; import { cookies } from 'next/headers'; +import { notFound } from 'next/navigation'; type Props = { params: { @@ -22,13 +23,20 @@ export default async function QuestionPage({ params: { slug } }: Props) { const cookie = `${ACCESS_TOKEN}=${accessToken}; ${REFRESH_TOKEN}=${refreshToken}`; const questionId = Number(slug); - const questionData = getQuestionById(questionId, cookie); + const question = await getQuestionById(questionId, cookie); + if (question.id === undefined) { + notFound(); + } + const answersData = getAnswersByQuestionId(questionId, cookie); const myData = checkUser(cookie); const likeData = getIsLike('questions', questionId, cookie); - const [question, answers, { id, isLogin, authority }, { isLike }] = - await Promise.all([questionData, answersData, myData, likeData]); + const [answers, { id, isLogin, authority }, { isLike }] = await Promise.all([ + answersData, + myData, + likeData, + ]); const { profileImageFilePath } = isLogin ? await getUserInfo(id) @@ -68,6 +76,9 @@ export default async function QuestionPage({ params: { slug } }: Props) { export async function generateMetadata({ params: { slug } }: Props) { const questionId = Number(slug); const { title, description } = await getQuestionById(questionId); + if (title === undefined) { + notFound(); + } return { title: title, diff --git a/src/app/series/[slug]/page.tsx b/src/app/series/[slug]/page.tsx index 20b06aa1..086ce115 100644 --- a/src/app/series/[slug]/page.tsx +++ b/src/app/series/[slug]/page.tsx @@ -10,6 +10,7 @@ import { getQuestionsByMemoId } from '@/service/questions'; import { getSeriesById } from '@/service/series'; import { ACCESS_TOKEN, REFRESH_TOKEN } from '@/utils/const'; import { cookies } from 'next/headers'; +import { notFound } from 'next/navigation'; type Props = { params: { @@ -24,6 +25,9 @@ export default async function SeriesDetailPage({ params: { slug } }: Props) { const seriesId = Number(slug); const { memoInfoList, likes, title } = await getSeriesById(seriesId); + if (title === undefined) { + notFound(); + } const memoData = getMemoById(memoInfoList[0].id, cookie); const likeData = getIsLike('series', seriesId, cookie); @@ -98,6 +102,9 @@ export default async function SeriesDetailPage({ params: { slug } }: Props) { export async function generateMetadata({ params: { slug } }: Props) { const seriesId = Number(slug); const { title, description } = await getSeriesById(seriesId); + if (title === undefined) { + notFound(); + } return { title: title, diff --git a/src/service/auth.ts b/src/service/auth.ts index 9c629638..3cf247fb 100644 --- a/src/service/auth.ts +++ b/src/service/auth.ts @@ -11,7 +11,9 @@ import { } from '@/types'; import { URLSearchParams } from 'next/dist/compiled/@edge-runtime/primitives/url'; -export async function userSignUp(userData: UserSignUpData): Promise { +export async function userSignUp( + userData: UserSignUpData +): Promise { return fetch(`${process.env.NEXT_PUBLIC_SERVER_IP_ADDRESS_SECURE}/users`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, @@ -27,13 +29,18 @@ export async function userSignUp(userData: UserSignUpData): Promise { - return fetch(`${process.env.NEXT_PUBLIC_SERVER_IP_ADDRESS_SECURE}/users/employer`, { - method: 'POST', - headers: { 'Content-Type': 'application/json' }, - credentials: 'include', - body: JSON.stringify(userData), - }) +export async function employerSignUp( + userData: EmployerSignUpData +): Promise { + return fetch( + `${process.env.NEXT_PUBLIC_SERVER_IP_ADDRESS_SECURE}/users/employer`, + { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + credentials: 'include', + body: JSON.stringify(userData), + } + ) .then((res) => { if (!res.ok) { throw new Error('error'); @@ -211,10 +218,7 @@ export async function getUserInfo( credentials: 'include', } ) - .then((res) => { - if (!res.ok) throw new Error('error 발생!'); - return res.json(); - }) + .then((res) => res.json()) .catch(console.error); } diff --git a/src/service/memos.ts b/src/service/memos.ts index 9e0c00e4..f5d64451 100644 --- a/src/service/memos.ts +++ b/src/service/memos.ts @@ -56,10 +56,7 @@ export async function getMemoById( credentials: 'include', } ) - .then((res) => { - if (!res.ok) throw new Error('error'); - return res.json(); - }) + .then((res) => res.json()) .catch(console.error); }