Client: check user is logged in for some pages

pull/97/head
Chocobozzz 2017-10-10 10:18:25 +02:00
parent 35bf0c83c8
commit f47bf2e142
No known key found for this signature in database
GPG Key ID: 583A612D890159BE
10 changed files with 54 additions and 12 deletions

View File

@ -3,7 +3,8 @@ import {
ActivatedRouteSnapshot, ActivatedRouteSnapshot,
CanActivateChild, CanActivateChild,
RouterStateSnapshot, RouterStateSnapshot,
CanActivate CanActivate,
Router
} from '@angular/router' } from '@angular/router'
import { AuthService } from '../core' import { AuthService } from '../core'
@ -11,10 +12,16 @@ import { AuthService } from '../core'
@Injectable() @Injectable()
export class AdminGuard implements CanActivate, CanActivateChild { export class AdminGuard implements CanActivate, CanActivateChild {
constructor (private auth: AuthService) {} constructor (
private router: Router,
private auth: AuthService
) {}
canActivate (route: ActivatedRouteSnapshot, state: RouterStateSnapshot) { 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) { canActivateChild (route: ActivatedRouteSnapshot, state: RouterStateSnapshot) {

View File

@ -3,13 +3,14 @@ import { RouterModule, Routes } from '@angular/router'
import { MetaGuard } from '@ngx-meta/core' import { MetaGuard } from '@ngx-meta/core'
import { LoginGuard } from '../core'
import { AccountComponent } from './account.component' import { AccountComponent } from './account.component'
const accountRoutes: Routes = [ const accountRoutes: Routes = [
{ {
path: 'account', path: 'account',
component: AccountComponent, component: AccountComponent,
canActivate: [ MetaGuard ], canActivate: [ MetaGuard, LoginGuard ],
data: { data: {
meta: { meta: {
title: 'My account' title: 'My account'

View File

@ -1,3 +1,4 @@
export * from './auth-status.model' export * from './auth-status.model'
export * from './auth-user.model' export * from './auth-user.model'
export * from './auth.service' export * from './auth.service'
export * from './login-guard.service'

View File

@ -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)
}
}

View File

@ -7,7 +7,7 @@ import { BrowserAnimationsModule } from '@angular/platform-browser/animations'
import { SimpleNotificationsModule } from 'angular2-notifications' import { SimpleNotificationsModule } from 'angular2-notifications'
import { ModalModule } from 'ngx-bootstrap/modal' import { ModalModule } from 'ngx-bootstrap/modal'
import { AuthService } from './auth' import { AuthService, LoginGuard } from './auth'
import { ServerService } from './server' import { ServerService } from './server'
import { ConfirmComponent, ConfirmService } from './confirm' import { ConfirmComponent, ConfirmService } from './confirm'
import { MenuComponent, MenuAdminComponent } from './menu' import { MenuComponent, MenuAdminComponent } from './menu'
@ -41,7 +41,8 @@ import { throwIfAlreadyLoaded } from './module-import-guard'
providers: [ providers: [
AuthService, AuthService,
ConfirmService, ConfirmService,
ServerService ServerService,
LoginGuard
] ]
}) })
export class CoreModule { export class CoreModule {

View File

@ -33,7 +33,7 @@
See videos See videos
</a> </a>
<a *ngIf="isLoggedIn" routerLink="/videos/add" routerLinkActive="active"> <a *ngIf="isLoggedIn" routerLink="/videos/upload" routerLinkActive="active">
<span class="hidden-xs glyphicon glyphicon-cloud-upload"></span> <span class="hidden-xs glyphicon glyphicon-cloud-upload"></span>
Upload a video Upload a video
</a> </a>

View File

@ -3,13 +3,14 @@ import { RouterModule, Routes } from '@angular/router'
import { MetaGuard } from '@ngx-meta/core' import { MetaGuard } from '@ngx-meta/core'
import { LoginGuard } from '../../core'
import { VideoAddComponent } from './video-add.component' import { VideoAddComponent } from './video-add.component'
const videoAddRoutes: Routes = [ const videoAddRoutes: Routes = [
{ {
path: '', path: '',
component: VideoAddComponent, component: VideoAddComponent,
canActivateChild: [ MetaGuard ] canActivate: [ MetaGuard, LoginGuard ]
} }
] ]

View File

@ -3,13 +3,14 @@ import { RouterModule, Routes } from '@angular/router'
import { MetaGuard } from '@ngx-meta/core' import { MetaGuard } from '@ngx-meta/core'
import { LoginGuard } from '../../core'
import { VideoUpdateComponent } from './video-update.component' import { VideoUpdateComponent } from './video-update.component'
const videoUpdateRoutes: Routes = [ const videoUpdateRoutes: Routes = [
{ {
path: '', path: '',
component: VideoUpdateComponent, component: VideoUpdateComponent,
canActivateChild: [ MetaGuard ] canActivate: [ MetaGuard, LoginGuard ]
} }
] ]

View File

@ -9,7 +9,7 @@ const videoWatchRoutes: Routes = [
{ {
path: '', path: '',
component: VideoWatchComponent, component: VideoWatchComponent,
canActivateChild: [ MetaGuard ] canActivate: [ MetaGuard ]
} }
] ]

View File

@ -22,11 +22,11 @@ const videosRoutes: Routes = [
} }
}, },
{ {
path: 'add', path: 'upload',
loadChildren: 'app/videos/+video-edit#VideoAddModule', loadChildren: 'app/videos/+video-edit#VideoAddModule',
data: { data: {
meta: { meta: {
title: 'Add a video' title: 'Upload a video'
} }
} }
}, },