Showing
1 changed file
with
18 additions
and
11 deletions
1 | import { useState, useEffect } from "react"; | 1 | import { useState, useEffect } from "react"; |
2 | -import { useNavigate } from "react-router-dom"; | 2 | +import { Navigate, useNavigate } from "react-router-dom"; |
3 | import authService from "../service/auth"; | 3 | import authService from "../service/auth"; |
4 | // components | 4 | // components |
5 | import Topbar from "../components/Topbar"; | 5 | import Topbar from "../components/Topbar"; |
6 | import Bottombar from "../components/Bottombar"; | 6 | import Bottombar from "../components/Bottombar"; |
7 | 7 | ||
8 | function Settings() { | 8 | function Settings() { |
9 | + const token = sessionStorage.getItem("user-token"); | ||
9 | const navigate = useNavigate(); | 10 | const navigate = useNavigate(); |
10 | - const [editSuccess, setEditSuccess] = useState(0); | 11 | + const [logoutSuccess, setLogoutSuccess] = useState(0); |
11 | const [userinfo, setUserinfo] = useState({ | 12 | const [userinfo, setUserinfo] = useState({ |
12 | username: "", | 13 | username: "", |
13 | country: "", | 14 | country: "", |
... | @@ -15,13 +16,12 @@ function Settings() { | ... | @@ -15,13 +16,12 @@ function Settings() { |
15 | email: "", | 16 | email: "", |
16 | }); | 17 | }); |
17 | 18 | ||
18 | - // useEffect(() => { | 19 | + useEffect(() => { |
19 | - // navigate("/login", { replace: true }); | 20 | + if (logoutSuccess === "logout") { |
20 | - // }, [ navigate]); | 21 | + sessionStorage.clear(); |
21 | - | 22 | + navigate("/login", { replace: true }); |
22 | - const logout = () => { | 23 | + } |
23 | - return; | 24 | + }, [logoutSuccess, setLogoutSuccess, navigate]); |
24 | - }; | ||
25 | 25 | ||
26 | const onChange = (e) => { | 26 | const onChange = (e) => { |
27 | setUserinfo((orgState) => ({ | 27 | setUserinfo((orgState) => ({ |
... | @@ -37,9 +37,15 @@ function Settings() { | ... | @@ -37,9 +37,15 @@ function Settings() { |
37 | return; | 37 | return; |
38 | } | 38 | } |
39 | const response = await authService.handleUserEdit(userinfo); | 39 | const response = await authService.handleUserEdit(userinfo); |
40 | - setEditSuccess(response); | 40 | + if (response === "success") { |
41 | + alert("User info successfully changed"); | ||
42 | + } | ||
41 | }; | 43 | }; |
42 | 44 | ||
45 | + if (!token) { | ||
46 | + return <Navigate to="/login" />; | ||
47 | + } | ||
48 | + | ||
43 | return ( | 49 | return ( |
44 | <div> | 50 | <div> |
45 | <Topbar /> | 51 | <Topbar /> |
... | @@ -89,7 +95,8 @@ function Settings() { | ... | @@ -89,7 +95,8 @@ function Settings() { |
89 | <br /> | 95 | <br /> |
90 | 96 | ||
91 | <div> | 97 | <div> |
92 | - Want to logout? <button onClick={() => logout()}>Logout</button> | 98 | + Want to logout?{" "} |
99 | + <button onClick={() => setLogoutSuccess("logout")}>Logout</button> | ||
93 | </div> | 100 | </div> |
94 | <Bottombar /> | 101 | <Bottombar /> |
95 | </div> | 102 | </div> | ... | ... |
-
Please register or login to post a comment