Skip to content

Commit

Permalink
fix: login issue (#307)
Browse files Browse the repository at this point in the history
  • Loading branch information
jw-r authored Dec 10, 2024
1 parent 07a4f70 commit b26ad2e
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 7 deletions.
4 changes: 2 additions & 2 deletions src/app/(routes)/profile/account/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
24 changes: 20 additions & 4 deletions src/app/api/auth/[...nextauth]/auth.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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,
})
Expand All @@ -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')
}
}
Expand Down
1 change: 1 addition & 0 deletions src/features/collection/components/detail-info/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ const DetailInfo = ({ id }: Props) => {

<FixedBottom>
<Link
// 바로 이동하지 않고, collection quiz_set_id를 가져오는 api를 실행함
href={`/quiz/${collectionData.id}?quizType=collection&collectionName=${collectionData.name}&collectionEmoji=${collectionData.emoji}`}
>
{/* 이동 /quiz/[id] - searchParams로 collectionId, createdAt, collectionName, collectionEmoji 넣어서 */}
Expand Down
Empty file added src/requests/auth/hooks.ts
Empty file.
24 changes: 24 additions & 0 deletions src/requests/auth/server.ts
Original file line number Diff line number Diff line change
@@ -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<SignInResponse>(API_ENDPOINTS.AUTH.LOGIN, params)
return data
} catch (error) {
throw error
}
}
2 changes: 1 addition & 1 deletion src/requests/user/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<User.Info>(API_ENDPOINTS.USER.GET.INFO)
return data
Expand Down
4 changes: 4 additions & 0 deletions src/shared/lib/axios/http-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.')
}
Expand Down
Empty file added src/types/auth.d.ts
Empty file.

0 comments on commit b26ad2e

Please sign in to comment.