J-man
J-man

Reputation: 1833

How to Handle Session Timeout in Angular 2 / Java Spring Boot

We have an angular 2 application with a java spring boot backend. In the spring backend, we have set in the YAML config file a session-timeout of 1 hour (for testing purposes, I changed it to 1 minute). Once the session times out, the token is no long authenticated and the user needs to login again to resume/start the application.

Once the session times out, the angular app should redirect the user to the login page. It should do this regardless of user activity/idle state.

The thing I am stuck on is how to redirect the user in the angular 2 app to the login page once the session times out. I am wondering where and how to implement this? In a service class? In a user service class (I have a user service class that retrieves logged in user info from the spring backend). Does every component in the app need to check for session state?

Wondering best practices with angular 2.

Thanks!

Upvotes: 1

Views: 9490

Answers (1)

Ashish Bajpai
Ashish Bajpai

Reputation: 281

Once user is logged in, you can start a one hour timer (can use Observable.timer), and navigate to login/logout (using router.navigate) at timer completion.

Or you can create custom Http service and use that for all backend calls. In custom service, navigate to logout/login after receiving error from backend. Refer https://stackoverflow.com/a/44929725/7710727

Upvotes: 3

Related Questions