My problem here is when the user is already logged in, I want to change my navbar to logout. I am using Nodejs and Ejs templates for my web project. My code is below. please help me to figure this out. I am also using Level 1 Authentication. I am trying to do basic first before I do the hashing. Thank you very much!
APP.JS
//jshint esversion:6
const express = require("express");
const https = require("https");
const bodyParser = require("body-parser");
const mongoose = require("mongoose");
const res = require("express/lib/response");
const app = express();
app.use(express.static("public"));
app.set("view engine", "ejs");
app.use(bodyParser.urlencoded({ extended: true }));
//TODO
mongoose.connect("mongodb://localhost:27017/barangayportalDB", { useNewUrlParser: true });
const userSchema = {
firstname: String,
lastname: String,
email: String,
password: String,
conpassword: String,
role: String
};
const User = new mongoose.model("User", userSchema);
app.get("/", function (req, res) {
res.render("main");
});
app.get("/login", function (req, res) {
res.render("login");
});
app.get("/register", function (req, res) {
res.render("register");
});
app.get("/logout", async (req, res) => {
res.redirect("/")
});
app.post("/login", function (req, res) {
const username = req.body.username;
const password = req.body.password;
const role = "admin";
User.findOne({ email: username }, function (err, foundUser) {
if (err) {
console.log(err);
} else {
if (foundUser) {
if (foundUser.role === role) {
res.render("adminportal")
}
else if (foundUser.password === password) {
res.render("portal")
}
}
}
})
});
app.post("/register", function (req, res) {
const newUser = new User({
firstname: req.body.firstname,
lastname: req.body.lastname,
email: req.body.username,
password: req.body.password,
conpassword: req.body.conpassword
});
newUser.save(function (err) {
if (err) {
console.log(err);
} else {
res.render("portal")
}
});
});
//---------------------------------------
app.listen(3000, function () {
console.log("Server started on port 3000");
});
Here is my Navbar.ejs This is where I want to change my login to signout.
<nav class="navbar navbar-expand-lg navbar-light">
<div class="container">
<a class="navbar-brand" href="/">Barangay Portal</a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarSupportedContent"
aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav me-auto mb-2 mb-lg-0">
<li class="nav-item">
<a class="nav-link active" aria-current="page" href="#home">Home</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#News">About</a>
</li>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-bs-toggle="dropdown"
aria-expanded="false">
Request
</a>
<ul class="dropdown-menu" aria-labelledby="navbarDropdown">
<li><a class="dropdown-item" href="/reqbrgyid">Dropdown</a></li>
<li><a class="dropdown-item" href="#">Drop Down</a></li>
<hr class="dropdown-divider">
</li>
<li><a class="dropdown-item" href="#">Other</a></li>
</ul>
</li>
</ul>
<ul class="navbar-nav">
<li class="nav-item navbar-distance">
//this i want to change this to Sign out and remove the register button as well
<a class="nav-link " href="/login">Sign in</a>
</li>
<li class="nav-item navbar-distance">
<a class="nav-link nav-border-without" href="#contacts">Talk to Us</a>
</li>
<li class="nav-item navbar-distance">
<a class="nav-link nav-border-with" href="/register">Register</a>
</li>
</ul>
</div>
</div>
</nav>