From b26ad2e302254e6b00d7d770b929f0027eca27e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EB=A5=98=EC=A0=95=EC=9A=B0?= <88191233+jw-r@users.noreply.github.com> Date: Tue, 10 Dec 2024 23:33:34 +0900 Subject: [PATCH] fix: login issue (#307) --- src/app/(routes)/profile/account/page.tsx | 4 ++-- src/app/api/auth/[...nextauth]/auth.ts | 24 +++++++++++++++---- .../components/detail-info/index.tsx | 1 + src/requests/auth/hooks.ts | 0 src/requests/auth/server.ts | 24 +++++++++++++++++++ src/requests/user/server.ts | 2 +- src/shared/lib/axios/http-server.ts | 4 ++++ src/types/auth.d.ts | 0 8 files changed, 52 insertions(+), 7 deletions(-) create mode 100644 src/requests/auth/hooks.ts create mode 100644 src/requests/auth/server.ts create mode 100644 src/types/auth.d.ts diff --git a/src/app/(routes)/profile/account/page.tsx b/src/app/(routes)/profile/account/page.tsx index 58876d0b..c1163b23 100644 --- a/src/app/(routes)/profile/account/page.tsx +++ b/src/app/(routes)/profile/account/page.tsx @@ -4,10 +4,10 @@ import { cn } from '@/shared/lib/utils' import CategoryDrawer from '@/features/user/components/category-drawer' import SetNameDialog from '@/features/user/components/set-name-dialog' import Link from 'next/link' -import { fetchUserInfo } from '@/requests/user/server' +import { getUserInfo } from '@/requests/user/server' const AccountPage = async () => { - const user = await fetchUserInfo() + const user = await getUserInfo() const interestCategories = user.interestCategories?.length ? user.interestCategories diff --git a/src/app/api/auth/[...nextauth]/auth.ts b/src/app/api/auth/[...nextauth]/auth.ts index 116a608a..ff41dfb5 100644 --- a/src/app/api/auth/[...nextauth]/auth.ts +++ b/src/app/api/auth/[...nextauth]/auth.ts @@ -1,9 +1,9 @@ import NextAuth, { Account, DefaultSession, NextAuthResult } from 'next-auth' -import { signIn as signInAPI } from '@/actions/fetchers/auth/sign-in' import Kakao from 'next-auth/providers/kakao' import Google from 'next-auth/providers/google' import { getUser } from '@/actions/fetchers/user/get-user' import { UserDTO } from '@/actions/types/dto/user.dto' +import { signIn as signInApi } from '@/requests/auth/server' declare module 'next-auth' { interface Session { @@ -28,7 +28,7 @@ export const { jwt: async ({ token, account, trigger }) => { if (account) { try { - const { accessToken, accessTokenExpiration, signUp } = await signInAPI({ + const { accessToken, accessTokenExpiration, signUp } = await signInApi({ socialPlatform: account.provider.toUpperCase() as 'GOOGLE' | 'KAKAO', accessToken: account.access_token as string, }) @@ -41,9 +41,25 @@ export const { } // 회원가입 했을 때만 첫 사용자인지 알 수 있다 try { - const user = await getUser(token.accessToken as string) - token.userDTO = user + // httpServer를 사용할 수 없음. 그 안에서 auth 를 통해 session을 호출하는데, 현 시점에서는 session이 없음 + const response = await fetch(process.env.NEXT_PUBLIC_DEV_API_URL + '/members/info', { + headers: { + Authorization: `Bearer ${token.accessToken as string}`, + 'Content-Type': 'application/json', + }, + cache: 'no-store', + next: { revalidate: 0 }, + }) + + if (!response.ok) { + throw new Error(`Failed to fetch user info: ${response.status}`) + } + + const data = (await response.json()) as UserDTO + + token.userDTO = data } catch (error) { + console.error('Error fetching user info:', error) throw new Error('Failed to get user') } } diff --git a/src/features/collection/components/detail-info/index.tsx b/src/features/collection/components/detail-info/index.tsx index 789a4eee..bdd74d39 100644 --- a/src/features/collection/components/detail-info/index.tsx +++ b/src/features/collection/components/detail-info/index.tsx @@ -99,6 +99,7 @@ const DetailInfo = ({ id }: Props) => { {/* 이동 /quiz/[id] - searchParams로 collectionId, createdAt, collectionName, collectionEmoji 넣어서 */} diff --git a/src/requests/auth/hooks.ts b/src/requests/auth/hooks.ts new file mode 100644 index 00000000..e69de29b diff --git a/src/requests/auth/server.ts b/src/requests/auth/server.ts new file mode 100644 index 00000000..fdbb29c0 --- /dev/null +++ b/src/requests/auth/server.ts @@ -0,0 +1,24 @@ +'use server' + +import { API_ENDPOINTS } from '@/shared/configs/endpoint' +import { httpServer } from '@/shared/lib/axios/http-server' + +interface SignInParams { + socialPlatform: 'KAKAO' | 'GOOGLE' + accessToken: string +} + +interface SignInResponse { + accessToken: string + accessTokenExpiration: string + signUp: boolean +} + +export const signIn = async (params: SignInParams) => { + try { + const { data } = await httpServer.post(API_ENDPOINTS.AUTH.LOGIN, params) + return data + } catch (error) { + throw error + } +} diff --git a/src/requests/user/server.ts b/src/requests/user/server.ts index ff29174d..d4155d0f 100644 --- a/src/requests/user/server.ts +++ b/src/requests/user/server.ts @@ -3,7 +3,7 @@ import { API_ENDPOINTS } from '@/shared/configs/endpoint' import { httpServer } from '@/shared/lib/axios/http-server' -export const fetchUserInfo = async () => { +export const getUserInfo = async () => { try { const { data } = await httpServer.get(API_ENDPOINTS.USER.GET.INFO) return data diff --git a/src/shared/lib/axios/http-server.ts b/src/shared/lib/axios/http-server.ts index 34026b40..7cd69fc8 100644 --- a/src/shared/lib/axios/http-server.ts +++ b/src/shared/lib/axios/http-server.ts @@ -11,6 +11,10 @@ export const httpServer = axios.create({ httpServer.interceptors.request.use( async (config) => { + if (config.url === '/login') { + return config + } + if (typeof window !== 'undefined') { throw new Error('httpServer should only be used in server-side code.') } diff --git a/src/types/auth.d.ts b/src/types/auth.d.ts new file mode 100644 index 00000000..e69de29b