Skip to content
Permalink
Browse files

fix admin route

  • Loading branch information
oritpersik authored and AvrahamO committed Jul 2, 2018
1 parent aed60da commit 25dc90f8e6b6c2ea3e47755b91f765544cc10470
@@ -27,11 +27,5 @@ const UserSchema = new mongoose.Schema({
versionKey: false
});

UserSchema.pre("save",function(next) {
if (this.roles.length === 0) {
this.roles.push('admin');
}
next();
});

module.exports = mongoose.model('User', UserSchema);
@@ -1,13 +1,16 @@
import { NgModule } from '@angular/core';
import { RouterModule, Routes} from '@angular/router';
import { RouterModule, Routes } from '@angular/router';

import { AdminComponent } from './admin.component';
import { OnlyAdminUsersGuard } from './admin-user-guard';

const routes: Routes = [{
path: '',
path: 'admin',
canActivate: [OnlyAdminUsersGuard],
component: AdminComponent,
children: [{
path: '',
component: AdminComponent,
}]
}];

@NgModule({
@@ -5,17 +5,10 @@ import { AuthService } from '../auth/auth.service';

@Injectable()
export class OnlyAdminUsersGuard implements CanActivate {
constructor(private authService : AuthService) {};
constructor() {}

canActivate() {
return new Observable<boolean> (observer => {
this
.authService
.getUser()
.subscribe(user => {
if (!user) observer.next(false);
observer.next(user.isAdmin)
})
});
const user = (<any>window).user;
return user && user.isAdmin;
}
}
}
@@ -1,7 +1,7 @@
import { Component, OnInit } from '@angular/core';

@Component({
selector: 'admin-module',
selector: 'app-admin',
templateUrl: './admin.component.html',
})
export class AdminComponent implements OnInit {
@@ -1,15 +1,10 @@
import {NgModule} from '@angular/core';
import {CommonModule} from '@angular/common';
import {RouterModule} from '@angular/router';

import { AdminRoutingModule } from './admin-routing.module';
import {AdminComponent} from './admin.component';
import {OnlyAdminUsersGuard} from './admin-user-guard';

/*
Don't leave side-effects outside of classes so this will tree-shake nicely on prod
e.g. `console.log('something')` is a side effect.
*/
@NgModule({
declarations: [
AdminComponent
@@ -21,8 +16,4 @@ import {OnlyAdminUsersGuard} from './admin-user-guard';
providers: [
OnlyAdminUsersGuard
]})
export class AdminModule {
constructor() {
console.log('`AdminModule` module initialized');
}
}
export class AdminModule {}
@@ -29,8 +29,8 @@ import { HomeComponent } from './home/home.component';
RouterModule,
SharedModule,
AuthModule,
AppRoutingModule,
AdminModule,
AppRoutingModule,
],
providers: [{
provide: HTTP_INTERCEPTORS,
@@ -7,7 +7,7 @@ export class AuthGuard implements CanActivate {
constructor(public router: Router) {}

canActivate() {
let user = (<any>window).user;
const user = (<any>window).user;
if (user) return true;

// not logged in so redirect to login page with the return url

0 comments on commit 25dc90f

Please sign in to comment.
You can’t perform that action at this time.