Client: check user is logged in for some pages
This commit is contained in:
parent
35bf0c83c8
commit
f47bf2e142
|
@ -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) {
|
||||
|
|
|
@ -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'
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
export * from './auth-status.model'
|
||||
export * from './auth-user.model'
|
||||
export * from './auth.service'
|
||||
export * from './login-guard.service'
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
}
|
|
@ -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 {
|
||||
|
|
|
@ -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>
|
||||
|
|
|
@ -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 ]
|
||||
}
|
||||
]
|
||||
|
||||
|
|
|
@ -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 ]
|
||||
}
|
||||
]
|
||||
|
||||
|
|
|
@ -9,7 +9,7 @@ const videoWatchRoutes: Routes = [
|
|||
{
|
||||
path: '',
|
||||
component: VideoWatchComponent,
|
||||
canActivateChild: [ MetaGuard ]
|
||||
canActivate: [ MetaGuard ]
|
||||
}
|
||||
]
|
||||
|
||||
|
|
|
@ -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'
|
||||
}
|
||||
}
|
||||
},
|
||||
|
|
Loading…
Reference in New Issue