I would say the auth service is responsible holding, managing, and exposing the state of user authentication. By this I am saying the auth service should not care about user idleness at all. This is the responsibility of the idle service.
So I would create a BehaviorSubject in the auth service that broadcasts the state of the user authentication. The idle service can use the subject to determine when an idle timer needs to start/stop.
export class AuthService {
user: BehaviorSubject<boolean> = new BehaviorSubject<boolean>(false);
logIn(){
// logic to log user in
this.user.next(true);
}
logOut(){
// logic to log user out
this.user.next(false);
}
}
Now the idle service can inject the auth service and determine how to handle the idle timer
export class IdleService {
constructor(private authService: AuthService){
this.authService.user.subscribe(res => {
// watch user authentication state and determine how to handle idle timer
});
}
idleOut(){
this.authService.logOut();
}
}
For this to work you will have to inject the IdleService on app load (for example in the app.component so the IdleService constructor method runs. I am not sure if this is the most elegant solution, but this is the first thing that comes to mind. Here is a very basic stackblitz demoing this concept.