Skip to content

Commit e9dff9c

Browse files
authored
Merge pull request #112 from flowbit-team/bugfix/105
bugfix: #105 소셜로그인 이후 토큰이 있어도 401로 떨어지는 현상해결
2 parents 7fc6af3 + 72e162e commit e9dff9c

3 files changed

Lines changed: 37 additions & 24 deletions

File tree

src/api/index.ts

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,25 @@ import axios, {
66
AxiosRequestConfig,
77
AxiosResponse,
88
InternalAxiosRequestConfig,
9+
RawAxiosRequestHeaders,
910
} from "axios";
1011

1112
/** API 사용 전, ENV 파일을 통해 서버 연동 설정을 해주세요 */
1213
const API_URL = import.meta.env.VITE_API_URL as string;
1314

14-
const baseApi = axios.create({
15-
baseURL: API_URL,
16-
timeout: 5000,
15+
const headers: RawAxiosRequestHeaders = {
16+
"Content-Type": "application/json",
17+
};
1718

18-
headers: {
19-
"Content-Type": "application/json",
20-
},
19+
const token = localStorage.getItem(ACCESS_TOKEN);
20+
if (token) {
21+
headers.Authorization = `Bearer ${token}`;
22+
}
23+
24+
const axiosInstance = axios.create({
25+
baseURL: API_URL,
26+
headers,
27+
withCredentials: true, // CORS 쿠키 전송을 위해 필요
2128
});
2229

2330
/** 개발 환경에서만 실행되논 로깅 함수 */
@@ -124,11 +131,11 @@ const onErrorResponse = (error: AxiosError | Error) => {
124131
};
125132

126133
/** 인터셉터를 설정 하고, Axios Instance를 반환하는 함수 */
127-
const setInterceptors = (axiosInstance: AxiosInstance): AxiosInstance => {
128-
axiosInstance.interceptors.request.use(onRequest, onErrorRequest);
129-
axiosInstance.interceptors.response.use(onResponse, onErrorResponse);
134+
const setInterceptors = (instance: AxiosInstance): AxiosInstance => {
135+
instance.interceptors.request.use(onRequest, onErrorRequest);
136+
instance.interceptors.response.use(onResponse, onErrorResponse);
130137

131-
return axiosInstance;
138+
return instance;
132139
};
133140

134-
export const api = setInterceptors(baseApi);
141+
export const api = setInterceptors(axiosInstance);

src/app/oauth2/redierct/index.tsx

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,30 @@
1-
import { loginState } from "@/store/user";
2-
import { ACCESS_TOKEN } from "@/utils/constant";
3-
import { useSetAtom } from "jotai";
41
import { useEffect } from "react";
5-
import { useLocation, useNavigate } from "react-router-dom";
2+
import { useNavigate } from "react-router-dom";
3+
import { useSetAtom } from "jotai";
4+
import { loginState, tokenState } from "@/store/user";
5+
import { ACCESS_TOKEN } from "@/utils/constant";
66

77
export default function Oauth2Redirect() {
8-
const location = useLocation();
98
const navigate = useNavigate();
10-
const params = new URLSearchParams(location.search);
11-
const accessToken = params.get("accessToken");
129
const setLogin = useSetAtom(loginState);
10+
const setToken = useSetAtom(tokenState);
1311

1412
useEffect(() => {
15-
if (accessToken) {
16-
localStorage.setItem(ACCESS_TOKEN, accessToken);
13+
const params = new URLSearchParams(window.location.search);
14+
const token = params.get("token");
15+
16+
if (token) {
17+
localStorage.setItem(ACCESS_TOKEN, token);
18+
setToken(token);
1719
setLogin(true);
1820
navigate("/");
1921
} else {
20-
alert("오류가 발생했어요, 관리자에게 문의해주세요");
22+
// 토큰이 없는 경우 에러 처리
23+
setLogin(false);
24+
setToken("");
25+
navigate("/signin");
2126
}
22-
}, []);
27+
}, [navigate, setLogin, setToken]);
2328

24-
return <>처리중입니다.</>;
29+
return null;
2530
}

src/store/user/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
import { atomWithStorage } from "jotai/utils";
22

3-
export const loginState = atomWithStorage("isLogin", false);
3+
export const loginState = atomWithStorage("isLogin", false);
4+
export const tokenState = atomWithStorage("accessToken", "");

0 commit comments

Comments
 (0)