<button type="submit" [disabled]="isLoading || form.invalid">Login</button>
</form>
<div *ngIf="error">{{ error }}</div>
-<div *ngIf="token">Welcome, {{ form.value.username }}! Your token is {{ token }}</div>
import { FormBuilder, FormGroup, ReactiveFormsModule, Validators } from "@angular/forms";
import { Store } from '@ngrx/store';
import { login } from '../store/login.actions';
-import { selectError, selectIsLoading, selectToken } from '../store/login.selectors';
+import { selectError, selectIsLoading } from '../store/login.selectors';
@Component({
selector: 'app-login',
form: FormGroup;
error: string|null = null;
- token: string|null = null;
isLoading : boolean = false;
constructor(private fb: FormBuilder) {
}
ngOnInit() {
- this.store.select(selectToken).subscribe(token => (this.token = token));
this.store.select(selectError).subscribe(error => (this.error = error));
this.store.select(selectIsLoading).subscribe(isLoading => (this.isLoading = isLoading));
}
import { Router } from "@angular/router";
import { Observable, of, throwError } from 'rxjs';
import { delay } from 'rxjs/operators';
+import { Store } from '@ngrx/store';
+import { selectError, selectToken } from '../store/login.selectors';
@Injectable({
providedIn: 'root'
})
export class AuthService {
private router = inject(Router);
+ private store = inject(Store);
private username : string|undefined;
+ ngOnInit() {
+ this.store.select(selectToken).subscribe(username => {
+ this.username = username;
+ this.router.navigate(['']);
+ });
+ this.store.select(selectError).subscribe(error => (this.username = undefined));
+ }
+
login(username : string, password : string) : Observable<string> {
const shouldFail = Math.random() < 0.3; // 30% Fehlerchance
if (shouldFail) {