Вы можете перенаправить, используя guard
и local Storage
import { Injectable } from '@angular/core';
import { CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot, Router } from '@angular/router';
import { Observable } from 'rxjs';
@Injectable()
export class AuthGuard implements CanActivate {
constructor(private router: Router) {
}
canActivate(
next: ActivatedRouteSnapshot,
state: RouterStateSnapshot): Observable<boolean> | Promise<boolean> | boolean {
const token = localStorage.getItem('token');
const role = localStorage.getItem('id');
if (role === 'driver' && !!token) {
this.router.navigate(['/driver']);
return true
} else if (role === 'student' && !!token) {
this.router.navigate(['/student']);
return true
} else if (role === 'student' && !!token) {
this.router.navigate(['/student']);
return true
} else if (role === 'admin' && !!token) {
this.router.navigate(['/admin']);
return true
} else {
this.router.navigate(['/login']);
return false
}
return false
}
}