|
1 | 1 | //카카오 인증 완료 후 인증 코드는 승인된 리디렉트 URL로 |
2 | 2 |
|
3 | | -import React, { useEffect } from "react"; |
4 | | -import { useNavigate } from "react-router-dom"; |
5 | | -import axios from "axios"; |
6 | | -import { UserInfoDto } from "../../../ProfileViewer/ResponseDto/UserInfoDto"; |
7 | | -import request from "../../../../apis/core"; |
8 | | -import Loading from "../../../../components/Loading"; |
| 3 | +import React, { useEffect } from 'react'; |
| 4 | +import { useNavigate } from 'react-router-dom'; |
| 5 | +import axios from 'axios'; |
| 6 | +import { UserInfoDto } from '../../../ProfileViewer/ResponseDto/UserInfoDto'; |
| 7 | +import request from '../../../../apis/core'; |
| 8 | +import Loading from '../../../../components/Loading'; |
9 | 9 |
|
10 | 10 | const KakaoCallback: React.FC = () => { |
11 | | - const navigate = useNavigate(); |
| 11 | + const navigate = useNavigate(); |
12 | 12 |
|
13 | | - useEffect(()=>{ |
| 13 | + useEffect(() => { |
| 14 | + const code = new URL(window.location.href).searchParams.get('code'); // URL에서 인증 코드 추출 |
| 15 | + console.log(code); // 인증 코드 출력 |
14 | 16 |
|
15 | | - const code = new URL(window.location.href).searchParams.get("code"); // URL에서 인증 코드 추출 |
16 | | - console.log(code); // 인증 코드 출력 |
17 | | - |
18 | | - if (code) { |
19 | | - // 인증 코드를 쿼리스트링으로 백엔드 서버에 전송 |
20 | | - axios.get(`https://api-dev.oodd.today/auth/login/kakao?code=${code}`) |
21 | | - .then(response => { |
22 | | - const statusCode = response.status; // 200 OK |
23 | | - console.log(JSON.stringify(response.data)) |
24 | | - if (statusCode === 200) { // 추후 Postman에서 api 호출해 보고 응답을 보고 적어야 함 |
25 | | - // userid를 서버로 보내 해당 유저의 nickname 유무에 따른 리디렉션 |
26 | | - const token = response.data.accessToken; |
27 | | - |
28 | | - localStorage.setItem('id', response.data.id); // 응답으로 id가 오지 않기 때문에 여기서 설정해야 함 수정 필요? |
29 | | - localStorage.removeItem('jwt_token'); |
30 | | - localStorage.setItem('jwt_token', token); |
31 | | - const userid = localStorage.getItem('id'); |
| 17 | + if (code) { |
| 18 | + // 인증 코드를 쿼리스트링으로 백엔드 서버에 전송 |
| 19 | + axios |
| 20 | + .get(`https://api-dev.oodd.today/auth/login/kakao?code=${code}`) |
| 21 | + .then((response) => { |
| 22 | + const statusCode = response.status; // 200 OK |
| 23 | + console.log(JSON.stringify(response.data)); |
| 24 | + if (statusCode === 200) { |
| 25 | + // 추후 Postman에서 api 호출해 보고 응답을 보고 적어야 함 |
| 26 | + // userid를 서버로 보내 해당 유저의 nickname 유무에 따른 리디렉션 |
| 27 | + const token = response.data.accessToken; |
32 | 28 |
|
33 | | - request.get<UserInfoDto>(`/users/${userid}`) |
34 | | - .then(response => { |
35 | | - console.log(response); |
36 | | - if(response.result.nickname){ |
37 | | - navigate('/'); |
38 | | - } |
39 | | - else{ |
40 | | - navigate(`/signup`) |
41 | | - } |
42 | | - }) |
43 | | - .catch(error => { |
44 | | - // API 요청 실패 시 처리 |
45 | | - console.error('API 요청 실패:', error); |
46 | | - alert('사용자 정보를 불러오지 못했습니다.'); |
47 | | - navigate('/login'); // 실패 시 로그인 페이지로 리디렉션 |
48 | | - }); |
49 | | - } else { |
50 | | - console.error('로그인 실패:', response.data); |
51 | | - alert('카카오 계정의 정보를 불러오지 못했습니다.'); |
52 | | - navigate('/login'); |
53 | | - // 로그인 실패 시 처리 (예: 오류 페이지로 리디렉션) |
54 | | - } |
55 | | - }) |
56 | | - .catch(error => { |
57 | | - console.error('서버 요청 실패:', error); |
58 | | - }); |
59 | | - } else { |
60 | | - // 인증 코드가 없는 경우 처리 |
61 | | - console.error('인증 코드가 없습니다.'); |
62 | | - } |
63 | | - }, [navigate]); |
64 | | - return( |
65 | | - <Loading/> |
66 | | - ) |
| 29 | + localStorage.setItem('id', response.data.id); // 응답으로 id가 오지 않기 때문에 여기서 설정해야 함 수정 필요? |
| 30 | + localStorage.removeItem('jwt_token'); |
| 31 | + localStorage.setItem('jwt_token', token); |
| 32 | + const userid = localStorage.getItem('id'); |
| 33 | + |
| 34 | + request |
| 35 | + .get<UserInfoDto>(`/users/${userid}`) |
| 36 | + .then((response) => { |
| 37 | + console.log(response); |
| 38 | + if (response.result.nickname) { |
| 39 | + navigate('/'); |
| 40 | + } else { |
| 41 | + navigate(`/signup`); |
| 42 | + } |
| 43 | + }) |
| 44 | + .catch((error) => { |
| 45 | + // API 요청 실패 시 처리 |
| 46 | + console.error('API 요청 실패:', error); |
| 47 | + alert('사용자 정보를 불러오지 못했습니다.'); |
| 48 | + navigate('/login'); // 실패 시 로그인 페이지로 리디렉션 |
| 49 | + }); |
| 50 | + } else { |
| 51 | + console.error('로그인 실패:', response.data); |
| 52 | + alert('카카오 계정의 정보를 불러오지 못했습니다.'); |
| 53 | + navigate('/login'); |
| 54 | + // 로그인 실패 시 처리 (예: 오류 페이지로 리디렉션) |
| 55 | + } |
| 56 | + }) |
| 57 | + .catch((error) => { |
| 58 | + console.error('서버 요청 실패:', error); |
| 59 | + }); |
| 60 | + } else { |
| 61 | + // 인증 코드가 없는 경우 처리 |
| 62 | + console.error('인증 코드가 없습니다.'); |
| 63 | + } |
| 64 | + }, [navigate]); |
| 65 | + return <Loading />; |
67 | 66 | }; |
68 | 67 |
|
69 | 68 | export default KakaoCallback; |
0 commit comments