From: Kai Moritz Date: Sun, 7 Sep 2025 00:26:33 +0000 (+0200) Subject: WIP:Login-Umleitung X-Git-Url: http://juplo.de/gitweb/?a=commitdiff_plain;h=07b575d3ecda04b1ac4ba9e1c7f32d09efa2e3f9;p=demos%2Fangular%2Fstored-login WIP:Login-Umleitung --- diff --git a/src/app/app.component.ts b/src/app/app.component.ts index 61a67e8..1ab92ed 100644 --- a/src/app/app.component.ts +++ b/src/app/app.component.ts @@ -1,5 +1,6 @@ import { Component } from '@angular/core'; import { RouterOutlet } from '@angular/router'; +import { AuthService } from './services/auth.service'; @Component({ selector: 'app-root', @@ -9,4 +10,11 @@ import { RouterOutlet } from '@angular/router'; }) export class AppComponent { title = 'stored-login'; + + constructor(private authService: AuthService) {} + + ngOnInit(): void + { + this.authService.assertUserIsKnown(); + } } diff --git a/src/app/services/auth.service.spec.ts b/src/app/services/auth.service.spec.ts new file mode 100644 index 0000000..f1251ca --- /dev/null +++ b/src/app/services/auth.service.spec.ts @@ -0,0 +1,16 @@ +import { TestBed } from '@angular/core/testing'; + +import { AuthService } from './auth.service'; + +describe('AuthService', () => { + let service: AuthService; + + beforeEach(() => { + TestBed.configureTestingModule({}); + service = TestBed.inject(AuthService); + }); + + it('should be created', () => { + expect(service).toBeTruthy(); + }); +}); diff --git a/src/app/services/auth.service.ts b/src/app/services/auth.service.ts new file mode 100644 index 0000000..b0c594a --- /dev/null +++ b/src/app/services/auth.service.ts @@ -0,0 +1,21 @@ +import { Injectable } from '@angular/core'; +import { Router } from "@angular/router"; + +@Injectable({ + providedIn: 'root' +}) +export class AuthService { + private username : string|undefined; + + constructor(private router : Router) { } + + login(username : string, password : string) { + this.username = username; + } + + assertUserIsKnown(): void { + if(!this.username) { + this.router.navigate(['login']); + } + } +}