import { Component } from '@angular/core';
import { RouterOutlet } from '@angular/router';
+import { AuthService } from './services/auth.service';
@Component({
selector: 'app-root',
})
export class AppComponent {
title = 'stored-login';
+
+ constructor(private authService: AuthService) {}
+
+ ngOnInit(): void
+ {
+ this.authService.assertUserIsKnown();
+ }
}
--- /dev/null
+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();
+ });
+});
--- /dev/null
+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']);
+ }
+ }
+}