Skip to content

Commit 0abc99a

Browse files
author
WebDeveloperGuide
committed
Logout functionality added, Fix toast message was not show after applied private router.
1 parent b6b19c5 commit 0abc99a

File tree

7 files changed

+23
-15
lines changed

7 files changed

+23
-15
lines changed

web_admin/src/App.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,14 @@ import Users from "./components/pages/user/Users";
1010
import NotFound from "./components/pages/NotFound";
1111
import AuthRoute from "./AuthRoute";
1212
import PrivateRouter from "./PrivateRouter";
13+
import Toast from "./components/LoadingError/Toast";
1314
import './App.css';
1415
import "react-toastify/dist/ReactToastify.css";
1516

1617
function App() {
1718
return (
1819
<>
20+
<Toast/>
1921
<Router>
2022
<Switch>
2123
<PrivateRouter path="/" component={Dashboard} exact />

web_admin/src/AuthRoute.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@ function AuthRoute({ component: Component, ...rest }) {
77
const { userInfo } = userLogin;
88
let isAdmin = 0;
99
if (userInfo){
10-
isAdmin = userInfo.data[0].isAdmin;
10+
isAdmin = (userInfo.data[0].isAdmin === true) ? 1 : 0;
1111
}
1212

1313
return (
1414
<Route
1515
{...rest}
1616
component={(props) => {
17-
if (userInfo && isAdmin) {
17+
if (isAdmin === 1) {
1818
return <Redirect to={`/dashboard`} />;
1919
} else {
2020
return <Component {...props} />;

web_admin/src/PrivateRouter.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@ function PrivateRouter({ component: Component, ...rest }) {
77
const { userInfo } = userLogin;
88
let isAdmin = 0;
99
if (userInfo){
10-
isAdmin = userInfo.data[0].isAdmin;
10+
isAdmin = (userInfo.data[0].isAdmin === true) ? 1 : 0;
1111
}
1212

1313
return (
1414
<Route
1515
{...rest}
1616
component={(props) => {
17-
if (userInfo && isAdmin) {
17+
if (isAdmin === 1) {
1818
return <Component {...props} />;
1919
} else {
2020
return <Redirect to={`/login`} />;

web_admin/src/components/Header.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,17 @@
1-
import {Link} from 'react-router-dom';
1+
import {Link,useHistory} from 'react-router-dom';
2+
import { logout } from "../redux/actions/userActions";
3+
import { useDispatch } from "react-redux";
4+
25

36
const Header = () => {
7+
const dispatch = useDispatch();
8+
const history = useHistory();
9+
10+
const logoutHandler = () => {
11+
dispatch(logout());
12+
history.push("/login");
13+
};
14+
415
return(
516
<nav className="navbar default-layout col-lg-12 col-12 p-0 fixed-top d-flex align-items-top flex-row">
617
<div className="text-center navbar-brand-wrapper d-flex align-items-center justify-content-start">
@@ -164,7 +175,7 @@ const Header = () => {
164175
<a className="dropdown-item"><i className="dropdown-item-icon mdi mdi-message-text-outline text-primary me-2" /> Messages</a>
165176
<a className="dropdown-item"><i className="dropdown-item-icon mdi mdi-calendar-check-outline text-primary me-2" /> Activity</a>
166177
<a className="dropdown-item"><i className="dropdown-item-icon mdi mdi-help-circle-outline text-primary me-2" /> FAQ</a>
167-
<a className="dropdown-item"><i className="dropdown-item-icon mdi mdi-power text-primary me-2" />Sign Out</a>
178+
<Link className="dropdown-item" to="#" onClick={logoutHandler}><i className="dropdown-item-icon mdi mdi-power text-primary me-2"/>Log Out</Link>
168179
</div>
169180
</li>
170181
</ul>

web_admin/src/components/pages/Login.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import { Link } from "react-router-dom";
22
import {useState, useEffect} from 'react';
33
import { useDispatch, useSelector } from "react-redux";
44
import Loading from "../LoadingError/Loading";
5-
import Toast from "../LoadingError/Toast";
65
import { Login } from "../../redux/actions/userActions";
76
import Message from "./../LoadingError/Error";
87

@@ -48,10 +47,7 @@ const LoginPage = ({history}) =>{
4847
}, [userInfo, history]);
4948

5049
return(
51-
<>
52-
<Toast />
53-
{error && <Message variant="alert-danger">{error}</Message>}
54-
{loading && <Loading />}
50+
<>
5551
<div className="container-scroller">
5652
<div className="container-fluid page-body-wrapper full-page-wrapper">
5753
<div className="content-wrapper d-flex align-items-center auth px-0">

web_admin/src/redux/actions/userActions.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,9 @@ export const Login = (email, password) => async (dispatch) => {
8888
});
8989
} else {
9090
dispatch({ type: USER_LOGIN_SUCCESS, payload: data });
91+
localStorage.setItem("userInfo", JSON.stringify(data));
9192
}
92-
93-
localStorage.setItem("userInfo", JSON.stringify(data));
93+
9494
} catch (error) {
9595
const message =
9696
error.response && error.response.data.message

web_services/routes/auth.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,7 @@ router.post("/login",async (req,res)=>{
3636
}else{
3737
const hashedPassword = CryptoJS.AES.decrypt(user.password, process.env.PASS_SECRET);
3838
const originalPassword = hashedPassword.toString(CryptoJS.enc.Utf8);
39-
console.log("password",req.body.password)
40-
console.log("originalPassword",originalPassword)
39+
4140
if(req.body.password!=originalPassword){
4241
res.status(200).json({success:0,message:"Invalid Email or Password"});
4342
}else{

0 commit comments

Comments
 (0)