My goal is to have the application automatically log out based on an 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;
});
}
The token includes an expiry time:
"exp": 1526016179 // expiry time
I thought of implementing this in the app.component which contains
<router-outlet></router-outlet>
:
ngOnInit() {
let token = localStorage.getItem('token');
timer:DateTime = new Date(_JwtHelper.decodeToken(token).exp);
if (timer && (Date.now() > timer)) {
logout();
}
}
However, a potential issue with this approach is that it would not logout automatically and would require user activity such as pressing a button to trigger the ngOnInit()
function in app.component
(which may not be guaranteed to fire every time a button is clicked throughout the website).
The desired functionality is for the application to automatically log out and redirect to the login page once the expiry time is reached.