Basically what you need to do is to set a flag in case of any client activity and than after 30 minutes you have to check for that flag. If flag wasn't set which means user wasn't being active so you can perform a logout()
action.
Here is some code sample(using ngrx) that you might find useful.
export class ClientActiveService {
constructor(
private store: Store<fromRoot.State>,
) { }
run() {
window.onload = () => { this.setActive(); };
window.onmousemove = () => { this.setActive(); };
window.onmousedown = () => { this.setActive(); };
window.onclick = () => { this.setActive(); };
window.onscroll = () => { this.setActive(); };
window.onkeypress = () => { this.setActive(); };
}
setActive() {
this.store.select(fromRoot.getClientActive)
.take(1)
.subscribe((active) => {
if (!active) {
this.store.dispatch(new layout.ClientActiveAction());
}
});
}
}
ClientActiveService
is a service that just emmiting an action if client was active. Somewhere like in app.component.ts
you have to inject that service and call this.clientActiveService.run();
Then somewhere in your code you have to setup a 30 minutes timer where you subscribe for an ClientInactiveAction
action
setInterval(() => {
this.store.select(fromRoot.getClientActive)
.take(1)
.subscribe((active) => {
if (!active) {
this.auth.logout();
}
});
}, 30 * 60 * 1000);
If you are not using ngrx you can just set a variable/flag
instead in
ClientActiveService
service. Then in setTimeout()
just check for
that variable and perform your logout()
action
Otherwise you might wanna use ng2-idle library. In that case Angular 2 - Logout using ng2-idle might help.