Home page carousel
[eliot.git] / blueprints / common / eliot-ui / frontend-src / src / app / _services / AuthGuard.ts
diff --git a/blueprints/common/eliot-ui/frontend-src/src/app/_services/AuthGuard.ts b/blueprints/common/eliot-ui/frontend-src/src/app/_services/AuthGuard.ts
new file mode 100644 (file)
index 0000000..7177e11
--- /dev/null
@@ -0,0 +1,31 @@
+import { Injectable } from '@angular/core';
+import { Router, CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router';
+
+import { AuthenticationService } from './../_services/authentication.service';
+
+@Injectable({ providedIn: 'root' })
+
+export class AuthGuard implements CanActivate {
+  
+    constructor(
+        private router: Router,
+        private authenticationService: AuthenticationService
+    ) { }
+
+    canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
+        const currentUser = this.authenticationService.currentUserValue;
+        
+        console.log("current user");
+        console.log(currentUser);
+        
+        if (currentUser) {
+            // authorised so return true
+            return true;
+        }
+
+        // not logged in so redirect to login page
+        this.router.navigate(['/login'], { queryParams: { returnUrl: state.url } });
+        return false;
+    }
+
+}
\ No newline at end of file