Skip to content

Commit de9b03f

Browse files
committed
74
1 parent 78ed216 commit de9b03f

File tree

4 files changed

+43
-8
lines changed

4 files changed

+43
-8
lines changed

db.json

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -888,13 +888,6 @@
888888
"publish": false,
889889
"createdAt": 1657240162434,
890890
"id": 127
891-
},
892-
{
893-
"title": "f",
894-
"body": "f",
895-
"publish": false,
896-
"createdAt": 1657240212897,
897-
"id": 130
898891
}
899892
]
900893
}

src/components/NavBar.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
1+
import { useDispatch, useSelector } from 'react-redux';
12
import { Link, NavLink } from 'react-router-dom';
3+
import { login, logout } from '../store/authSlice';
24

35
const NavBar = () => {
6+
const isLoggedIn = useSelector(state => state.auth.isLoggedIn);
7+
const dispatch = useDispatch();
48
return (
59
<nav className="navbar navbar-dark bg-dark">
610
<div className="container">
@@ -11,6 +15,20 @@ const NavBar = () => {
1115
}}
1216
className="navbar-nav"
1317
>
18+
<li className="nav-item me-2">
19+
<button
20+
className="text-white btn btn-link text-decoration-none"
21+
onClick={() => {
22+
if (isLoggedIn) {
23+
dispatch(logout());
24+
} else {
25+
dispatch(login());
26+
}
27+
}}
28+
>
29+
{isLoggedIn ? 'Logout' : 'Login'}
30+
</button>
31+
</li>
1432
<li className="nav-item me-2">
1533
<NavLink
1634
activeClassName="active"

src/store/authSlice.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { createSlice } from '@reduxjs/toolkit';
2+
3+
const initialState = {
4+
isLoggedIn: false
5+
};
6+
7+
const authSlice = createSlice({
8+
name: 'auth',
9+
initialState,
10+
reducers: {
11+
login: (state) => {
12+
state.isLoggedIn = true;
13+
},
14+
logout: (state) => {
15+
state.isLoggedIn = false;
16+
}
17+
}
18+
})
19+
20+
export const { login, logout } = authSlice.actions;
21+
22+
export default authSlice.reducer;

src/store/store.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import { configureStore } from '@reduxjs/toolkit'
22
import toastReducer from './toastSlice'
3+
import authReducer from './authSlice'
34

45
export const store = configureStore({
56
reducer: {
6-
toast: toastReducer
7+
toast: toastReducer,
8+
auth: authReducer
79
},
810
})

0 commit comments

Comments
 (0)