Being new to web development, I am keeping my expectations low.
I have a sign-in page that saves the entered email as a cookie. The account page deletes this cookie when the user logs out by pressing a logout button.
I am attempting to create middleware that will redirect the user to the sign-in page if the cookie is deleted (i.e., the user logs out).
Everything works as intended when the cookie is set up. However, pressing the logout button does not render the sign-in page.
In the sign-in route, the script looks like this:
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;
The logout button is a form element! - I couldn't come up with anything easier.
<form method="post">
<input type="submit" value="Log out"/>
</form>
Logout route:
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) => {
// const {cookies} = req;
res.cookie('email', '', {expires: new Date(0)});
res.clearCookie("email");
res.redirect('/sign')
})
module.exports = router;
Main file:
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/Add_post/add_post.js');
app.use('/add-post', addPostRouter);
const accountRouter = require('./views/Account/account.js');
app.use('/account', accountRouter);
app.listen(3000)
I've attempted various approaches, but I can't seem to figure out why the page won't render smoothly.