How to redirect to login page after cookie expires in Angular JS?

How to redirect to login page after cookie expires in Angular JS?

 Posted on 2015-05-18 16:04:40 by iamtheDanger
 Last Updated: Aug 24, 2015Views: 948Votes: 0

I have the following piece of code where the user is stored in the cookie. The expiry time for the cookie is also set. The user page is still showing even after the cookie has expired.

             var expired = new Date();
             expired.setTime(expired.getTime() + (60*1000));
             $cookieStore.put('user', user.username, {expires : expired });

2 answers

By Fisherman

     var expired = new Date();
             expired.setTime(expired.getTime() + (60*1000));
             $cookieStore.put('user', user.username, {expires : expired });
             $timeout(function(){
                 if(!$cookieStore.get('user')){
                 $window.location.href = 'redirectlink';
}


},60);

By sebastienbarbier

What about on page loading, you get the expiration date and use a setTimeout() to refresh it or redirect your user on an other page ?

Some ressources about settimeout :

  • https://developer.mozilla.org/en-US/docs/Web/API/WindowTimers/setTimeout
  • Stackoverflow : JavaScript,setTimeout

Best would be to display 1 minutes before expiration date a popup inviting user to update it or perform an action.

2 similar answers  β 

By TheSharpieOne

Assuming your cookie/session is not httpOnly you can poll to check if the cookie/session exists locally (document.cookie), redirecting when your cookie is not found within document.cookie. If you have the httpOnly flag set, you will not be able to see it in document.cookie. You can set a timeout for 20 minutes. Use the AngularJS's http interceptors to clear the timeout when a request is made and then create a new one.

By stefan

So, if I understand what you're asking properly, you can't really "detect" a cookie expiration per se.

If a cookie is expired, the browser will not send it to the server, which is to say, cookie expiration actually happens on the browser side, not the server side.

However, if you wanted to "fake" it, you might try something like the following psuedocode:

on "login":
    set a cookie that expires in 100x \
    the amount of time it takes your session to expire

on "ensureAuthenticated":
    check for req.user
    if i don't have req.user but have my other cookie
        Fire my expiration event, which unsets the other cookie
    if i don't have req.user or my other cookie
        Send the user to the login page
    if i've got req.user
        Re-set my other cookie so that i update the expiration

Might not be 100% of what you're looking for, but should be close.

Asp.net persistent login cookie expires randomly
 LudovicoVotes: 0 Relevancy: 93%
I'm trying to implement a login form with the remember me functionality in ASP.NET 4.0. I've set the timeout option in the web.config to 1 year (525600), but after a random amount of time after I logon, I always get logged off. The cookie is created correctly, I can see it in the browser with the right expire value (september 2014), but it seems that this cookie after some time is not readed by the ASP.NET environment anymore. I tryed to login with. or. or with this custom code.
Spring security and angular javascript redirect to login page
 Freaky ThommiVotes: 1 Relevancy: 90%
Hi I am facing a problem with spring security+Spring MVC+angular javascript.. When the session is invalid I want to request the (whole) page to get redirected to login page. I tried But its not working since its a one page app... Or any suggestion to handle sessiontimeout in angular javascript single page app Any help ?
How to make entire page redirect to login after session expires in mvc?
 VrankelaVotes: 0 Relevancy: 87%
I have this attribute overridden and decorated it on each controller which does its job perfectly on all normal views. And I have this one of many cases where I need to update a certain partial view within a parent view. What happens here is that if the session "LoggedUserID" is expired, what it will do is it will only redirect that div container divSensorNames (where the partial view is) to the login page but leave the entire parrent page stuck where it was.
how to Redirect to login page when session expires asp.net MVC 5 ASP.net Identity 2.0
 Harshadaa HarshaVotes: 1 Relevancy: 84%
I have created default webproject in VS2013 with latest ASP.net Identity 2.0. I would like to know what setting are reuired to change the timeout of logging session so that I can test pratically.
Redirect to login page when session expires + silex
 nielsvVotes: 0 Relevancy: 81%
I want to redirect to my login page when my session is expired. What happens now. When you are for example logged in and at the dashboard, you will see you're username in the right top corner. But when you don't do anything for 2 hours or so and then refresh you get an error that it's impossible to access an attribute 'username' of NULL. This comes because you're logged out...
how to make redirect when the the login limit expires?
 DaveczVotes: 0 Relevancy: 78%
on my page I use a lot of Jquery-ajax to some user action (sending messages in system, adding comments to articles etc.). This functionality is allow only for registered a logged users. for example. In my web.config I set login timeout to 30 minutes. When a user writes a longer comment and the login limit expires, the application does not redirect to the login (eg. http. //localhost/Account/Logon?ReturnUrl....). How do I make a redirect in this case?
Auto Logout And Redirect To Login Page
 SumanPallaVotes: 0 Relevancy: 75%
Hi, I need code for auto redirect to Login page when session expires. here need to consider the following things. 1)when page is in idle mode[if session expires auto redirect to login screen and kill the session] 2) when user performing any action increase the session time. Note . when page in idle mode then only auto redirect to login screen when session expires. How it can be possible? Please help me.
Redirect to a login page if the user is not yet logged in Angular JS
 userit1985Votes: 0 Relevancy: 72%
How can I prevent user enter App url's if he doesn't logged. App.js. How can I get indocation if user log-in? Moreover How can I prevent user to enter pages through url, how can I hide the url?
How do I redirect to login page if authentication is required in angular-ui-router?
 chovyVotes: 0 Relevancy: 69%
My state.go('login') is executing but page is not changing the page when I go to /#/videos. Its looking for the authenticate flag and if present and user is not logged in, I'm trying to redirect them to the login page.
How Do You Break A JavaScript Redirect To Mobile?
 JasonVotes: 0 Relevancy: 66%
I have created a simple script that will detect the users device and if that device is mobile than it will redirect to the mobile side. When you are on mobile the site will create a cookie that should essentially break the redirect when you visit desktop again. Below is the code on the main website. The cookie is successfully being instantiated on the mobile side with the following code. jQuery.cookie('mobilea',1, {path. '/'}); Any ideas?
Get root website url in javascript for redirect
 InbalVotes: 4 Relevancy: 63%
I want to redirect to Login page from every page in my website after session timeout.I try to set window.location to the login page. but "~" seems to be unsupported.my Login page is located under my root folder. for example. *http. //server/website/*Login.aspx How can I get this url in javascript? Thanks a lot, Inbal.
javascript redirect to a page after login
 vicentiubVotes: 0 Relevancy: 60%
Hi, I want to redirect the user to a page that needs authentication without asking his username and password. I want to do thisautomatically using javascript form submit (I have valid username and password) and then redirect the user to the page that he asked. Thank you! Vicentiu
node express res.redirect without hash
 omgMayaVotes: 2 Relevancy: 57%
The page url is http. //example.com/dashboard#/catalog. When the cookie expires, I do a And the resulting url in the browser is http. //example.com/login#/catalog. How could I redirect without the hash part i.e Make the url to be http. //example.com/login Thanks for your help!!
Javascript to delete JSESSIONID cookie
 RanPaulVotes: 0 Relevancy: 54%
Am trying to delete JSESSIONID cookie as below in my login page since IE10 is sending expired JSESSIONID cookie to server. Whenever I reload the page I see JSESSIONID = A64F97BF3AF662AC56238F2C23D529AA in the console log instead of JSESSIONID = A64F97BF3AF662AC56238F2C23D529AA=; expires=Thu, 01 Jan 1970 00. 00. 01 GMT; Can someone please help me to fix this issue?
redirecting to external page | angular
 coderVotes: 0 Relevancy: 51%
I have an angular app that I need to secure. We have a central SSO app in our organisation.This central app provides a login page and sets cookie after authenticating the user. Now I need to redirect user from my angular app to this central login page in case authentication cookie is missing/expired. Can someone please help, how can I redirect to a different page using angular routing ? It seems angular always redirect only relative to base url.
How can Javascript redirect a website user back to login page if need be?
 HelpingHandVotes: 0 Relevancy: 48%
If a website user attempts to enter a secure page before entering the login page for the whole website, what script will be able to redirect him back to the login page without hindering his usage of the page normally (after he logged in)? I am trying to do this with javascript and cookies. You guys have any ideas? Thanks ahead of time. I need the cookie creation and detection scripts. Thank you.
How to show popup window when cookie expires using forms authentication in asp.net
 SHAMIM AHMEDVotes: 0 Relevancy: 45%
I have to redirect to another page in another application. How to show popup window when cookie expires using forms authentication in asp.net so that before redirect to another page. i.e. In which http. module should I inject logic as cookie is expired and redirect to another application page. Thanks in advance.
Disable Session regeneration in Yii2
 d.raevVotes: 0 Relevancy: 42%
I have an Yii2 application which is doing most of the work through an external API, including the authentication and most of the read/update requests. After the login I store a some encrypted values in the session, and store the encryption-key for them in a cookie. The problem is, the cookie (or the session?).. expires after 30+ min of inactivity .. but the session is automatically regenerated (user is not prompted to login),but the retreaving of the values is broken.
How to secure multiple applications via one session login
 HiteshVotes: 0 Relevancy: 39%
I am working on multiple .NET applications residing on same server. User can login using there credentials and have links to multiple other applications. The problem is, these applications, except the primary one, are accessible without any authentication as well, if url is known. I want to bring all these applications under same security authentication. Currently I am setting up a cookie on login which expires in 10 hours. can i use the same cookie in other applications. if yes, then how.

你可能感兴趣的:(web)