Technologies:
Tolerim
a day ago
The intended functionality of Express login middleware is not operational.
As a web beginner, I have implemented a sign-in feature that saves the email entered as a cookie, and an account page that deletes it when the log-out button is pressed. I am attempting to create a middleware that redirects the user to sign-in when the cookie is deleted from logging out. The sign-in route script is shown below where if the email cookie is detected, the user is redirected to the main page; otherwise, the sign-in page is rendered.
const fs = require('fs');
const path = require('path');
const { v4 } = require('uuid');
const express = require("express");
const cookieParser = require('cookie-parser');
const { containsObject, getUsers } = require('../Auth/auth.cjs');
const {userExists} = require('./signin.cjs')
const router = express.Router();
router.get('/', (req, res) => {
const {cookies} = req;
if(cookies['email']){
res.redirect('/main');
}
else{
res.render('SignIn/signin');
}
})
router.post('/', async (req, res) => {
const user = {
email:req.body.email,
password:req.body.password,
}
const result = await userExists(user);
if(result){
res.cookie('email', req.body.email);
res.redirect('/main');
}
else{
res.render('SignIn/signin', user)
}
})
module.exports = router;
Note that the log-out button is implemented using a form:
Here is the log-out route that clears the email cookie and redirects the user to the sign-in page when the log-out button is clicked.
const fs = require('fs');
const path = require('path');
const { v4 } = require('uuid');
const express = require('express');
const { route } = require('../SignIn/signin');
const { compile } = require('ejs');
const {getUser, getPostsFromUser} = require('./account.cjs')
const router = express.Router();
router.get("/", async (req, res) => {
const {cookies} = req;
const email = cookies['email'];
const userResponse = await getUser(email);
const user = {
username:userResponse['username'],
email:userResponse['email'],
}
console.log(await getPostsFromUser(user));
res.render('Account/account', user);
})
router.post('/', (req, res) => {
res.cookie('email', '', {expires: new Date(0)});
res.clearCookie("email");
res.redirect('/sign')
})
module.exports = router;
The main file intercepts the request and response objects to check if the email cookie is defined. If it is undefined, then the client is redirected to the sign-in page. The routing implementation for the different pages are also shown below.
const { localsName } = require("ejs");
const express = require("express");
const app = express();
const cookieParser = require('cookie-parser');
const users = []
let loggedIn = false
app.set('view engine', 'ejs');
app.use(cookieParser());
app.use(express.static('public'));
app.use(express.urlencoded({extended:true}))
app.use((req, res, next) => {
const {cookies} = req;
console.log(cookies['email'])
if(cookies['email'] == undefined){
return res.redirect('/sign');
}
else{
next();
}
})
app.get('/', (req, res) =>{
res.redirect('/sign')
})
const signRouter = require('./views/SignIn/signin.js');
app.use('/sign', signRouter);
const authRouter = require('./views/Auth/auth.js');
app.use('/auth', authRouter);
const mainRouter = require('./views/Main/main.js');
app.use('/main', mainRouter);
const addPostRouter = require('./views/Addpost/addpost.js');
app.use('/add-post', addPostRouter);
const accountRouter = require('./views/Account/account.js');
app.use('/account', accountRouter);
app.listen(3000)
I have attempted multiple ways to get this to work but have been unsuccessful in rendering the sign-in page after the log-out button is clicked.Answers(1)
Tolerim
a day ago
Verified Answer
It seems like you're trying to implement a middleware that redirects the user to sign in if the email cookie is deleted, but you're having trouble getting the redirect to work correctly.
Without knowing the specific error message or behavior you're seeing, it's a bit hard to diagnose the issue. However, one thing you could try is changing the middleware to explicitly check for the presence of the cookie rather than checking for undefined. You could also try adding a return before the redirect statement to make sure that it doesn't continue executing the code after the redirect. Here's what the middleware could look like with those changes:
app.use((req, res, next) => {
const { cookies } = req;
const email = cookies['email'];
if (!email) {
return res.redirect('/sign');
}
next();
});
Hopefully, this helps! Let me know if you have any follow-up questions.