-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #138 from Team-Lecue/feature/Login
[ Login ] dev 머지!
- Loading branch information
Showing
10 changed files
with
122 additions
and
12 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,31 @@ | ||
import axios from 'axios'; | ||
|
||
import { | ||
KAKAO_BASE_URL, | ||
KAKAO_REDIRECT_URI, | ||
KAKAO_REST_API_KEY, | ||
} from './oAuth'; | ||
|
||
export const getLoginToken = async () => { | ||
const AUTHORIZE_CODE = new URL(window.location.href).searchParams.get('code'); | ||
const GRANT_TYPE = 'authorization_code'; | ||
|
||
if (AUTHORIZE_CODE) { | ||
try { | ||
const response = await axios.post( | ||
`${KAKAO_BASE_URL}/token?grant_type=${GRANT_TYPE}&client_id=${KAKAO_REST_API_KEY}&redirect_uri=${KAKAO_REDIRECT_URI}&code=${AUTHORIZE_CODE}`, | ||
{}, | ||
{ | ||
headers: { | ||
'Content-type': 'application/x-www-form-urlencoded;charset=utf-8', | ||
}, | ||
}, | ||
); | ||
|
||
return response.data.access_token; | ||
} catch (error) { | ||
console.error('로그인 토큰을 가져오는 중 에러 발생:', error); | ||
throw error; | ||
} | ||
} | ||
}; |
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,5 @@ | ||
export const KAKAO_REST_API_KEY = import.meta.env.VITE_KAKAO_API_KEY; | ||
export const KAKAO_REDIRECT_URI = import.meta.env.VITE_KAKAO_REDIRECT_URI; | ||
export const KAKAO_BASE_URL = import.meta.env.VITE_KAKAO_BASE_URL; | ||
|
||
export const KAKAO_AUTH_URL = `${KAKAO_BASE_URL}/authorize?response_type=code&client_id=${KAKAO_REST_API_KEY}&redirect_uri=${KAKAO_REDIRECT_URI}`; |
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 { api } from '../../libs/api'; | ||
|
||
export const postLoginToken = async (token: string) => { | ||
const response = await api.post( | ||
'/api/login', | ||
{ | ||
socialPlatform: 'KAKAO', | ||
}, | ||
{ | ||
headers: { | ||
'Content-Type': 'application/json', | ||
Authorization: `Bearer ${token}`, | ||
}, | ||
}, | ||
); | ||
|
||
return { | ||
nickname: response.data.data.nickname, | ||
tokenDto: response.data.data.tokenDto, | ||
}; | ||
}; |
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,34 @@ | ||
import { useEffect } from 'react'; | ||
import { useNavigate } from 'react-router-dom'; | ||
|
||
import { getLoginToken } from '../../api/getLoginToken'; | ||
import { postLoginToken } from '../../api/postLoginToken'; | ||
|
||
function LoginCallback() { | ||
const navigate = useNavigate(); | ||
|
||
useEffect(() => { | ||
const fetchData = async () => { | ||
try { | ||
const tokenRes = await getLoginToken(); | ||
|
||
const { nickname, tokenDto } = await postLoginToken(tokenRes); | ||
|
||
if (nickname === null) { | ||
navigate('/register', { state: { token: tokenDto.accessToken } }); | ||
} else { | ||
window.localStorage.setItem('token', tokenDto.accessToken); | ||
window.localStorage.setItem('nickname', nickname); | ||
} | ||
} catch (error) { | ||
console.error('로딩-fetchData() 에러 발생:', error); | ||
} | ||
}; | ||
|
||
fetchData(); | ||
}, []); | ||
|
||
return <div>로딩중</div>; | ||
} | ||
|
||
export default LoginCallback; |
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
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