Skip to content

Commit

Permalink
hotfix: refreshToken 쿠키에서 추출
Browse files Browse the repository at this point in the history
  • Loading branch information
pepperdad committed Dec 19, 2024
1 parent 052a3f3 commit a007329
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 18 deletions.
6 changes: 2 additions & 4 deletions src/apis/domains/users/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,11 @@ export const usePostKakaoLogin = () => {
if (response) {
const userData = response;

console.log(response);

if (userData) {
const { accessToken, nickname, refreshToken, role } = userData;
const { accessToken, nickname, role } = userData;

if (accessToken && nickname) {
setUserData({ nickname, accessToken, refreshToken, role });
setUserData({ nickname, accessToken, role });
} else {
console.error("accessToken or nickname is undefined");
}
Expand Down
30 changes: 18 additions & 12 deletions src/hooks/useTokenRefresher.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,23 @@ export default function TokenRefresher() {

useEffect(() => {
const user = localStorage.getItem("user");
console.log(user);
console.log(JSON.parse(user)?.role);
console.log(JSON.parse(user)?.refreshToken);
console.log(!user || !JSON.parse(user)?.role || !JSON.parse(user)?.refreshToken);

if (!user || !JSON.parse(user)?.role || !JSON.parse(user)?.refreshToken) {
// 기존에 존재하던 유저 role, refreshToken 유무로 임시로 토큰 제거 후 리로드
// localStorage.clear();
// openAlert({ title: "다시 로그인 해주세요." });
// window.location.reload();
return;
const getCookie = (name: string): string | undefined => {
const value = `; ${document.cookie}`;
const parts = value.split(`; ${name}=`);
if (parts.length === 2) {
return parts.pop()?.split(";").shift();
}
};

if (user) {
if (!JSON.parse(user)?.role) {
// 기존에 존재하던 유저 role 유무로 임시로 토큰 제거 후 리로드
localStorage.clear();
openAlert({ title: "다시 로그인 해주세요." });
window.location.reload();
return;
}
}

const interceptor = instance.interceptors.response.use(
Expand All @@ -31,7 +37,7 @@ export default function TokenRefresher() {

if (status === 401 && user) {
try {
const refreshToken = JSON.parse(user)?.refreshToken;
const refreshToken = getCookie("refreshToken");

const response: AxiosResponse<{ data: { accessToken: string } }> = await get(
"/users/refresh-token",
Expand All @@ -53,7 +59,7 @@ export default function TokenRefresher() {
localStorage.clear();
navigate("/main");
openAlert({ title: "장시간 미활동으로 인해 \n자동으로 로그아웃 되었습니다." });
// window.location.reload();
window.location.reload();
}
} else if (status === 500) {
openAlert({
Expand Down
1 change: 0 additions & 1 deletion src/stores/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,5 @@ import { atomWithStorage } from "jotai/utils";
export const userAtom = atomWithStorage<UserProps>("user", {
nickname: "",
accessToken: "",
refreshToken: "",
role: "",
});
1 change: 0 additions & 1 deletion src/typings/userType.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
export interface UserProps {
nickname: string;
accessToken: string;
refreshToken: string;
role: string;
}

0 comments on commit a007329

Please sign in to comment.