-
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.
Browse files
Browse the repository at this point in the history
[Feat/#94] 카카오 로그인 기능 구현
- Loading branch information
Showing
9 changed files
with
197 additions
and
1 deletion.
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 |
---|---|---|
|
@@ -11,6 +11,7 @@ node_modules | |
dist | ||
dist-ssr | ||
*.local | ||
.env | ||
|
||
# Editor directories and files | ||
.vscode | ||
|
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,14 @@ | ||
import axios from "axios"; | ||
|
||
export const getData = async (token: string) => { | ||
try { | ||
const res = await axios.get("https://kapi.kakao.com/v2/user/me", { | ||
headers: { | ||
Authorization: `Bearer ${token}`, | ||
}, | ||
}); | ||
return await res.data; | ||
} catch (error) { | ||
console.log(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,25 @@ | ||
import axios from "axios"; | ||
|
||
export const getToken = async (code: string) => { | ||
const grant_type = "authorization_code"; | ||
const REST_API = import.meta.env.VITE_REST_API; | ||
const REDIRECT_URI = "http://localhost:5173/auth"; | ||
const AUTHORIZE_CODE = code; | ||
|
||
try { | ||
const res = await axios.post( | ||
`https://kauth.kakao.com/oauth/token?grant_type=${grant_type}&client_id=${REST_API}&redirect_uri=${REDIRECT_URI}&code=${AUTHORIZE_CODE}`, | ||
{ | ||
headers: { | ||
"Content-type": "application/x-www-form-urlencoded;charset=utf-8", | ||
}, | ||
} | ||
); | ||
|
||
const token = res.data.access_token; | ||
|
||
return token; | ||
} catch (error) { | ||
console.log(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,24 @@ | ||
import styled from "styled-components"; | ||
|
||
import Button from "@components/commons/button/Button"; | ||
|
||
import { requestKakaoLogin } from "@utils/kakaoLogin"; | ||
|
||
// 실제 사용할 때는 원하는 페이지에 button onClick에 requestKakaoLogin만 주면 됨 | ||
|
||
const KakaoLoginTest = () => { | ||
return ( | ||
<Test> | ||
<Button onClick={requestKakaoLogin}>카카오 로그인 테스트~~</Button> | ||
</Test> | ||
); | ||
}; | ||
|
||
export default KakaoLoginTest; | ||
|
||
const Test = styled.div` | ||
width: 37.5rem; | ||
height: 66.7rem; | ||
background-color: white; | ||
`; |
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,43 @@ | ||
import { useEffect } from "react"; | ||
import { useNavigate } from "react-router-dom"; | ||
|
||
import { getToken } from "@apis/kakoLogin/postKakaoToken"; | ||
import { getData } from "@apis/kakoLogin/getKakaoData"; | ||
|
||
const KakaoLogin = () => { | ||
const navigate = useNavigate(); | ||
|
||
const code = new URL(window.location.href).searchParams.get("code"); | ||
|
||
useEffect(() => { | ||
const fetchData = async () => { | ||
if (code) { | ||
// 전에 있던 페이지에 따라 어떤 페이지로 이동할 건지 분기처리 필요 | ||
// 분기 처리 안 되면 이전 페이지로 이동 | ||
navigate("/kakao-login"); | ||
|
||
try { | ||
const token = await getToken(code); | ||
const userData = await getData(token); | ||
|
||
// 이 부분 이메일 + 닉네임 + 인가코드까지 서버에 POST | ||
// 현재는 임시로 console 찍어뒀는데, API POST 붙이기 | ||
|
||
const nickname = userData.properties.nickname; | ||
const email = userData.kakao_account.email; | ||
|
||
console.log(userData); | ||
console.log(`Nickname: ${nickname}, Email: ${email}`); | ||
} catch (error) { | ||
console.error(error); | ||
} | ||
} | ||
}; | ||
|
||
fetchData(); | ||
}, [code, navigate]); | ||
|
||
return <div>로그인 과정 페이지~ </div>; | ||
}; | ||
|
||
export default KakaoLogin; |
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,15 @@ | ||
export const requestKakaoLogin = () => { | ||
const REST_API = import.meta.env.VITE_REST_API; | ||
const REDIRECT_URI = "http://localhost:5173/auth"; | ||
|
||
const kakaoURL = `https://kauth.kakao.com/oauth/authorize?client_id=${REST_API}&redirect_uri=${REDIRECT_URI}&response_type=code`; | ||
|
||
const handleLogin = () => { | ||
const code = new URL(window.location.href).searchParams.get("code"); | ||
window.location.href = kakaoURL; | ||
|
||
return code; | ||
}; | ||
|
||
return handleLogin(); | ||
}; |
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