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,12 +1,47 @@
1 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 10
 import Navbar from "./components/Navbar/";
5 11
 import Home from "./pages/Home/";
6 12
 import SignupPage from "./pages/Signup";
7 13
 import LoginPage from "./pages/Login/";
14
+import ProfilePage from "./pages/Profile";
8 15
 
9 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 45
 	return (
11 46
 		<div className="App">
12 47
 			<Router>
@@ -16,12 +51,9 @@ const App = () => {
16 51
 						<Route exact path="/">
17 52
 							<Home />
18 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 57
 					</Switch>
26 58
 				</section>
27 59
 			</Router>

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

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

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

@@ -36,8 +36,10 @@
36 36
 
37 37
   .navbar__RightSide{
38 38
     display: flex;
39
-    justify-content: space-between;
40
-    width: 350px;
39
+    justify-content: end;
41 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,7 +41,8 @@ const LoginForm = () => {
41 41
 				const payload = {
42 42
 					type: "user",
43 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 47
 				dispatch(setCurrentUser(payload));
47 48
 				history.push("/");

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

@@ -1,9 +1,40 @@
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 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 24
 	return (
5 25
 		<div className="test">
6 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 38
 		</div>
8 39
 	);
9 40
 };

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

@@ -0,0 +1,17 @@
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