Skip to content

Commit ef6b5a1

Browse files
committed
fix: add jwt token
1 parent 4e86cc8 commit ef6b5a1

9 files changed

+24
-37
lines changed

src/api/UserContext.jsx

+24-28
Original file line numberDiff line numberDiff line change
@@ -5,31 +5,21 @@ const UserContext = createContext();
55
export const UserProvider = ({ children }) => {
66
const [isLoggedin, setIsLoggedin] = useState(false);
77
const [user, setUser] = useState(null);
8+
const [token, setToken] = useState(localStorage.getItem("jwtToken"));
89

9-
const checkSession = async () => {
10-
try {
11-
const response = await fetch(
12-
"https://rw2644hx4c.execute-api.us-east-1.amazonaws.com/api/users/session",
13-
{
14-
method: "GET",
15-
credentials: "include",
16-
}
17-
);
18-
19-
if (response.ok) {
20-
const data = await response.json();
21-
setUser(data.user);
22-
setIsLoggedin(true);
23-
} else {
24-
setUser(null);
25-
setIsLoggedin(false);
10+
useEffect(() => {
11+
// 컴포넌트 마운트 시 로컬 스토리지의 토큰 확인
12+
const storedToken = localStorage.getItem("jwtToken");
13+
if (storedToken) {
14+
setToken(storedToken);
15+
setIsLoggedin(true);
16+
// 토큰이 있으면 사용자 정보도 로컬 스토리지에서 가져옴
17+
const storedUser = JSON.parse(localStorage.getItem("user"));
18+
if (storedUser) {
19+
setUser(storedUser);
2620
}
27-
} catch (error) {
28-
console.error("Failed to check session:", error);
29-
setUser(null);
30-
setIsLoggedin(false);
3121
}
32-
};
22+
}, []);
3323

3424
const login = async (user_id, password) => {
3525
try {
@@ -40,7 +30,6 @@ export const UserProvider = ({ children }) => {
4030
headers: {
4131
"Content-Type": "application/json",
4232
},
43-
credentials: "include",
4433
body: JSON.stringify({ userId: user_id, user_password: password }),
4534
}
4635
);
@@ -49,6 +38,10 @@ export const UserProvider = ({ children }) => {
4938
const data = await response.json();
5039
setUser(data.user);
5140
setIsLoggedin(true);
41+
setToken(data.token);
42+
// 로컬 스토리지에 토큰과 사용자 정보 저장
43+
localStorage.setItem("jwtToken", data.token);
44+
localStorage.setItem("user", JSON.stringify(data.user));
5245
} else {
5346
const errorData = await response.json();
5447
throw new Error(errorData.message);
@@ -72,6 +65,13 @@ export const UserProvider = ({ children }) => {
7265
if (response.ok) {
7366
setUser(null);
7467
setIsLoggedin(false);
68+
// 로컬 스토리지에서 토큰과 사용자 정보 제거
69+
localStorage.removeItem("jwtToken");
70+
localStorage.removeItem("user");
71+
// 상태 초기화
72+
setUser(null);
73+
setIsLoggedin(false);
74+
setToken(null);
7575
} else {
7676
const errorData = await response.json();
7777
throw new Error(errorData.message);
@@ -81,12 +81,8 @@ export const UserProvider = ({ children }) => {
8181
}
8282
};
8383

84-
useEffect(() => {
85-
checkSession();
86-
}, []);
87-
8884
return (
89-
<UserContext.Provider value={{ user, isLoggedin, login, logout }}>
85+
<UserContext.Provider value={{ user, isLoggedin, token, login, logout }}>
9086
{children}
9187
</UserContext.Provider>
9288
);

src/components/price/PriceItem.jsx

-1
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,6 @@ function PriceItem(props) {
9797
headers: {
9898
"Content-Type": "application/json",
9999
},
100-
credentials: "include",
101100
body: JSON.stringify({
102101
product_id: product_id,
103102
quantity: count,

src/components/shopping/ShoppingBudget.jsx

-1
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,6 @@ function ShoppingBudget({ budget, present, hideButtons }) {
113113
headers: {
114114
"Content-Type": "application/json",
115115
},
116-
credentials: "include",
117116
body: JSON.stringify({
118117
budget: parseInt(newBudget),
119118
}),

src/components/shopping/ShoppingList.jsx

-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ function ShoppingList({ hideButtons }) {
3030
headers: {
3131
"Content-Type": "application/json",
3232
},
33-
credentials: "include",
3433
}
3534
);
3635

src/pages/LandingPage.jsx

-2
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,6 @@ function LandingPage() {
117117
headers: {
118118
"Content-Type": "application/json",
119119
},
120-
credentials: "include",
121120
body: JSON.stringify({
122121
product_id: item.product_id,
123122
quantity: 1,
@@ -152,7 +151,6 @@ function LandingPage() {
152151
headers: {
153152
"Content-Type": "application/json",
154153
},
155-
credentials: "include",
156154
}
157155
);
158156
const data = await response.json();

src/pages/MyPage.jsx

-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ function MyPage() {
4242
headers: {
4343
"Content-Type": "application/json",
4444
},
45-
credentials: "include",
4645
body: JSON.stringify({ newName: name }),
4746
}
4847
);

src/pages/price/PriceCategorySearchPage.jsx

-1
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,6 @@ function PriceCategorySearchPage() {
7979
headers: {
8080
"Content-Type": "application/json",
8181
},
82-
credentials: "include",
8382
}
8483
);
8584
const data = await response.json();

src/pages/price/PriceMainPage.jsx

-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,6 @@ function PriceMainPage() {
7878
headers: {
7979
"Content-Type": "application/json",
8080
},
81-
credentials: "include",
8281
}
8382
);
8483
const data = await response.json();

src/pages/price/PriceNameSearchPage.jsx

-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@ function PriceNameSearchPage() {
6868
headers: {
6969
"Content-Type": "application/json",
7070
},
71-
credentials: "include",
7271
}
7372
);
7473
const data = await response.json();

0 commit comments

Comments
 (0)