* The subscription for `error` is not required, because the subscription for
`username` is also triggered, when the username is reset after an error.
* The `login` function must not manipulate the local variable `username`
at all.
* The separate logic in the app-components's init-hook, that redirects
to the login page, when the user is not yet known, is no longer
necessary, since the routing can also be triggered in the subscription
for `username`.
private authService = inject(AuthService);
title = 'stored-login';
-
- ngOnInit(): void
- {
- this.authService.assertUserIsKnown();
- }
}
constructor() {
this.store.select(selectUsername).subscribe(username => {
this.username = username;
- this.router.navigate(['']);
+ if (username) {
+ this.router.navigate(['']);
+ }
+ else {
+ this.router.navigate(['login']);
+ }
});
- this.store.select(selectError).subscribe(error => (this.username = undefined));
}
login(username : string, password : string) : Observable<string> {
if (shouldFail) {
return throwError(() => new Error('Simulierter Fehler im Service'));
}
- this.username = username;
return of(username).pipe(delay(1000));
}
-
- assertUserIsKnown(): void {
- if(!this.username) {
- this.router.navigate(['login']);
- }
- }
}