File tree Expand file tree Collapse file tree 6 files changed +42
-9
lines changed Expand file tree Collapse file tree 6 files changed +42
-9
lines changed Original file line number Diff line number Diff line change 878
878
{
879
879
"title" : " 2" ,
880
880
"body" : " 2" ,
881
- "publish" : false ,
881
+ "publish" : true ,
882
882
"createdAt" : 1657240147188 ,
883
883
"id" : 126
884
884
},
885
885
{
886
886
"title" : " 2" ,
887
887
"body" : " 2" ,
888
- "publish" : false ,
888
+ "publish" : true ,
889
889
"createdAt" : 1657240162434 ,
890
890
"id" : 127
891
891
}
Original file line number Diff line number Diff line change @@ -8,6 +8,7 @@ import routes from './routes';
8
8
import Toast from './components/Toast' ;
9
9
import useToast from './hooks/toast' ;
10
10
import { useSelector } from 'react-redux' ;
11
+ import ProtectedRoute from './ProtectedRoute' ;
11
12
12
13
function App ( ) {
13
14
const toasts = useSelector ( state => state . toast . toasts ) ;
@@ -22,6 +23,13 @@ function App() {
22
23
< div className = "container mt-3" >
23
24
< Switch >
24
25
{ routes . map ( ( route ) => {
26
+ if ( route . auth ) {
27
+ return < ProtectedRoute
28
+ path = { route . path }
29
+ component = { route . component }
30
+ key = { route . path }
31
+ />
32
+ }
25
33
return < Route
26
34
key = { route . path }
27
35
exact
Original file line number Diff line number Diff line change
1
+ import { useSelector } from "react-redux" ;
2
+ import { Redirect , Route } from "react-router-dom" ;
3
+
4
+ const ProtectedRoute = (
5
+ { component, path, key}
6
+ ) => {
7
+ const isLoggedIn = useSelector ( state => state . auth . isLoggedIn ) ;
8
+ if ( ! isLoggedIn ) {
9
+ return < Redirect to = "/" />
10
+ }
11
+
12
+ return < Route
13
+ component = { component }
14
+ path = { path }
15
+ key = { key }
16
+ exact
17
+ /> ;
18
+ } ;
19
+
20
+ export default ProtectedRoute ;
Original file line number Diff line number Diff line change @@ -29,7 +29,7 @@ const NavBar = () => {
29
29
{ isLoggedIn ? 'Logout' : 'Login' }
30
30
</ button >
31
31
</ li >
32
- < li className = "nav-item me-2" >
32
+ { isLoggedIn ? < li className = "nav-item me-2" >
33
33
< NavLink
34
34
activeClassName = "active"
35
35
className = "nav-link"
@@ -38,7 +38,7 @@ const NavBar = () => {
38
38
>
39
39
Admin
40
40
</ NavLink >
41
- </ li >
41
+ </ li > : null }
42
42
< li className = "nav-item" >
43
43
< NavLink
44
44
activeClassName = "active"
Original file line number Diff line number Diff line change @@ -3,11 +3,13 @@ import axios from 'axios';
3
3
import { useEffect , useState } from "react" ;
4
4
import LoadingSpinner from "../components/LoadingSpinner" ;
5
5
import { Link } from "react-router-dom" ;
6
+ import { useSelector } from "react-redux" ;
6
7
7
8
const ShowPage = ( ) => {
8
9
const { id } = useParams ( ) ;
9
10
const [ post , setPost ] = useState ( null ) ;
10
11
const [ loading , setLoading ] = useState ( true ) ;
12
+ const isLoggedIn = useSelector ( state => state . auth . isLoggedIn ) ;
11
13
12
14
const getPost = ( id ) => {
13
15
axios . get ( `http://localhost:3001/posts/${ id } ` ) . then ( ( res ) => {
@@ -32,14 +34,14 @@ const ShowPage = () => {
32
34
< div >
33
35
< div className = "d-flex" >
34
36
< h1 className = "flex-grow-1" > { post . title } </ h1 >
35
- < div >
37
+ { isLoggedIn && < div >
36
38
< Link
37
39
className = "btn btn-primary"
38
40
to = { `/blogs/${ id } /edit` }
39
41
>
40
42
Edit
41
43
</ Link >
42
- </ div >
44
+ </ div > }
43
45
</ div >
44
46
< small className = "text-muted" >
45
47
Created At: { printDate ( post . createdAt ) }
Original file line number Diff line number Diff line change @@ -16,15 +16,18 @@ const routes = [
16
16
} ,
17
17
{
18
18
path : '/admin' ,
19
- component : AdminPage
19
+ component : AdminPage ,
20
+ auth : true
20
21
} ,
21
22
{
22
23
path : '/blogs/create' ,
23
- component : CreatePage
24
+ component : CreatePage ,
25
+ auth : true
24
26
} ,
25
27
{
26
28
path : '/blogs/:id/edit' ,
27
- component : EditPage
29
+ component : EditPage ,
30
+ auth : true
28
31
} ,
29
32
{
30
33
path : '/blogs/:id' ,
You can’t perform that action at this time.
0 commit comments