Skip to content

Commit

Permalink
feat: 최초 가입자 query params 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
gyeongza committed Jul 9, 2024
1 parent 4a68703 commit f462746
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 23 deletions.
25 changes: 7 additions & 18 deletions src/features/auth/api/oauth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ export const logoutToLoginPage = () => {
window.location.href = '/login?needLogin=true';
};

export const getLoginToken = async (code: string) => {
export const getLoginToken = async (code: string, isFirstUser: string) => {
try {
const response = await instance.get(`${API_BASE_URL}/api/v1/oauth/login/kakao?code=${code}`);
const response = await instance.get(`${API_BASE_URL}/api/v1/oauth/login/kakao?code=${code}&new=${isFirstUser}`);
const jwt = response.headers?.['authorization'] as string;

saveAccessToken(jwt);
Expand All @@ -36,26 +36,15 @@ export const getLoginToken = async (code: string) => {
}
};

export const getNewLoginToken = async (code: string) => {
export const handleLogin = async (code: string, isFirstUser: 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);
await getLoginToken(code, isFirstUser);
} catch (error) {
if (error instanceof AxiosError) {
if (error.status === 400 && error.code === 'ERR_4000') {
if (error.status === 400 && error.code === 'ERR_4010') {
// 최초 로그인 사용자인 경우
await getNewLoginToken(code);
alert('최초 가입자입니다. 회원가입 페이지로 이동합니다.');
window.location.href = '/signup';
} else {
alert('로그인 실패:');
console.error(error);
Expand Down
13 changes: 8 additions & 5 deletions src/features/auth/components/KakaoCallback.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,22 @@ import { Loading } from '@/shared/components/Loading';
export default function KakaoCallback() {
const searchParams = useSearchParams();
const [authCode, setAuthCode] = useState<string | null>(null);
const [isFirstUser, setIsFirstUser] = useState<string | null>(null);
const router = useRouter();
const { toast } = useToast();

useEffect(() => {
const code = searchParams.get('code');
if (code && code !== authCode) {
const 최초가입자인가 = searchParams.get('new');
if (code && code !== authCode && 최초가입자인가 && 최초가입자인가 !== isFirstUser) {
setAuthCode(code);
setIsFirstUser(최초가입자인가);
}
}, [searchParams, authCode]);
}, [searchParams, authCode, isFirstUser]);

useEffect(() => {
if (authCode) {
handleLogin(authCode)
if (authCode && isFirstUser !== null) {
handleLogin(authCode, isFirstUser)
.then(() => {
toast({
title: '로그인 완료!',
Expand All @@ -33,7 +36,7 @@ export default function KakaoCallback() {
router.push('/login');
});
}
}, [authCode, router, toast]);
}, [authCode, router, toast, isFirstUser]);

return (
<div className="flex w-full h-[80vh] justify-center items-center">
Expand Down

0 comments on commit f462746

Please sign in to comment.