简体   繁体   中英

AngularJS: How to logout when login cookie expires

The angularjs application is on my index.cshtml page. When the user first hits the index.cshtml page, if they are not logged in it will redirect them the login page. When they are logged in the system will redirect them to the angular application. The login session expires after 20 minutes. When users login session expies I need the angular application to redirect to the login page just like a regular web application. Is there a way for angular to check the login session/cookie on every action? For example, if a user presses a button or navigates to a different route and the session has expired they should be redirected to the login page.

Also, the user must refresh the browser for them to logout and be redirected to the login screen.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM