-import { Component } from '@angular/core';
+import { Component, inject } from '@angular/core';
import { RouterOutlet } from '@angular/router';
+import { AuthService } from './services/auth.service';
@Component({
selector: 'app-root',
styleUrl: './app.component.less'
})
export class AppComponent {
+ private authService = inject(AuthService);
+
title = 'stored-login';
+
+ ngOnInit(): void
+ {
+ this.authService.assertUserIsKnown();
+ }
}
-import { Injectable } from '@angular/core';
+import { inject, Injectable } from '@angular/core';
+import { Router } from "@angular/router";
import { Observable, of } from 'rxjs';
@Injectable({
providedIn: 'root'
})
export class AuthService {
+ private router = inject(Router);
+
private username : string|undefined;
login(username : string, password : string) : Observable<string> {
return of(username);
}
+
+ assertUserIsKnown(): void {
+ if(!this.username) {
+ this.router.navigate(['login']);
+ }
+ }
}