Skip to content

Commit 45e4b5c

Browse files
committed
75
1 parent de9b03f commit 45e4b5c

File tree

6 files changed

+42
-9
lines changed

6 files changed

+42
-9
lines changed

db.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -878,14 +878,14 @@
878878
{
879879
"title": "2",
880880
"body": "2",
881-
"publish": false,
881+
"publish": true,
882882
"createdAt": 1657240147188,
883883
"id": 126
884884
},
885885
{
886886
"title": "2",
887887
"body": "2",
888-
"publish": false,
888+
"publish": true,
889889
"createdAt": 1657240162434,
890890
"id": 127
891891
}

src/App.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import routes from './routes';
88
import Toast from './components/Toast';
99
import useToast from './hooks/toast';
1010
import { useSelector } from 'react-redux';
11+
import ProtectedRoute from './ProtectedRoute';
1112

1213
function App() {
1314
const toasts = useSelector(state => state.toast.toasts);
@@ -22,6 +23,13 @@ function App() {
2223
<div className="container mt-3">
2324
<Switch>
2425
{routes.map((route) => {
26+
if (route.auth) {
27+
return <ProtectedRoute
28+
path={route.path}
29+
component={route.component}
30+
key={route.path}
31+
/>
32+
}
2533
return <Route
2634
key={route.path}
2735
exact

src/ProtectedRoute.jsx

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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;

src/components/NavBar.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ const NavBar = () => {
2929
{isLoggedIn ? 'Logout' : 'Login'}
3030
</button>
3131
</li>
32-
<li className="nav-item me-2">
32+
{isLoggedIn ? <li className="nav-item me-2">
3333
<NavLink
3434
activeClassName="active"
3535
className="nav-link"
@@ -38,7 +38,7 @@ const NavBar = () => {
3838
>
3939
Admin
4040
</NavLink>
41-
</li>
41+
</li> : null}
4242
<li className="nav-item">
4343
<NavLink
4444
activeClassName="active"

src/pages/ShowPage.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@ import axios from 'axios';
33
import { useEffect, useState } from "react";
44
import LoadingSpinner from "../components/LoadingSpinner";
55
import { Link } from "react-router-dom";
6+
import { useSelector } from "react-redux";
67

78
const ShowPage = () => {
89
const { id } = useParams();
910
const [post, setPost] = useState(null);
1011
const [loading, setLoading] = useState(true);
12+
const isLoggedIn = useSelector(state => state.auth.isLoggedIn);
1113

1214
const getPost = (id) => {
1315
axios.get(`http://localhost:3001/posts/${id}`).then((res) => {
@@ -32,14 +34,14 @@ const ShowPage = () => {
3234
<div>
3335
<div className="d-flex">
3436
<h1 className="flex-grow-1">{post.title}</h1>
35-
<div>
37+
{isLoggedIn && <div>
3638
<Link
3739
className="btn btn-primary"
3840
to={`/blogs/${id}/edit`}
3941
>
4042
Edit
4143
</Link>
42-
</div>
44+
</div>}
4345
</div>
4446
<small className="text-muted">
4547
Created At: {printDate(post.createdAt)}

src/routes.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,18 @@ const routes = [
1616
},
1717
{
1818
path: '/admin',
19-
component: AdminPage
19+
component: AdminPage,
20+
auth: true
2021
},
2122
{
2223
path: '/blogs/create',
23-
component: CreatePage
24+
component: CreatePage,
25+
auth: true
2426
},
2527
{
2628
path: '/blogs/:id/edit',
27-
component: EditPage
29+
component: EditPage,
30+
auth: true
2831
},
2932
{
3033
path: '/blogs/:id',

0 commit comments

Comments
 (0)