-
Notifications
You must be signed in to change notification settings - Fork 0
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
Feature/#98 SNS 로그인
- Loading branch information
Showing
7 changed files
with
158 additions
and
2 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
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,44 @@ | ||
import axios from 'axios'; | ||
import { API_PATH } from 'config/constants'; | ||
import { useEffect } from 'react'; | ||
import { useLocation, useNavigate } from 'react-router-dom'; | ||
import { useUpdateAuth } from 'store/auth'; | ||
|
||
export default function GoogleLogin() { | ||
const location = useLocation(); | ||
const updateAuth = useUpdateAuth(); | ||
const navigate = useNavigate(); | ||
const snsLogin = async () => { | ||
try { | ||
const code = location.search.slice(6, location.search.length).split('&')[0]; | ||
const clientId = process.env.REACT_APP_GOOGLE_CLIENT_ID; | ||
const clientSecret = process.env.REACT_APP_GOOGLE_CLIENT_SECRET; | ||
const redirectUri = process.env.REACT_APP_SERVER_LOGIN_REDIRECT_URL; | ||
const requestBody = `code=${code}&client_id=${clientId}&client_secret=${clientSecret}&redirect_uri=${redirectUri}/google&grant_type=authorization_code`; | ||
|
||
const googleResponse = await axios.post('https://oauth2.googleapis.com/token', requestBody); | ||
const googleIdToken = googleResponse.data.id_token; | ||
const googleLogin = await axios.post(`${API_PATH}/login/GOOGLE`, {}, { | ||
headers: { | ||
Authorization: googleIdToken, | ||
}, | ||
}); | ||
sessionStorage.setItem('accessToken', googleLogin.data.accessToken); | ||
localStorage.setItem('refreshToken', googleLogin.data.refreshToken); | ||
updateAuth(); | ||
} catch (e) { | ||
// console.log(e); | ||
} | ||
}; | ||
useEffect(() => { | ||
if (location !== undefined) { | ||
snsLogin(); | ||
} else { | ||
// url 오류로 인한 콜백 | ||
navigate('/login'); | ||
} | ||
}); | ||
return ( | ||
<div /> | ||
); | ||
} |
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,57 @@ | ||
import { useLocation, useNavigate } from 'react-router-dom'; | ||
import { useEffect } from 'react'; | ||
import { API_PATH } from 'config/constants'; | ||
import axios, { AxiosResponse } from 'axios'; | ||
import qs from 'qs'; | ||
import { useUpdateAuth } from 'store/auth'; | ||
|
||
interface KakaoResponse { | ||
access_token: string, | ||
expires_in: number, | ||
refresh_token: string, | ||
refresh_token_expires_in: number, | ||
scope: string, | ||
token_type: string, | ||
} | ||
|
||
export default function KakaoLogin() { | ||
const location = useLocation(); | ||
const updateAuth = useUpdateAuth(); | ||
const navigate = useNavigate(); | ||
const headers = { | ||
'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8', | ||
}; | ||
const data = { | ||
grant_type: 'authorization_code', | ||
client_id: process.env.REACT_APP_KAKAO_CLIENT_ID, | ||
redirect_uri: `${process.env.REACT_APP_SERVER_LOGIN_REDIRECT_URL}/kakao`, | ||
code: location.search.slice(6, location.search.length), | ||
}; | ||
const snsLogin = async () => { | ||
try { | ||
const response: AxiosResponse<KakaoResponse> = await axios.post('https://kauth.kakao.com/oauth/token', qs.stringify(data), { headers }); | ||
const kakaoAccessToken = response.data.access_token; | ||
|
||
const kakaoLogin = await axios.post(`${API_PATH}/login/KAKAO`, {}, { | ||
headers: { | ||
Authorization: kakaoAccessToken, | ||
}, | ||
}); | ||
sessionStorage.setItem('accessToken', kakaoLogin.data.accessToken); | ||
localStorage.setItem('refreshToken', kakaoLogin.data.refreshToken); | ||
await updateAuth(); | ||
} catch { | ||
// | ||
} | ||
}; | ||
useEffect(() => { | ||
if (location !== undefined) { | ||
snsLogin(); | ||
} else { | ||
navigate('/login'); | ||
} | ||
}); | ||
return ( | ||
<div /> | ||
); | ||
} |
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,40 @@ | ||
import axios from 'axios'; | ||
import { API_PATH } from 'config/constants'; | ||
import { useEffect } from 'react'; | ||
import { useLocation, useNavigate } from 'react-router-dom'; | ||
import { useUpdateAuth } from 'store/auth'; | ||
|
||
export default function NaverLogin() { | ||
const location = useLocation(); | ||
const updateAuth = useUpdateAuth(); | ||
const navigate = useNavigate(); | ||
const clientId = process.env.REACT_APP_NAVER_CLIENT_ID; | ||
const clientSecret = process.env.REACT_APP_NAVER_CLIENT_SECRET; | ||
const snsLogin = async () => { | ||
try { | ||
const naverResponse = await axios.post(`/oauth2.0/token?grant_type=authorization_code&client_secret=${clientSecret}&client_id=${clientId}&code=${location.search.split('&')[0].slice(6)}&state=1`); | ||
const headers = { | ||
authorization: naverResponse.data.access_token, | ||
}; | ||
const result = await axios.post(`${API_PATH}/login/NAVER`, {}, { | ||
headers, | ||
}); | ||
sessionStorage.setItem('accessToken', result.data.accessToken); | ||
localStorage.setItem('refreshToken', result.data.refreshToken); | ||
await updateAuth(); | ||
} catch (e) { | ||
// | ||
} | ||
}; | ||
useEffect(() => { | ||
if (location !== undefined) { | ||
snsLogin(); | ||
} else { | ||
// url 오류로 인한 콜백 | ||
navigate('/login'); | ||
} | ||
}); | ||
return ( | ||
<div /> | ||
); | ||
} |
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