Get user saved in session in EJB with NodeJS

70
January 31, 2022, at 04:00 AM

Hi im having problem with express-session, it seems like not saving my user session. Here is when I declare my session

 //Session
//app.use(express.urlencoded({extended: true}))
app.use(session({
    secret: "key per i cookie",
    saveUninitialized: false,
    resave: false,
    store: MongoStore.create({ 
        mongoUrl: process.env.URI,
        autoRemove: 'disabled'
    }),
    cookie: { maxAge:30000,secure:false }
}))
app.use(cookieParser());

app.use(function(req, res, next){
    res.locals.session = req.session.user
    next()
})

And then saving it when user is logged in

app.post('/login', async(req,res) =>{
    const { username, password } = req.body
    const user = await User.findOne({username}).lean()
    if(!user){
        return res.json({status:'error', error:'Username o password non validi'})
    }
    if(await bcrypt.compare(password, user.password)){
        const token = jwt.sign({
            id: user._id, 
            username: user.username
        },
            JWT_SECRET
        )
        req.session.user = username
        req.session.save()
        console.log('Utente loggato ' + req.session.user)
        return res.json({status:'ok', data: token})
    }
    
    res.json({status:'error',error:'Username o password non validi'})
})

Apparently in the post I save the session but after login, when trying to access in an EJB page with

<% if(session.user) { %>
     <h1>Session stored</h1>
   <% } else { %>
     <p>No session</p>
   <% }%>

I got no session stored. What im doing wrong?? Thanks in advance

READ ALSO
Revslider jQuery &quot;Long-From-Top&quot; not working

Revslider jQuery "Long-From-Top" not working

i have a revslider and want to have a long-from-top but it still looks like a short-from-top

80
How can I check if the player is in a boat? [closed]

How can I check if the player is in a boat? [closed]

Want to improve this question? Update the question so it's on-topic for Stack Overflow

73
Clicking on non keyboard-focusable element with selenium chromedriver in python

Clicking on non keyboard-focusable element with selenium chromedriver in python

First I am trying to go to this link: driverget("https://www

80
How does tp_call slot of the class&#39; metaclass invokes the actual __init__ method of the class, when initializing an instance

How does tp_call slot of the class' metaclass invokes the actual __init__ method of the class, when initializing an instance

First I'm gonna try to explain a few things and be more explicit so that I can point out what my real problem is

86