-1

I have problem with auto logout when the user is inactive. I have a logout button and a redirect homepage "/"

app.js

app.get('/logout',userContro.user_logout);

userController.js

user_logout = function(req,res){
   res.clearCookie("userId"); //clear cookie
   res.redirect('/'); 
};

What I need is to redirect to the homepage when the user is inactive for 5 minutes. I can try using setInterval when the user logIn and counter and then return true if timeout, but I don't know how to add the function to redirect to the homepage.

I found Session auto logout after inactivity but it is still not working for me.

Thanks, everyone so much!

Andrew Hardiman
  • 929
  • 1
  • 15
  • 30
hhm95
  • 1
  • 1
  • 3

2 Answers2

2

Using the session library of npm will work on this situation.

Install it using the following command.

npm install express-session

Then, require it and use it in the app.js as follow

var session=require('express-session')
app.use(session({secret:"Key",cookie:{maxAge:600000}}))

Here, the maxAge is a property that automatically session out when there is no request come from the user within the specified time.

CodeBug
  • 1,649
  • 1
  • 8
  • 23
Sivaprasad
  • 119
  • 10
  • Do we had to validate in the client side if the cookie exists? to keep the user logged in or log him out – German May 06 '21 at 23:16
0

I done with handling in file js fontend:

setInterval(check_Mouse,1000); //check every second
var x=0;
var cnt=0;
function myFunction(e) {
    cnt=0;      //onmousemove
}
function check_Mouse(){
if(cnt==120){   //number second to logout
    cnt=0;
    window.location.href="/";   //go to homepage
}else{
    cnt+=1;   //if not timeout, cnt++
}
}
hhm95
  • 1
  • 1
  • 3