I am developing a web app with react front-end and node baack-end
I want to delete the cookie being stored so that I can successfully be able to logout the user. I have tried using the js-cookie library and react-cookie library but it does not work.
nav_component:
JavaScript
x
64
64
1
import React from "react";
2
import { Link, useHistory } from "react-router-dom";
3
import { useSelector } from "react-redux";
4
import { NavDropdown } from "react-bootstrap";
5
import Cookies from "js-cookie";
6
7
function Nav() {
8
const user = useSelector((state) => state.user);
9
const { name, id, loginId } = user;
10
const history = useHistory();
11
12
function logout() {
13
Cookies.remove("userId", { path: "/", domain: "localhost" });
14
localStorage.clear();
15
history.push("/login");
16
}
17
18
return (
19
<nav className="navbar navbar-expand navbar-light fixed-top">
20
<ul class="nav">
21
<li class="nav-item">
22
<Link to={"/"} class="nav-link active" aria-current="page">
23
Home
24
</Link>
25
</li>
26
<li class="nav-item">
27
<Link to={"/register"} class="nav-link">
28
Register
29
</Link>
30
</li>
31
<li class="nav-item">
32
<Link to={"/login"} class="nav-link">
33
Login
34
</Link>
35
</li>
36
<li class="nav-item">
37
<Link
38
to={"/admin"}
39
class="nav-link"
40
tabindex="-1"
41
aria-disabled="true"
42
>
43
Admin
44
</Link>
45
</li>
46
</ul>
47
{name ? (
48
<ul class="username">
49
<li>
50
<NavDropdown title={name}>
51
<NavDropdown.Item>
52
{id}: {loginId}
53
</NavDropdown.Item>
54
<NavDropdown.Item onClick={logout}>Logout</NavDropdown.Item>
55
</NavDropdown>
56
</li>
57
</ul>
58
) : null}
59
</nav>
60
);
61
}
62
63
export default Nav;
64
How I set the cookie in the backend:
JavaScript
1
52
52
1
app.use(cookieParser());
2
app.use(
3
express.urlencoded({
4
extended: true,
5
})
6
);
7
8
app.use(
9
session({
10
key: "userId",
11
secret: "subscribe",
12
resave: false,
13
saveUninitialized: false,
14
cookies: {
15
expires: 60 * 60 * 8,
16
},
17
})
18
);
19
app.get("/login", (req, res) => {
20
if (req.session.user) {
21
res.send({ loggedIn: true, user: req.session.user });
22
} else {
23
res.send({ loggedIn: false });
24
}
25
});
26
27
app.post("/login", (req, res) => {
28
const LoginID = req.body.LoginID;
29
const Password = req.body.Password;
30
db.query(
31
"SELECT * FROM usermst WHERE LoginID = ? AND password = ?",
32
[LoginID, Password],
33
(err, result) => {
34
if (err) {
35
res.send({ err: err });
36
}
37
38
if (result.length > 0) {
39
const userid = result[0].UserID;
40
const token = jwt.sign({ userid }, "jwtSecret", {
41
expiresIn: 60 * 60 * 24,
42
});
43
req.session.user = result;
44
45
res.json({ auth: true, token: token, result: result });
46
} else {
47
res.send({ message: "Wrong LoginID/Password combination" });
48
}
49
}
50
);
51
});
52
It should be noted that in my backend there is some code related to JWT tokens but I have not yet implemented that in my front-end. The only thing that I have implemented is the cookie and session.
Advertisement
Answer
In my backend I added this code for logging out to which I made an axios reques from the frontend:
JavaScript
1
10
10
1
res.clearCookie("userId", { path: "/" });
2
res
3
.status(200)
4
.json({ success: true, message: "User logged out successfully" });
5
};
6
7
app.get("/logout", logout, (req, res) => {
8
res.send("You have been logged out successfully");
9
});
10
Whereby “userId” id the name of the cookie