I want application to automatically logout based on expiry token.
Angular Client Code:
login(credentials) {
return this.http.post('http://something/api/login/',
credentials)
.map(response => {
let result = response.json();
if (result && result.token) {
localStorage.setItem('token', result.token);
return true;
}
return false;
});
}
Since the token conatains :
"exp": 1526016179 // expiry time
I could perhaps do this in app.component which has <router-outlet></router-outlet>
:
ngOnInit() {
let token = localStorage.getItem('token');
timer:DateTime = new Date(_JwtHelper.decodeToken(token).exp);
if (timer && (Date.now() > timer)) {
logout();
}
}
But the problem with this approach would be that it won't logout automatically. It would require some sort of activity from user such as button click to fire ngOnInit()
of app.component
(which I am not even sure if it will fire everytime there is a button click anywhere in the website, probably not).
The application should automatically log out and redirect to login page as soon as the expiry time is hit.