From 4a68703a8e46f51b0f2642f900ef93fbf2935d5e Mon Sep 17 00:00:00 2001 From: gyeongza Date: Tue, 9 Jul 2024 17:28:38 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20=EC=B5=9C=EC=B4=88=EA=B0=80=EC=9E=85?= =?UTF-8?q?=EC=9E=90,=20=EA=B8=B0=EC=A1=B4=EA=B0=80=EC=9E=85=EC=9E=90=20?= =?UTF-8?q?=EB=B6=84=EA=B8=B0=EC=B2=98=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/features/auth/api/oauth.ts | 30 +++++++++++++++++++ .../auth/components/KakaoCallback.tsx | 4 +-- src/features/auth/components/signup/index.tsx | 12 ++++++-- src/shared/api/axios/instance.ts | 1 - 4 files changed, 42 insertions(+), 5 deletions(-) diff --git a/src/features/auth/api/oauth.ts b/src/features/auth/api/oauth.ts index b339787..f38cd68 100644 --- a/src/features/auth/api/oauth.ts +++ b/src/features/auth/api/oauth.ts @@ -2,6 +2,7 @@ import { ACCESS_TOKEN_LOCAL_STORAGE_KEY } from '../constants'; import LocalStorage from '@/shared/utils/localStorage'; import instance from '@/shared/api/axios/instance'; import { API_BASE_URL } from '@/shared/api/constants'; +import { AxiosError } from 'axios'; export const getAccessToken = () => LocalStorage.getItem(ACCESS_TOKEN_LOCAL_STORAGE_KEY); @@ -35,6 +36,35 @@ export const getLoginToken = async (code: string) => { } }; +export const getNewLoginToken = async (code: string) => { + try { + const response = await instance.get(`${API_BASE_URL}/api/v1/oauth/login/new/kakao?code=${code}`); + const jwt = response.headers?.['authorization'] as string; + + saveAccessToken(jwt); + } catch (error) { + alert('로그인 실패:'); + console.error(error); + } +}; + +export const handleLogin = async (code: string) => { + try { + await getLoginToken(code); + } catch (error) { + if (error instanceof AxiosError) { + if (error.status === 400 && error.code === 'ERR_4000') { + // 최초 로그인 사용자인 경우 + await getNewLoginToken(code); + } else { + alert('로그인 실패:'); + console.error(error); + throw error; + } + } + } +}; + export const saveAccessToken = (response: string) => { if (!response) { removeAccessToken(); diff --git a/src/features/auth/components/KakaoCallback.tsx b/src/features/auth/components/KakaoCallback.tsx index 400c85d..fedd7b9 100644 --- a/src/features/auth/components/KakaoCallback.tsx +++ b/src/features/auth/components/KakaoCallback.tsx @@ -3,7 +3,7 @@ import { useState, useEffect } from 'react'; import { useRouter, useSearchParams } from 'next/navigation'; import { useToast } from '@/shared/components/shadcn/ui/use-toast'; -import { getLoginToken } from '@/features/auth/api/oauth'; +import { handleLogin } from '@/features/auth/api/oauth'; import { Loading } from '@/shared/components/Loading'; export default function KakaoCallback() { @@ -21,7 +21,7 @@ export default function KakaoCallback() { useEffect(() => { if (authCode) { - getLoginToken(authCode) + handleLogin(authCode) .then(() => { toast({ title: '로그인 완료!', diff --git a/src/features/auth/components/signup/index.tsx b/src/features/auth/components/signup/index.tsx index 958beed..cb557e5 100644 --- a/src/features/auth/components/signup/index.tsx +++ b/src/features/auth/components/signup/index.tsx @@ -6,11 +6,14 @@ import { Input } from '@/shared/components/shadcn/ui/input'; import { Label } from '@/shared/components/shadcn/ui/label'; import { ChangeEvent, FormEvent, KeyboardEvent, useRef, useState } from 'react'; import { getSignupStatus } from '../../api/getSignupStatus'; +import { API_BASE_URL } from '@/shared/api/constants'; +import { useRouter } from 'next/navigation'; export default function Signup() { const [phoneNumber, setPhoneNumber] = useState(['010', '', '']); const [isValid, setIsValid] = useState(false); const [error, setError] = useState(''); + const router = useRouter(); const inputRefs = [useRef(null), useRef(null)]; @@ -52,8 +55,13 @@ export default function Signup() { const response = await getSignupStatus(fullNumber); const 회원가입된유저인가 = response.data.isDuplicatedPhoneNumber; - console.log(회원가입된유저인가); - // 여기에 서버로 데이터를 보내는 로직을 추가할 수 있습니다. + if (회원가입된유저인가) { + alert('이미 가입된 회원입니다. 카카오 로그인을 통해 로그인해주세요.'); + router.push('/login'); + } else { + alert('최초 가입자입니다. 카카오 로그인으로 이동합니다.'); + window.location.href = `${API_BASE_URL}/api/v1/oauth/kakao`; + } } else { setError('유효한 전화번호를 입력해주세요.'); } diff --git a/src/shared/api/axios/instance.ts b/src/shared/api/axios/instance.ts index c75b195..f08fa11 100644 --- a/src/shared/api/axios/instance.ts +++ b/src/shared/api/axios/instance.ts @@ -29,7 +29,6 @@ instance.interceptors.response.use( }, async function (error) { const { - config, response: { status }, } = error;