Differenzansicht 16-guards
im Vergleich zu 15-lazyloading

← ZurĂĽck zur Ăśbersicht | Demo | Quelltext auf GitHub
src/app/admin/book-create/book-create.component.spec.ts DELETED
@@ -1,23 +0,0 @@
1
- import { ComponentFixture, TestBed } from '@angular/core/testing';
2
-
3
- import { BookCreateComponent } from './book-create.component';
4
-
5
- describe('BookCreateComponent', () => {
6
- let component: BookCreateComponent;
7
- let fixture: ComponentFixture<BookCreateComponent>;
8
-
9
- beforeEach(async () => {
10
- await TestBed.configureTestingModule({
11
- declarations: [ BookCreateComponent ]
12
- })
13
- .compileComponents();
14
-
15
- fixture = TestBed.createComponent(BookCreateComponent);
16
- component = fixture.componentInstance;
17
- fixture.detectChanges();
18
- });
19
-
20
- it('should create', () => {
21
- expect(component).toBeTruthy();
22
- });
23
- });
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
src/app/admin/book-edit/book-edit.component.spec.ts DELETED
@@ -1,23 +0,0 @@
1
- import { ComponentFixture, TestBed } from '@angular/core/testing';
2
-
3
- import { BookEditComponent } from './book-edit.component';
4
-
5
- describe('BookEditComponent', () => {
6
- let component: BookEditComponent;
7
- let fixture: ComponentFixture<BookEditComponent>;
8
-
9
- beforeEach(async () => {
10
- await TestBed.configureTestingModule({
11
- declarations: [ BookEditComponent ]
12
- })
13
- .compileComponents();
14
-
15
- fixture = TestBed.createComponent(BookEditComponent);
16
- component = fixture.componentInstance;
17
- fixture.detectChanges();
18
- });
19
-
20
- it('should create', () => {
21
- expect(component).toBeTruthy();
22
- });
23
- });
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
src/app/admin/book-form/book-form.component.spec.ts DELETED
@@ -1,23 +0,0 @@
1
- import { ComponentFixture, TestBed } from '@angular/core/testing';
2
-
3
- import { BookFormComponent } from './book-form.component';
4
-
5
- describe('BookFormComponent', () => {
6
- let component: BookFormComponent;
7
- let fixture: ComponentFixture<BookFormComponent>;
8
-
9
- beforeEach(async () => {
10
- await TestBed.configureTestingModule({
11
- declarations: [ BookFormComponent ]
12
- })
13
- .compileComponents();
14
-
15
- fixture = TestBed.createComponent(BookFormComponent);
16
- component = fixture.componentInstance;
17
- fixture.detectChanges();
18
- });
19
-
20
- it('should create', () => {
21
- expect(component).toBeTruthy();
22
- });
23
- });
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
src/app/admin/form-errors/form-errors.component.spec.ts DELETED
@@ -1,23 +0,0 @@
1
- import { ComponentFixture, TestBed } from '@angular/core/testing';
2
-
3
- import { FormErrorsComponent } from './form-errors.component';
4
-
5
- describe('FormErrorsComponent', () => {
6
- let component: FormErrorsComponent;
7
- let fixture: ComponentFixture<FormErrorsComponent>;
8
-
9
- beforeEach(async () => {
10
- await TestBed.configureTestingModule({
11
- declarations: [ FormErrorsComponent ]
12
- })
13
- .compileComponents();
14
-
15
- fixture = TestBed.createComponent(FormErrorsComponent);
16
- component = fixture.componentInstance;
17
- fixture.detectChanges();
18
- });
19
-
20
- it('should create', () => {
21
- expect(component).toBeTruthy();
22
- });
23
- });
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
src/app/admin/shared/async-validators.service.spec.ts DELETED
@@ -1,16 +0,0 @@
1
- import { TestBed } from '@angular/core/testing';
2
-
3
- import { AsyncValidatorsService } from './async-validators.service';
4
-
5
- describe('AsyncValidatorsService', () => {
6
- let service: AsyncValidatorsService;
7
-
8
- beforeEach(() => {
9
- TestBed.configureTestingModule({});
10
- service = TestBed.inject(AsyncValidatorsService);
11
- });
12
-
13
- it('should be created', () => {
14
- expect(service).toBeTruthy();
15
- });
16
- });
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
src/app/app-routing.module.ts CHANGED
@@ -2,6 +2,7 @@ import { NgModule } from '@angular/core';
2
  import { RouterModule, Routes } from '@angular/router';
3
 
4
  import { HomeComponent } from './home/home.component';
 
5
 
6
  const routes: Routes = [
7
  {
@@ -19,7 +20,8 @@ const routes: Routes = [
19
  },
20
  {
21
  path: 'admin',
22
- loadChildren: () => import('./admin/admin.module').then(m => m.AdminModule)
 
23
  }
24
  ];
25
 
2
  import { RouterModule, Routes } from '@angular/router';
3
 
4
  import { HomeComponent } from './home/home.component';
5
+ import { authGuard } from './shared/auth.guard';
6
 
7
  const routes: Routes = [
8
  {
20
  },
21
  {
22
  path: 'admin',
23
+ loadChildren: () => import('./admin/admin.module').then(m => m.AdminModule),
24
+ canActivate: [authGuard]
25
  }
26
  ];
27
 
src/app/books/book-details/book-details.component.spec.ts DELETED
@@ -1,23 +0,0 @@
1
- import { ComponentFixture, TestBed } from '@angular/core/testing';
2
-
3
- import { BookDetailsComponent } from './book-details.component';
4
-
5
- describe('BookDetailsComponent', () => {
6
- let component: BookDetailsComponent;
7
- let fixture: ComponentFixture<BookDetailsComponent>;
8
-
9
- beforeEach(async () => {
10
- await TestBed.configureTestingModule({
11
- declarations: [ BookDetailsComponent ]
12
- })
13
- .compileComponents();
14
-
15
- fixture = TestBed.createComponent(BookDetailsComponent);
16
- component = fixture.componentInstance;
17
- fixture.detectChanges();
18
- });
19
-
20
- it('should create', () => {
21
- expect(component).toBeTruthy();
22
- });
23
- });
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
src/app/books/book-list/book-list.component.spec.ts DELETED
@@ -1,23 +0,0 @@
1
- import { ComponentFixture, TestBed } from '@angular/core/testing';
2
-
3
- import { BookListComponent } from './book-list.component';
4
-
5
- describe('BookListComponent', () => {
6
- let component: BookListComponent;
7
- let fixture: ComponentFixture<BookListComponent>;
8
-
9
- beforeEach(async () => {
10
- await TestBed.configureTestingModule({
11
- declarations: [ BookListComponent ]
12
- })
13
- .compileComponents();
14
-
15
- fixture = TestBed.createComponent(BookListComponent);
16
- component = fixture.componentInstance;
17
- fixture.detectChanges();
18
- });
19
-
20
- it('should create', () => {
21
- expect(component).toBeTruthy();
22
- });
23
- });
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
src/app/books/shared/loggedin-only.directive.spec.ts DELETED
@@ -1,8 +0,0 @@
1
- import { LoggedinOnlyDirective } from './loggedin-only.directive';
2
-
3
- describe('LoggedinOnlyDirective', () => {
4
- it('should create an instance', () => {
5
- const directive = new LoggedinOnlyDirective();
6
- expect(directive).toBeTruthy();
7
- });
8
- });
 
 
 
 
 
 
 
 
src/app/home/home.component.spec.ts CHANGED
@@ -1,3 +1,4 @@
 
1
  import { ComponentFixture, TestBed } from '@angular/core/testing';
2
 
3
  import { HomeComponent } from './home.component';
@@ -8,7 +9,8 @@ describe('HomeComponent', () => {
8
 
9
  beforeEach(async () => {
10
  await TestBed.configureTestingModule({
11
- declarations: [ HomeComponent ]
 
12
  })
13
  .compileComponents();
14
 
1
+ import { NO_ERRORS_SCHEMA } from '@angular/core';
2
  import { ComponentFixture, TestBed } from '@angular/core/testing';
3
 
4
  import { HomeComponent } from './home.component';
9
 
10
  beforeEach(async () => {
11
  await TestBed.configureTestingModule({
12
+ declarations: [ HomeComponent ],
13
+ schemas: [NO_ERRORS_SCHEMA]
14
  })
15
  .compileComponents();
16
 
src/app/search/search.component.spec.ts DELETED
@@ -1,23 +0,0 @@
1
- import { ComponentFixture, TestBed } from '@angular/core/testing';
2
-
3
- import { SearchComponent } from './search.component';
4
-
5
- describe('SearchComponent', () => {
6
- let component: SearchComponent;
7
- let fixture: ComponentFixture<SearchComponent>;
8
-
9
- beforeEach(async () => {
10
- await TestBed.configureTestingModule({
11
- declarations: [ SearchComponent ]
12
- })
13
- .compileComponents();
14
-
15
- fixture = TestBed.createComponent(SearchComponent);
16
- component = fixture.componentInstance;
17
- fixture.detectChanges();
18
- });
19
-
20
- it('should create', () => {
21
- expect(component).toBeTruthy();
22
- });
23
- });
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
src/app/shared/auth.guard.spec.ts ADDED
@@ -0,0 +1,17 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import { TestBed } from '@angular/core/testing';
2
+ import { CanActivateFn } from '@angular/router';
3
+
4
+ import { authGuard } from './auth.guard';
5
+
6
+ describe('authGuard', () => {
7
+ const executeGuard: CanActivateFn = (...guardParameters) =>
8
+ TestBed.runInInjectionContext(() => authGuard(...guardParameters));
9
+
10
+ beforeEach(() => {
11
+ TestBed.configureTestingModule({});
12
+ });
13
+
14
+ it('should be created', () => {
15
+ expect(executeGuard).toBeTruthy();
16
+ });
17
+ });
src/app/shared/auth.guard.ts ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import { inject } from '@angular/core';
2
+ import { CanActivateFn, Router } from '@angular/router';
3
+ import { map, take } from 'rxjs';
4
+
5
+ import { AuthService } from './auth.service';
6
+
7
+ export const authGuard: CanActivateFn = () => {
8
+ const authService = inject(AuthService);
9
+ const router = inject(Router);
10
+
11
+ if (authService.isAuthenticated) {
12
+ return true;
13
+ } else {
14
+ window.alert('Not logged in!');
15
+ return router.parseUrl('/home');
16
+ }
17
+ };
18
+
19
+ // Alternative Lösung mit Observable
20
+ export const authGuardAlternative: CanActivateFn = () => {
21
+ const authService = inject(AuthService);
22
+ const router = inject(Router);
23
+
24
+ return authService.isAuthenticated$.pipe(
25
+ take(1),
26
+ map(isAuthenticated => {
27
+ if (isAuthenticated) {
28
+ return true;
29
+ } else {
30
+ window.alert('Not logged in!');
31
+ return router.parseUrl('/home');
32
+ }
33
+ })
34
+ );
35
+ };
src/app/shared/book-store.service.spec.ts DELETED
@@ -1,16 +0,0 @@
1
- import { TestBed } from '@angular/core/testing';
2
-
3
- import { BookStoreService } from './book-store.service';
4
-
5
- describe('BookStoreService', () => {
6
- let service: BookStoreService;
7
-
8
- beforeEach(() => {
9
- TestBed.configureTestingModule({});
10
- service = TestBed.inject(BookStoreService);
11
- });
12
-
13
- it('should be created', () => {
14
- expect(service).toBeTruthy();
15
- });
16
- });