-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
FE-29 🔀 로그인, 간편 로그인 수정 사항 반영 요청 (#140)
* ✨ 네이버 post api 추가 * ✨ 구글 post api 추가 * 🔧 라우팅 수정 * ✨ 네이버 로그인 훅 추가 및 라우팅, 에러 핸들링 구현 * ✨ 구글 로그인 훅 추가 및 라우팅, 에러 핸들링 구현 * ✨ 네이버 간편 로그인 리다이렉트 설정 * ✨ 구글 간편 로그인 리다이렉트 설정 * 🔧 환경 변수 적용: 네이버, 구글, 카카오 로그인 URL 업데이트 * 🔧 가입하기 경로 수정 * 🔧 로그인 훅 라우팅 수정 * 🔧 로그인 훅 에러처리 로직 수정
- Loading branch information
1 parent
c74c7dd
commit f16b7e8
Showing
9 changed files
with
187 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import httpClient from '.'; | ||
|
||
const postGoogleOauth = async (code: string) => { | ||
const response = await httpClient.post('/auth/signIn/GOOGLE', { | ||
redirectUri: process.env.NEXT_PUBLIC_GOOGLE_REDIRECT_URI, | ||
token: code, | ||
}); | ||
return response.data; | ||
}; | ||
|
||
export default postGoogleOauth; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import httpClient from '.'; | ||
|
||
const postNaverOauth = async (code: string, state: string) => { | ||
const response = await httpClient.post('/auth/signIn/NAVER', { | ||
state, | ||
redirectUri: process.env.NEXT_PUBLIC_NAVER_REDIRECT_URI, | ||
token: code, | ||
}); | ||
return response.data; | ||
}; | ||
|
||
export default postNaverOauth; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import postGoogleOauth from '@/apis/postGoogleOauth'; | ||
import { toast } from '@/components/ui/use-toast'; | ||
import { useMutation } from '@tanstack/react-query'; | ||
import { isAxiosError } from 'axios'; | ||
import { useRouter } from 'next/router'; | ||
|
||
const useGoogleLogin = () => { | ||
const router = useRouter(); | ||
|
||
return useMutation({ | ||
mutationFn: async (code: string) => { | ||
const result = await postGoogleOauth(code); | ||
localStorage.setItem('accessToken', result.accessToken); | ||
localStorage.setItem('refreshToken', result.refreshToken); | ||
return result; | ||
}, | ||
onSuccess: () => { | ||
router.push('/epigrams'); | ||
}, | ||
onError: (error) => { | ||
if (isAxiosError(error)) { | ||
const status = error.response?.status; | ||
|
||
if (!status) return; | ||
|
||
if (status === 400) { | ||
toast({ description: '잘못된 요청입니다. 요청을 확인해 주세요.', className: 'bg-state-error text-white font-semibold' }); | ||
router.push('/auth/SignIn'); | ||
return; | ||
} | ||
|
||
if (status >= 500) { | ||
toast({ description: '서버에 문제가 발생했습니다. 잠시 후 다시 시도해 주세요.', className: 'bg-state-error text-white font-semibold' }); | ||
} | ||
} | ||
toast({ description: '알 수 없는 에러가 발생했습니다.', className: 'bg-state-error text-white font-semibold' }); | ||
}, | ||
}); | ||
}; | ||
|
||
export default useGoogleLogin; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import postNaverOauth from '@/apis/postNaverOauth'; | ||
import { toast } from '@/components/ui/use-toast'; | ||
import { useMutation } from '@tanstack/react-query'; | ||
import { isAxiosError } from 'axios'; | ||
import { useRouter } from 'next/router'; | ||
|
||
const useNaverLogin = () => { | ||
const router = useRouter(); | ||
|
||
return useMutation({ | ||
mutationFn: async ({ code, state }: { code: string; state: string }) => { | ||
const result = await postNaverOauth(code, state); | ||
localStorage.setItem('accessToken', result.accessToken); | ||
localStorage.setItem('refreshToken', result.refreshToken); | ||
return result; | ||
}, | ||
onSuccess: () => { | ||
router.push('/epigrams'); | ||
}, | ||
onError: (error) => { | ||
if (isAxiosError(error)) { | ||
const status = error.response?.status; | ||
|
||
if (!status) return; | ||
|
||
if (status === 400) { | ||
toast({ description: '잘못된 요청입니다. 요청을 확인해 주세요.', className: 'bg-state-error text-white font-semibold' }); | ||
router.push('/auth/SignIn'); | ||
return; | ||
} | ||
|
||
if (status >= 500) { | ||
toast({ description: '서버에 문제가 발생했습니다. 잠시 후 다시 시도해 주세요.', className: 'bg-state-error text-white font-semibold' }); | ||
} | ||
} | ||
|
||
toast({ description: '알 수 없는 에러가 발생했습니다.', className: 'bg-state-error text-white font-semibold' }); | ||
}, | ||
}); | ||
}; | ||
|
||
export default useNaverLogin; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { useEffect } from 'react'; | ||
import { useSearchParams } from 'next/navigation'; | ||
import useGoogleLogin from '@/hooks/useGoogleLogin'; | ||
|
||
export default function Google() { | ||
const searchParams = useSearchParams(); | ||
const code = searchParams.get('code'); | ||
const { mutate: login } = useGoogleLogin(); | ||
|
||
useEffect(() => { | ||
if (code) { | ||
login(code); | ||
} else { | ||
/* eslint-disable no-console */ | ||
console.log(code); // code가 없을 때 콘솔에 출력 | ||
} | ||
}, [code, login]); | ||
} | ||
|
||
// code가 없는 경우의 예시 http://localhost:3000/auth/redirect/kakao | ||
// 토스트로 에러 메시지 띄우고, 로그인 페이지로 리다이렉트 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import useNaverLogin from '@/hooks/useNaverLogin'; | ||
import { useSearchParams } from 'next/navigation'; | ||
import { useEffect } from 'react'; | ||
|
||
export default function Naver() { | ||
const searchParams = useSearchParams(); | ||
const code = searchParams.get('code'); | ||
const state = searchParams.get('state'); | ||
const { mutate: login } = useNaverLogin(); | ||
|
||
useEffect(() => { | ||
if (code && state) { | ||
login({ code, state }); | ||
} else { | ||
/* eslint-disable no-console */ | ||
console.log(code, state); // code가 없을 때 콘솔에 출력 | ||
} | ||
}, [code, state, login]); | ||
} |