join/src/app/auth.guard.ts

29 lines
790 B
TypeScript

import { Injectable } from '@angular/core';
import { Router } from '@angular/router';
import { Observable, of } from 'rxjs';
import { map, catchError } from 'rxjs/operators';
import { LoginService } from './services/auth.service';
@Injectable({
providedIn: 'root',
})
export class AuthGuard {
constructor(private loginService: LoginService, private router: Router) {}
canActivate(): Observable<boolean> | Promise<boolean> | boolean {
return this.loginService.checkAuthUser().pipe(
map((isAuthenticated) => {
if (isAuthenticated) {
return true;
} else {
this.router.navigate(['/']);
return false;
}
}),
catchError(() => {
this.router.navigate(['/']);
return of(false);
})
);
}
}