From e543a2f4a1557a486718ad0e7c49b64b157a1aea Mon Sep 17 00:00:00 2001 From: Bastian Wagner Date: Fri, 19 Jun 2026 08:59:30 +0200 Subject: [PATCH] redirect --- listify-client/src/app/app.routes.ts | 9 +++++---- listify-client/src/app/auth/login/login.component.ts | 10 ++++++++-- .../src/app/auth/register/register.component.ts | 10 ++++++++-- listify-client/src/app/auth/unauth.guard.ts | 10 ++++++++++ .../app/auth/verify-email/verify-email.component.ts | 8 +++++++- 5 files changed, 38 insertions(+), 9 deletions(-) create mode 100644 listify-client/src/app/auth/unauth.guard.ts diff --git a/listify-client/src/app/app.routes.ts b/listify-client/src/app/app.routes.ts index 8f19504..53e7fda 100644 --- a/listify-client/src/app/app.routes.ts +++ b/listify-client/src/app/app.routes.ts @@ -1,5 +1,6 @@ import { Routes } from '@angular/router'; import { authGuard } from './auth/auth.guard'; +import { unauthGuard } from './auth/unauth.guard'; import { LoginComponent } from './auth/login/login.component'; import { RegisterComponent } from './auth/register/register.component'; import { AccountComponent } from './account/account.component'; @@ -10,10 +11,10 @@ import { TemplatesComponent } from './templates/templates.component'; import { TemplateDetailComponent } from './templates/template-detail/template-detail.component'; export const routes: Routes = [ - { path: '', pathMatch: 'full', redirectTo: 'login' }, - { path: 'login', component: LoginComponent }, - { path: 'register', component: RegisterComponent }, - { path: 'verify-email', component: VerifyEmailComponent }, + { path: '', pathMatch: 'full', redirectTo: 'lists' }, + { path: 'login', component: LoginComponent, canActivate: [unauthGuard] }, + { path: 'register', component: RegisterComponent, canActivate: [unauthGuard] }, + { path: 'verify-email', component: VerifyEmailComponent, canActivate: [unauthGuard] }, { path: 'auth', children: [{ path: 'verify-email', component: VerifyEmailComponent }], diff --git a/listify-client/src/app/auth/login/login.component.ts b/listify-client/src/app/auth/login/login.component.ts index fa9ad6a..bd5a479 100644 --- a/listify-client/src/app/auth/login/login.component.ts +++ b/listify-client/src/app/auth/login/login.component.ts @@ -1,4 +1,4 @@ -import { Component, inject } from '@angular/core'; +import { Component, inject, OnInit } from '@angular/core'; import { NonNullableFormBuilder, ReactiveFormsModule, Validators } from '@angular/forms'; import { Router, RouterLink } from '@angular/router'; import { finalize } from 'rxjs'; @@ -29,7 +29,7 @@ import { OnboardingService } from '../../onboarding/onboarding.service'; templateUrl: './login.component.html', styleUrl: '../auth-page.scss', }) -export class LoginComponent { +export class LoginComponent implements OnInit { private readonly auth = inject(AuthService); private readonly formBuilder = inject(NonNullableFormBuilder); private readonly router = inject(Router); @@ -41,6 +41,12 @@ export class LoginComponent { password: ['', [Validators.required, Validators.minLength(8)]], }); protected loading = false; + + ngOnInit(): void { + if (this.auth.isAuthenticated()) { + void this.router.navigateByUrl('/lists'); + } + } protected resendingVerification = false; protected hidePassword = true; diff --git a/listify-client/src/app/auth/register/register.component.ts b/listify-client/src/app/auth/register/register.component.ts index d618bf6..db353d2 100644 --- a/listify-client/src/app/auth/register/register.component.ts +++ b/listify-client/src/app/auth/register/register.component.ts @@ -1,4 +1,4 @@ -import { Component, inject } from '@angular/core'; +import { Component, inject, OnInit } from '@angular/core'; import { NonNullableFormBuilder, ReactiveFormsModule, Validators } from '@angular/forms'; import { Router, RouterLink } from '@angular/router'; import { finalize } from 'rxjs'; @@ -28,7 +28,7 @@ import { getAuthErrorMessage } from '../error-message'; templateUrl: './register.component.html', styleUrl: '../auth-page.scss', }) -export class RegisterComponent { +export class RegisterComponent implements OnInit { private readonly auth = inject(AuthService); private readonly formBuilder = inject(NonNullableFormBuilder); private readonly router = inject(Router); @@ -42,6 +42,12 @@ export class RegisterComponent { protected loading = false; protected hidePassword = true; + ngOnInit(): void { + if (this.auth.isAuthenticated()) { + void this.router.navigateByUrl('/lists'); + } + } + submit(): void { if (this.form.invalid) { this.form.markAllAsTouched(); diff --git a/listify-client/src/app/auth/unauth.guard.ts b/listify-client/src/app/auth/unauth.guard.ts new file mode 100644 index 0000000..894aa05 --- /dev/null +++ b/listify-client/src/app/auth/unauth.guard.ts @@ -0,0 +1,10 @@ +import { inject } from '@angular/core'; +import { CanActivateFn, Router } from '@angular/router'; +import { AuthService } from './auth.service'; + +export const unauthGuard: CanActivateFn = () => { + const auth = inject(AuthService); + const router = inject(Router); + + return !auth.isAuthenticated() ? true : router.parseUrl('/lists'); +}; diff --git a/listify-client/src/app/auth/verify-email/verify-email.component.ts b/listify-client/src/app/auth/verify-email/verify-email.component.ts index dc18870..7607bb1 100644 --- a/listify-client/src/app/auth/verify-email/verify-email.component.ts +++ b/listify-client/src/app/auth/verify-email/verify-email.component.ts @@ -1,5 +1,5 @@ import { Component, OnInit, inject, signal } from '@angular/core'; -import { ActivatedRoute, RouterLink } from '@angular/router'; +import { ActivatedRoute, Router, RouterLink } from '@angular/router'; import { MatButtonModule } from '@angular/material/button'; import { MatCardModule } from '@angular/material/card'; import { MatIconModule } from '@angular/material/icon'; @@ -24,12 +24,18 @@ type VerificationState = 'loading' | 'success' | 'error' | 'missing-token'; export class VerifyEmailComponent implements OnInit { private readonly auth = inject(AuthService); private readonly route = inject(ActivatedRoute); + private readonly router = inject(Router); protected readonly state = signal('loading'); protected readonly message = signal('E-Mail wird bestÃĪtigt.'); protected readonly email = signal(null); ngOnInit(): void { + if (this.auth.isAuthenticated()) { + void this.router.navigateByUrl('/lists'); + return; + } + const token = this.route.snapshot.queryParamMap.get('token'); if (!token) {