Client: check user is logged in for some pages

This commit is contained in:
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,
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) {

View File

@ -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'

View File

@ -1,3 +1,4 @@
export * from './auth-status.model'
export * from './auth-user.model'
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 { 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 {

View File

@ -33,7 +33,7 @@
See videos
</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>
Upload a video
</a>

View File

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

View File

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

View File

@ -9,7 +9,7 @@ const videoWatchRoutes: Routes = [
{
path: '',
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',
data: {
meta: {
title: 'Add a video'
title: 'Upload a video'
}
}
},