lets say i have code like this where i have post on loginuser. This code should work normally but i want to ask you, if there is way of how to pass some information from this post to another get or post. For example my next get would be like this:
app.get("/somepage", function(request,response){
if(user is logged in)
response.render("page");
else
response.render("login);
}
And i want to pass something from my post loginuser so i can check if user is loggedin so he can visit this somepage or if he is not he will be redirected to login page.
app.post("/loginuser", async function(request, response){
try {
var email = request.body.email;
var password = request.body.password;
if(!email || !password){
return response.status(400).render("login",{
message: "Provide an email and password"
})
}
db.query("SELECT * FROM users WHERE email = ?", [email], async function(error,results){
if(!results || !(await bcrypt.compare(password, results[0].password))){
return response.status(401).render("login",{
message: "Email or password incorrect"
})
}
else{
const id = results[0].id;
const token = jwt.sign({id:id}, process.env.JWT_SECRET,{
expiresIn: process.env.JWT_EXPIRES_IN
})
const cookieOptions = {
expires: new Date(
Date.now() + process.env.JWT_COOKIE_EXPIRES * 24 * 60 * 60 * 1000
),
httpOnly: true
}
response.cookie("jwt", token, cookieOptions);
response.render("home");
}
})
} catch (error) {
console.log(error);}})