From f47bf2e142b0b22460558e23720f5aed8deb982c Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Tue, 10 Oct 2017 10:18:25 +0200 Subject: Client: check user is logged in for some pages --- client/src/app/core/auth/index.ts | 1 + client/src/app/core/auth/login-guard.service.ts | 30 +++++++++++++++++++++++++ client/src/app/core/core.module.ts | 5 +++-- client/src/app/core/menu/menu.component.html | 2 +- 4 files changed, 35 insertions(+), 3 deletions(-) create mode 100644 client/src/app/core/auth/login-guard.service.ts (limited to 'client/src/app/core') diff --git a/client/src/app/core/auth/index.ts b/client/src/app/core/auth/index.ts index 8e5caa7ed..a81f2c002 100644 --- a/client/src/app/core/auth/index.ts +++ b/client/src/app/core/auth/index.ts @@ -1,3 +1,4 @@ export * from './auth-status.model' export * from './auth-user.model' export * from './auth.service' +export * from './login-guard.service' diff --git a/client/src/app/core/auth/login-guard.service.ts b/client/src/app/core/auth/login-guard.service.ts new file mode 100644 index 000000000..c09e8fe97 --- /dev/null +++ b/client/src/app/core/auth/login-guard.service.ts @@ -0,0 +1,30 @@ +import { Injectable } from '@angular/core' +import { + ActivatedRouteSnapshot, + CanActivateChild, + RouterStateSnapshot, + CanActivate, + Router +} from '@angular/router' + +import { AuthService } from './auth.service' + +@Injectable() +export class LoginGuard implements CanActivate, CanActivateChild { + + constructor ( + private router: Router, + private auth: AuthService + ) {} + + canActivate (route: ActivatedRouteSnapshot, state: RouterStateSnapshot) { + if (this.auth.isLoggedIn() === true) return true + + this.router.navigate([ '/login' ]) + return false + } + + canActivateChild (route: ActivatedRouteSnapshot, state: RouterStateSnapshot) { + return this.canActivate(route, state) + } +} diff --git a/client/src/app/core/core.module.ts b/client/src/app/core/core.module.ts index fd1586f8e..163a6bbde 100644 --- a/client/src/app/core/core.module.ts +++ b/client/src/app/core/core.module.ts @@ -7,7 +7,7 @@ import { BrowserAnimationsModule } from '@angular/platform-browser/animations' import { SimpleNotificationsModule } from 'angular2-notifications' import { ModalModule } from 'ngx-bootstrap/modal' -import { AuthService } from './auth' +import { AuthService, LoginGuard } from './auth' import { ServerService } from './server' import { ConfirmComponent, ConfirmService } from './confirm' import { MenuComponent, MenuAdminComponent } from './menu' @@ -41,7 +41,8 @@ import { throwIfAlreadyLoaded } from './module-import-guard' providers: [ AuthService, ConfirmService, - ServerService + ServerService, + LoginGuard ] }) export class CoreModule { diff --git a/client/src/app/core/menu/menu.component.html b/client/src/app/core/menu/menu.component.html index fb4c4a6a9..ca341a0fd 100644 --- a/client/src/app/core/menu/menu.component.html +++ b/client/src/app/core/menu/menu.component.html @@ -33,7 +33,7 @@ See videos - + Upload a video -- cgit v1.2.3