From 1206141a3e853eb2e60eb856c5e9cf1fd3c619d3 Mon Sep 17 00:00:00 2001
From: David Donges <david.donges@mni.thm.de>
Date: Tue, 6 Mar 2018 14:15:03 +0100
Subject: [PATCH] Implement route guard

Implemented route guard that can be used to protect routes from access by not logged in users.
---
 src/app/app.module.ts                |  4 +++-
 src/app/authentication.guard.spec.ts | 15 +++++++++++++++
 src/app/authentication.guard.ts      | 25 +++++++++++++++++++++++++
 3 files changed, 43 insertions(+), 1 deletion(-)
 create mode 100644 src/app/authentication.guard.spec.ts
 create mode 100644 src/app/authentication.guard.ts

diff --git a/src/app/app.module.ts b/src/app/app.module.ts
index 1a09f92bc..e067baf02 100644
--- a/src/app/app.module.ts
+++ b/src/app/app.module.ts
@@ -47,6 +47,7 @@ import {
 import { LoginScreenComponent } from './login-screen/login-screen.component';
 import { NotificationService } from './notification.service';
 import { AuthenticationService } from './authentication.service';
+import { AuthenticationGuard } from './authentication.guard';
 
 @NgModule({
   declarations: [
@@ -100,7 +101,8 @@ import { AuthenticationService } from './authentication.service';
   ],
   providers: [
     NotificationService,
-    AuthenticationService
+    AuthenticationService,
+    AuthenticationGuard
   ],
   bootstrap: [AppComponent]
 })
diff --git a/src/app/authentication.guard.spec.ts b/src/app/authentication.guard.spec.ts
new file mode 100644
index 000000000..4e97bbe88
--- /dev/null
+++ b/src/app/authentication.guard.spec.ts
@@ -0,0 +1,15 @@
+import { TestBed, async, inject } from '@angular/core/testing';
+
+import { AuthenticationGuard } from './authentication.guard';
+
+describe('AuthenticationGuard', () => {
+  beforeEach(() => {
+    TestBed.configureTestingModule({
+      providers: [AuthenticationGuard]
+    });
+  });
+
+  it('should ...', inject([AuthenticationGuard], (guard: AuthenticationGuard) => {
+    expect(guard).toBeTruthy();
+  }));
+});
diff --git a/src/app/authentication.guard.ts b/src/app/authentication.guard.ts
new file mode 100644
index 000000000..c4cf7fe48
--- /dev/null
+++ b/src/app/authentication.guard.ts
@@ -0,0 +1,25 @@
+import { Injectable } from '@angular/core';
+import { CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot, Router } from '@angular/router';
+import { Observable } from 'rxjs/Observable';
+import { AuthenticationService } from './authentication.service';
+import 'rxjs/add/operator/map';
+import 'rxjs/add/observable/of';
+import 'rxjs/add/operator/catch';
+
+@Injectable()
+export class AuthenticationGuard implements CanActivate {
+  constructor(private authenticationService: AuthenticationService,
+              private router: Router) {
+  }
+
+  canActivate(next: ActivatedRouteSnapshot,
+              state: RouterStateSnapshot): Observable<boolean> {
+    return this.authenticationService.isLoggedIn().map(isLoggedIn => {
+      if (!isLoggedIn) {
+        // TODO: redirect to error page
+        this.router.navigate(['/']);
+      }
+      return isLoggedIn;
+    });
+  }
+}
-- 
GitLab