From 48baf2e5d81c5d17977de868425e90d680878bdb Mon Sep 17 00:00:00 2001 From: Kai Moritz Date: Mon, 15 Sep 2025 22:01:29 +0200 Subject: [PATCH] redirect to home on successful login --- src/app/login/login.component.html | 1 - src/app/login/login.component.ts | 4 +--- src/app/services/auth.service.ts | 11 +++++++++++ 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/src/app/login/login.component.html b/src/app/login/login.component.html index 6ca848a..948fb29 100644 --- a/src/app/login/login.component.html +++ b/src/app/login/login.component.html @@ -12,4 +12,3 @@
{{ error }}
-
Welcome, {{ form.value.username }}! Your token is {{ token }}
diff --git a/src/app/login/login.component.ts b/src/app/login/login.component.ts index 8de7ea8..bbaf374 100644 --- a/src/app/login/login.component.ts +++ b/src/app/login/login.component.ts @@ -3,7 +3,7 @@ import { CommonModule } from '@angular/common'; 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', @@ -19,7 +19,6 @@ export class LoginComponent { form: FormGroup; error: string|null = null; - token: string|null = null; isLoading : boolean = false; constructor(private fb: FormBuilder) { @@ -30,7 +29,6 @@ export class LoginComponent { } 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)); } diff --git a/src/app/services/auth.service.ts b/src/app/services/auth.service.ts index 9c39495..959b7c4 100644 --- a/src/app/services/auth.service.ts +++ b/src/app/services/auth.service.ts @@ -2,15 +2,26 @@ import { inject, Injectable } from '@angular/core'; 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 { const shouldFail = Math.random() < 0.3; // 30% Fehlerchance if (shouldFail) { -- 2.39.5