1

i have node and react application. for middleware i am using expressjs.

i am trying to redirect to login page after session time out. i am using express-session for authentication, i am able to get session timeout but not able to redirect to login page.

My login url is http://localhost:8009/login which is implemented in react js for ui part. app.use('/', login); router in node and expressjs

below is my code.

var session = require('express-session');

app.use(session({
    secret: 'Test Service',
    name: "test",
    saveUninitialized: true,
    resave:false,
    cookie: {
        //maxAge: 24 * 60 * 60 * 1000,
        path: '/login',
        maxAge: 1 * 10 * 60 * 1000, 
        overwrite: false    
    }
}));

can anyone help me on this. i have also checked below urls but not able to get the solutions.

How to keep the session active when redirecting from domain to subdomain in Express js/ https://gitlab.com/dinh.dich/express-session-expire-timeout#README

Sangram Badi
  • 4,054
  • 9
  • 45
  • 78

1 Answers1

1

In my opinion, here are two options you have

1 - if you are talking about session and redirecting if there is no session :

Create a function in your router as such :

module.exports.home = (req,res)=>{
if(!req.session.user){
    //index is the main page without any session
    res.render('index');
}
else{
    //render to a different page
}
};

Hopefully this will give you a better idea. Good luck !

AmirHossein Rd
  • 393
  • 4
  • 13
  • i have tried this but is there any json key or attribute present by which i can redirect to other page ? – Sangram Badi Apr 30 '18 at 07:58
  • I'm sorry, I don't quite understand what you mean. Can you give an example of what u mean – AmirHossein Rd Apr 30 '18 at 07:59
  • `cookie: { path: '/login', maxAge: 1 * 10 * 60 * 1000, overwrite: false, anyKey: '/redirectPtah' }` please check `anyKey: '/redirectPtah'`. is there any key present for redirection ? – Sangram Badi Apr 30 '18 at 08:00
  • Well , imo that wouldn't work because those are properties of the cookie object,meaning even if could do anykey: '/redirectpath' , that is still not associated with the maxage. The properties are independent of each other – AmirHossein Rd Apr 30 '18 at 08:06
  • 1
    Also even if you could do it, your code wouldn't be clean, it's a better practice to handle routing through express routing – AmirHossein Rd Apr 30 '18 at 08:07
  • Hi @SangramBadi if your code for this question is on github then please provide a link, it'll be very helpful. I'm trying to create an application using Nodejs And Reactjs which has sessions and cookies and i'm unable to find a good resource to learn from – mk1024 Jun 16 '18 at 05:36
  • sorry @mk1024, it's not possible to give access, because it's our organization's application. if you want me to create like this application you can check my link also https://www.fiverr.com/sangrambadi/create-restful-api-and-ui-for-you-using-nodejs?arrived_from_manage_gigs=true&display_share=true – Sangram Badi Jun 16 '18 at 06:44