AngularJS: how to log out when cookie expires

The angularjs application is on my index.cshtml page. When the user first gets to the index.cshtml page, if they are not logged in, he redirects them to the login page. When you log in, the system redirects them to the angular application. The registration session expires in 20 minutes. When users start login sessions, I need the angular application to be redirected to the login page, like a regular web application. Is there any way for angular to check the login / cookie session for each action? For example, if a user clicks a button or goes to another route and the session has expired, they should be redirected to the login page.

In addition, the user must update the browser so that he logs out and is redirected to the login screen.

+4
source share
1 answer

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 in document.cookie. If you have the httpOnly flag set, you won’t be able to see it in document.cookie. You can set a timeout within 20 minutes. Use AngularJS http interceptors to clear the timeout when the request is made, and then create a new one.

+2
source

Source: https://habr.com/ru/post/1539467/


All Articles