ActivatedRouteSnapshot,
CanActivateChild,
RouterStateSnapshot,
- CanActivate
+ CanActivate,
+ Router
} from '@angular/router'
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) {
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'
export * from './auth-status.model'
export * from './auth-user.model'
export * from './auth.service'
+export * from './login-guard.service'
--- /dev/null
+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)
+ }
+}
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'
providers: [
AuthService,
ConfirmService,
- ServerService
+ ServerService,
+ LoginGuard
]
})
export class CoreModule {
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>
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 ]
}
]
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 ]
}
]
{
path: '',
component: VideoWatchComponent,
- canActivateChild: [ MetaGuard ]
+ canActivate: [ MetaGuard ]
}
]
}
},
{
- path: 'add',
+ path: 'upload',
loadChildren: 'app/videos/+video-edit#VideoAddModule',
data: {
meta: {
- title: 'Add a video'
+ title: 'Upload a video'
}
}
},