Вы должны добавить файл защиты с функцией CanActivate
guard.ts
import { Injectable } from '@angular/core';
import { CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot, UrlTree, Router } from '@angular/router';
import { Observable } from 'rxjs';
import { AuthService } from 'src/app/services/auth.service';
@Injectable({ providedIn: 'root' }) export class AdminGuard implements CanActivate { constructor(private authService: AuthService, private router: Router) {}
canActivate(
next: ActivatedRouteSnapshot,
state: RouterStateSnapshot): Observable<boolean> | Promise<boolean> | boolean | UrlTree {
const currentUser = this.authService.isAuthenticated();
if (currentUser) {
return true;
} else {
return this.router.parseUrl('/sign-in');
}
} }
auth.service.ts
getCurrentUserToken() {
firebase.auth().currentUser.getIdToken()/*.getToken()*/
.then(
(token: string) => {
localStorage.setItem('isLoggedIn', JSON.stringify(token));
}
);
localStorage.getItem('UserisLoggedNotIn');
}
isAuthenticated(): boolean {
return (localStorage.getItem('isLoggedIn')) ? true : false;
// return (this.userId) ? true : false;
}
isAuthenticated(): boolean {
return (localStorage.getItem('isLoggedIn')) ? true : false;
// return (this.userId) ? true : false;
}