Skip to content

Commit

Permalink
feat: 최초가입자, 기존가입자 분기처리
Browse files Browse the repository at this point in the history
  • Loading branch information
gyeongza committed Jul 9, 2024
1 parent 2821d1b commit 4a68703
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 5 deletions.
30 changes: 30 additions & 0 deletions src/features/auth/api/oauth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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();
Expand Down
4 changes: 2 additions & 2 deletions src/features/auth/components/KakaoCallback.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -21,7 +21,7 @@ export default function KakaoCallback() {

useEffect(() => {
if (authCode) {
getLoginToken(authCode)
handleLogin(authCode)
.then(() => {
toast({
title: '로그인 완료!',
Expand Down
12 changes: 10 additions & 2 deletions src/features/auth/components/signup/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<string[]>(['010', '', '']);
const [isValid, setIsValid] = useState<boolean>(false);
const [error, setError] = useState<string>('');
const router = useRouter();

const inputRefs = [useRef<HTMLInputElement>(null), useRef<HTMLInputElement>(null)];

Expand Down Expand Up @@ -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('유효한 전화번호를 입력해주세요.');
}
Expand Down
1 change: 0 additions & 1 deletion src/shared/api/axios/instance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ instance.interceptors.response.use(
},
async function (error) {
const {
config,
response: { status },
} = error;

Expand Down

0 comments on commit 4a68703

Please sign in to comment.