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/+admin/admin-guard.service.ts | 13 +++++++--- client/src/app/account/account-routing.module.ts | 3 ++- 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 +- .../videos/+video-edit/video-add-routing.module.ts | 3 ++- .../+video-edit/video-update-routing.module.ts | 3 ++- .../+video-watch/video-watch-routing.module.ts | 2 +- client/src/app/videos/videos-routing.module.ts | 4 +-- 10 files changed, 54 insertions(+), 12 deletions(-) create mode 100644 client/src/app/core/auth/login-guard.service.ts (limited to 'client') diff --git a/client/src/app/+admin/admin-guard.service.ts b/client/src/app/+admin/admin-guard.service.ts index a0ad48175..429dc032d 100644 --- a/client/src/app/+admin/admin-guard.service.ts +++ b/client/src/app/+admin/admin-guard.service.ts @@ -3,7 +3,8 @@ import { ActivatedRouteSnapshot, CanActivateChild, RouterStateSnapshot, - CanActivate + CanActivate, + Router } from '@angular/router' import { AuthService } from '../core' @@ -11,10 +12,16 @@ import { AuthService } from '../core' @Injectable() export class AdminGuard implements CanActivate, CanActivateChild { - constructor (private auth: AuthService) {} + constructor ( + private router: Router, + private auth: AuthService + ) {} canActivate (route: ActivatedRouteSnapshot, state: RouterStateSnapshot) { - return this.auth.isAdmin() + if (this.auth.isAdmin() === true) return true + + this.router.navigate([ '/login' ]) + return false } canActivateChild (route: ActivatedRouteSnapshot, state: RouterStateSnapshot) { diff --git a/client/src/app/account/account-routing.module.ts b/client/src/app/account/account-routing.module.ts index 029d97788..74d9aa03e 100644 --- a/client/src/app/account/account-routing.module.ts +++ b/client/src/app/account/account-routing.module.ts @@ -3,13 +3,14 @@ import { RouterModule, Routes } from '@angular/router' import { MetaGuard } from '@ngx-meta/core' +import { LoginGuard } from '../core' import { AccountComponent } from './account.component' const accountRoutes: Routes = [ { path: 'account', component: AccountComponent, - canActivate: [ MetaGuard ], + canActivate: [ MetaGuard, LoginGuard ], data: { meta: { title: 'My account' 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 diff --git a/client/src/app/videos/+video-edit/video-add-routing.module.ts b/client/src/app/videos/+video-edit/video-add-routing.module.ts index 9e8fa4acc..e7a32627c 100644 --- a/client/src/app/videos/+video-edit/video-add-routing.module.ts +++ b/client/src/app/videos/+video-edit/video-add-routing.module.ts @@ -3,13 +3,14 @@ import { RouterModule, Routes } from '@angular/router' import { MetaGuard } from '@ngx-meta/core' +import { LoginGuard } from '../../core' import { VideoAddComponent } from './video-add.component' const videoAddRoutes: Routes = [ { path: '', component: VideoAddComponent, - canActivateChild: [ MetaGuard ] + canActivate: [ MetaGuard, LoginGuard ] } ] diff --git a/client/src/app/videos/+video-edit/video-update-routing.module.ts b/client/src/app/videos/+video-edit/video-update-routing.module.ts index 1d06a7ac3..22c27a072 100644 --- a/client/src/app/videos/+video-edit/video-update-routing.module.ts +++ b/client/src/app/videos/+video-edit/video-update-routing.module.ts @@ -3,13 +3,14 @@ import { RouterModule, Routes } from '@angular/router' import { MetaGuard } from '@ngx-meta/core' +import { LoginGuard } from '../../core' import { VideoUpdateComponent } from './video-update.component' const videoUpdateRoutes: Routes = [ { path: '', component: VideoUpdateComponent, - canActivateChild: [ MetaGuard ] + canActivate: [ MetaGuard, LoginGuard ] } ] diff --git a/client/src/app/videos/+video-watch/video-watch-routing.module.ts b/client/src/app/videos/+video-watch/video-watch-routing.module.ts index 97fa5c725..bdd4f945e 100644 --- a/client/src/app/videos/+video-watch/video-watch-routing.module.ts +++ b/client/src/app/videos/+video-watch/video-watch-routing.module.ts @@ -9,7 +9,7 @@ const videoWatchRoutes: Routes = [ { path: '', component: VideoWatchComponent, - canActivateChild: [ MetaGuard ] + canActivate: [ MetaGuard ] } ] diff --git a/client/src/app/videos/videos-routing.module.ts b/client/src/app/videos/videos-routing.module.ts index 225b6b018..d3869748b 100644 --- a/client/src/app/videos/videos-routing.module.ts +++ b/client/src/app/videos/videos-routing.module.ts @@ -22,11 +22,11 @@ const videosRoutes: Routes = [ } }, { - path: 'add', + path: 'upload', loadChildren: 'app/videos/+video-edit#VideoAddModule', data: { meta: { - title: 'Add a video' + title: 'Upload a video' } } }, -- cgit v1.2.3