Browse Source

Ajout de AuthRoute et UnAuthRoute et d'une page profile

Lou 3 years ago
parent
commit
8aef249fa5

+ 39
- 7
src/App.jsx View File

1
 import React from "react";
1
 import React from "react";
2
-import { BrowserRouter as Router, Route, Switch } from "react-router-dom";
2
+import {
3
+	BrowserRouter as Router,
4
+	Route,
5
+	Switch,
6
+	Redirect
7
+} from "react-router-dom";
8
+import { useSelector } from "react-redux";
3
 
9
 
4
 import Navbar from "./components/Navbar/";
10
 import Navbar from "./components/Navbar/";
5
 import Home from "./pages/Home/";
11
 import Home from "./pages/Home/";
6
 import SignupPage from "./pages/Signup";
12
 import SignupPage from "./pages/Signup";
7
 import LoginPage from "./pages/Login/";
13
 import LoginPage from "./pages/Login/";
14
+import ProfilePage from "./pages/Profile";
8
 
15
 
9
 const App = () => {
16
 const App = () => {
17
+	const currentUser = useSelector((state) => state.currentUser);
18
+
19
+	const UnAuthRoute = ({ component: Component, ...rest }) => (
20
+		<Route
21
+			{...rest}
22
+			render={(props) =>
23
+				currentUser ? (
24
+					<Redirect to={{ pathname: "/" }} />
25
+				) : (
26
+					<Component {...props} />
27
+				)
28
+			}
29
+		/>
30
+	);
31
+
32
+	const AuthRoute = ({ component: Component, ...rest }) => (
33
+		<Route
34
+			{...rest}
35
+			render={(props) =>
36
+				currentUser ? (
37
+					<Component {...props} />
38
+				) : (
39
+					<Redirect to={{ pathname: "/signin" }} />
40
+				)
41
+			}
42
+		/>
43
+	);
44
+
10
 	return (
45
 	return (
11
 		<div className="App">
46
 		<div className="App">
12
 			<Router>
47
 			<Router>
16
 						<Route exact path="/">
51
 						<Route exact path="/">
17
 							<Home />
52
 							<Home />
18
 						</Route>
53
 						</Route>
19
-						<Route exact path="/login">
20
-							<LoginPage />
21
-						</Route>
22
-						<Route exact path="/Signup">
23
-							<SignupPage />
24
-						</Route>
54
+						<UnAuthRoute path="/login" component={LoginPage} />
55
+						<UnAuthRoute path="/signup" component={SignupPage} />
56
+						<AuthRoute path="/me" component={ProfilePage} />
25
 					</Switch>
57
 					</Switch>
26
 				</section>
58
 				</section>
27
 			</Router>
59
 			</Router>

+ 3
- 1
src/components/Navbar/index.jsx View File

36
 				)}
36
 				)}
37
 				{currentUser && (
37
 				{currentUser && (
38
 					<>
38
 					<>
39
-						<button>Profil</button>
39
+						<Link to="/me">
40
+							<button>Profile</button>
41
+						</Link>
40
 						<LogoutButton />
42
 						<LogoutButton />
41
 					</>
43
 					</>
42
 				)}
44
 				)}

+ 4
- 2
src/components/Navbar/index.scss View File

36
 
36
 
37
   .navbar__RightSide{
37
   .navbar__RightSide{
38
     display: flex;
38
     display: flex;
39
-    justify-content: space-between;
40
-    width: 350px;
39
+    justify-content: end;
41
     margin-right: 15px;
40
     margin-right: 15px;
41
+    button {
42
+      margin: 0px 10px;
43
+    }
42
   }
44
   }
43
 } 
45
 } 

+ 2
- 1
src/components/forms/LoginForm/index.jsx View File

41
 				const payload = {
41
 				const payload = {
42
 					type: "user",
42
 					type: "user",
43
 					id: jwt_decode(response.data.token).user_id,
43
 					id: jwt_decode(response.data.token).user_id,
44
-					email: userData.email
44
+					email: userData.email,
45
+					username: response.data.username
45
 				};
46
 				};
46
 				dispatch(setCurrentUser(payload));
47
 				dispatch(setCurrentUser(payload));
47
 				history.push("/");
48
 				history.push("/");

+ 32
- 1
src/pages/Home/index.jsx View File

1
-import React from "react";
1
+import React, { useEffect } from "react";
2
+import { useSelector } from "react-redux";
3
+
4
+import Cookies from "js-cookie";
2
 
5
 
3
 const Home = () => {
6
 const Home = () => {
7
+	const currentUser = useSelector((state) => state.currentUser);
8
+
9
+	// const isThereAToken = () => {
10
+	// 	let token = Cookies.get("token");
11
+	// 	if (token) {
12
+	// 		console.log("Connecté 666");
13
+	// 	} else {
14
+	// 		console.log("Non connecté 666");
15
+	// 	}
16
+	// };
17
+	// Problématique à résoudre : s'il n'y a plus de token stocké dans un cookie (supprimé volontairement ou non par un·e utilisateurice), il faut alors déconnecter l'utilisateurice (supprimer le contenu du store Redux)
18
+	// Piste : une fonction qui serait exécutée dans les useEffect des pages uniquement, pas des composants, et qui observerait la présence/absence d'un token, et viderait le store si nécessaire
19
+
20
+	// useEffect(() => {
21
+	// 	isThereAToken();
22
+	// }, []);
23
+
4
 	return (
24
 	return (
5
 		<div className="test">
25
 		<div className="test">
6
 			<h2>Home</h2>
26
 			<h2>Home</h2>
27
+			{currentUser && (
28
+				<div>
29
+					<p>Utilisateurice connecté·e, bravo !</p>
30
+				</div>
31
+			)}
32
+
33
+			{!currentUser && (
34
+				<div>
35
+					<p>Utilisateurice non connecté·e.</p>
36
+				</div>
37
+			)}
7
 		</div>
38
 		</div>
8
 	);
39
 	);
9
 };
40
 };

+ 17
- 0
src/pages/Profile/index.jsx View File

1
+import React, { useEffect } from "react";
2
+import { useSelector } from "react-redux";
3
+
4
+const ProfilePage = () => {
5
+	const currentUser = useSelector((state) => state.currentUser);
6
+
7
+	useEffect(() => {
8
+		console.log(currentUser);
9
+	}, []);
10
+	return (
11
+		<div>
12
+			<h1>Hello {currentUser.username}</h1>
13
+		</div>
14
+	);
15
+};
16
+
17
+export default ProfilePage;

Loading…
Cancel
Save