title: How to implement role redirection in angular 6
body: <p><em>I have created a frontend using Angular 6 and I want implement role based authorisation how can i achieve this am using angular 6 node.js, passport ,mongoose its a mean stack application</em></p>
  
  <p><strong>Here is my auth.guard.ts</strong></p>
  
  <pre><code>import { Injectable } from '@angular/core';
  import { CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot, UrlTree } from '@angular/router';
  import { Observable } from 'rxjs';
  import { UserService } from '../shared/user.service';
  import { Router } from '@angular/router';
  
  @Injectable({
    providedIn: 'root'
  })
  export class AuthGuard implements CanActivate {
  
  
  
  constructor(private userService:UserService,private router: Router) {}
  
    canActivate(
      next: ActivatedRouteSnapshot,
      state: RouterStateSnapshot):  boolean  {
  
  
  if(!this.userService.isloggedIn()){
    this.router.navigateByUrl('/user');
    this.userService.deleteToken();
    return false;
  }
  
      return true;
    }
  
  }
  </code></pre>
  
  <p><strong>Here is my user.model.ts</strong></p>
  
  <pre><code>export class User {
      fullname:string;
      email:string;
      university:string;
      College:string;
      Department:string;
      password:string;
      admintype:string;
  
  }
  </code></pre>
  
  <p><strong>Here is my routes.ts</strong></p>
  
  <pre><code>import { Routes } from '@angular/router';
  import { UserComponent } from './user/user.component';
  import { SignUpComponent } from './user/sign-up/sign-up.component';
  import { SignInComponent } from './user/sign-in/sign-in.component';
  import { AuthGuard } from './auth/auth.guard';
  import { AdminComponent } from './user/admin/admin.component';
  import { Role } from './shared/role';
  
  export const appRoutes: Routes = [
      {
          path: 'user',component:SignInComponent
      },
      {
          path:'signup', component:SignUpComponent, canActivate:[AuthGuard]
      },
      {
          path:'admin', component:AdminComponent,
           canActivate:[AuthGuard]
      },
      {
          path: '',component:SignInComponent
      },
      {
          path:'',redirectTo:'/user', pathMatch:'full'
      }
  ];
  </code></pre>
  
  <p><strong>Here is my Mongoose model</strong></p>
  
  <pre><code> const mongoose = require('mongoose');
   const bcrypt = require('bcryptjs');
   const jwt = require('jsonwebtoken');
  
   const userSchema = new mongoose.Schema({
       fullname:{
           type:String,
           required:'Full name cant be empty',
           min:6,
           max:255
       },
       email:{
           type:String,
           required:'Email cant be Empty',
           max:255,
           unique:true
       },
       University:{
           type:String,
           default:"Sokoine University of Agriculture"
       },
       College:{
           type:String,
           required:'College cant be Empty'
       },
  
       Department:{
           type:String,
           required:'department cant be empty'
       },
       password:{
           type:String,
           required:'pasword cant be empty',
           max :1024,
           minlength: [6,'password must be atlest 6 character long']
       },
       admintype:{
          type:String,
          enum :['HOD','CICT','Sports','SUASAB','Admin']
      },
       date:{
  type:Date,
  default:Date.now
       },
       saltSecret:String
   });
  
  
   //custom validation
  userSchema.path('email').validate((val) =&gt; {
      emailRegex = /^(([^&lt;&gt;()\[\]\\.,;:\s@"]+(\.[^&lt;&gt;()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
      return emailRegex.test(val);
  },'Invalid E-mail. ');
  
  
  
  
   //events
   userSchema.pre('save', function(next) {
       bcrypt.genSalt(10, (err, salt)=&gt; {
           bcrypt.hash(this.password, salt, (err, hash) =&gt; {
               this.password = hash;
               this.saltSecret = salt;
               next();
           });
       });
   });
  
   //methods
   userSchema.methods.verifyPassword = function(password){
       return bcrypt.compareSync(password, this.password);
   };
  
   userSchema.methods.generateJwt = function() {
       return jwt.sign({ _id:this._id},
          process.env.JWT_SECRET,
          {expiresIn:process.env.JWT_EXP});
   }
  
   module.exports = mongoose.model('User',userSchema)
  </code></pre>
  
  <p><strong>Here is my signin.component.ts</strong></p>
  
  <pre><code>```import { Component, OnInit } from '@angular/core';
  import { NgForm } from '@angular/forms';
  import { UserService } from 'src/app/shared/user.service';
  import { Router } from '@angular/router';
  
  @Component({
    selector: 'app-sign-in',
    templateUrl: './sign-in.component.html',
    styleUrls: ['./sign-in.component.css']
  })
  export class SignInComponent implements OnInit {
  
    constructor( private userService:UserService, private router:Router) { }
  
    model = {
     email:'',
     password:''
    };
  
    emailRegex = /^(([^&lt;&gt;()\[\]\\.,;:\s@"]+(\.[^&lt;&gt;()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
  
  
    serverErrorMessages : string;
    ngOnInit() {
      if(this.userService.isloggedIn())
      {
        this.router.navigateByUrl('/admin');
      }
    }
  
  
    onSubmit(form :NgForm)
    {
     this.userService.login(form.value).subscribe( 
       res =&gt;{
        this.userService.setToken(res['token']);
        this.router.navigateByUrl('/admin');
     },
     err =&gt;{
       this.serverErrorMessages = err.message;
  
     });
    }
  }```
  </code></pre>
  
  <p><strong>Here is my role.ts</strong></p>
  
  <pre><code>```export enum Role {
      Admin = 'Admin',
      Cict = 'admincict',
      Sport = 'adminsport',
      SuaHub = 'adminsuahub'
  }```
  </code></pre>
  
  <p><strong>Have tried several tutrials but non helps me at all</strong></p>