Skip to content

Commit e31fad7

Browse files
authored
Merge pull request #251 from MBTips/feature/사용자-로그인-유지-기한-관리
feat: 사용자 로그인 유지 기한 관리
2 parents 904b4c5 + 3577642 commit e31fad7

File tree

2 files changed

+40
-6
lines changed

2 files changed

+40
-6
lines changed

src/App.tsx

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useEffect } from "react";
1+
import { useEffect, useState } from "react";
22
import {
33
BrowserRouter as Router,
44
Routes,
@@ -19,8 +19,10 @@ import KaKaoLogin from "@/pages/KaKaoLogin";
1919
import MbtiTestIntro from "@/pages/MbtiTestIntro";
2020
import MbtiTestQuestions from "@/pages/MbtiTestQuestions";
2121
import MbtiTestResult from "@/pages/MbtiTestResult";
22-
import CenteredLayout from "@/components/CenteredLayout";
2322
import Error from "@/pages/Error";
23+
import CenteredLayout from "@/components/CenteredLayout";
24+
import ToastMessage from "@/components/ToastMessage";
25+
import useAuthStore from "@/store/useAuthStore";
2426

2527
const PageTracker = () => {
2628
const location = useLocation();
@@ -69,14 +71,37 @@ const PageTracker = () => {
6971
};
7072

7173
const App = () => {
74+
const { logout } = useAuthStore();
75+
const [toastMessage, setToastMessage] = useState("");
76+
const storageAuth = localStorage.getItem("auth-storage");
77+
const parsedAuth = storageAuth ? JSON.parse(storageAuth).state : null;
78+
79+
const checkSession = () => {
80+
const expirationTime = new Date(
81+
new Date(parsedAuth.loginTime).getTime() + 24 * 60 * 60 * 1000
82+
);
83+
const now = new Date();
84+
if (now > expirationTime) {
85+
setToastMessage("로그인 세션이 만료되었습니다.");
86+
logout();
87+
}
88+
};
89+
7290
useEffect(() => {
7391
initGA();
92+
if (parsedAuth) checkSession();
7493
}, []);
7594

7695
return (
7796
<Router>
7897
<PageTracker />
7998
<CenteredLayout>
99+
{toastMessage && (
100+
<ToastMessage
101+
message={toastMessage}
102+
onClose={() => setToastMessage("")}
103+
/>
104+
)}
80105
<Routes>
81106
<Route path="/" element={<Home />} />
82107
<Route path="/select-info" element={<SelectInfo />} />

src/store/useAuthStore.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,34 @@ import instance from "@/api/axios";
55
interface AuthStore {
66
isLoggedIn: boolean;
77
accessToken: string | null;
8+
loginTime: string | null;
89
login: (code: string) => Promise<{ ok: boolean }>;
910
logout: () => void;
1011
}
1112

13+
interface LoginResponse {
14+
data: string;
15+
}
16+
1217
const useAuthStore = create(
1318
persist<AuthStore>(
1419
(set) => ({
1520
isLoggedIn: false,
1621
accessToken: null,
22+
loginTime: null,
1723
login: async (code: string) => {
1824
try {
1925
const requestURI =
20-
// 아래 코드는 백엔드팀에서 작업해주시면 동일한 uri로 바뀔 예정 -> 4.24 정준영
2126
import.meta.env.MODE === "production"
2227
? `/api/kakao/login?code=${code}`
2328
: `/api/kakao/login?code=${code}&redirectUrl=https://localhost:5173/kakao-login`;
2429

25-
const res = await instance.get(requestURI);
30+
const res = await instance.get<LoginResponse>(requestURI);
31+
const currentTime = new Date().toISOString();
2632
set({
2733
isLoggedIn: true,
28-
accessToken: res.data.data as string
34+
accessToken: res.data.data,
35+
loginTime: currentTime
2936
});
3037
return {
3138
ok: true
@@ -37,8 +44,10 @@ const useAuthStore = create(
3744
logout: () => {
3845
set({
3946
isLoggedIn: false,
40-
accessToken: null
47+
accessToken: null,
48+
loginTime: null
4149
});
50+
window.location.href = "/";
4251
return { ok: true };
4352
}
4453
}),

0 commit comments

Comments
 (0)