26 lines
847 B
TypeScript
26 lines
847 B
TypeScript
|
import { Injectable } from '@angular/core';
|
||
|
import { ActivatedRouteSnapshot, CanActivate, Router, RouterStateSnapshot, UrlTree } from '@angular/router';
|
||
|
import { Observable } from 'rxjs';
|
||
|
import { AuthService } from '../_services/auth.service';
|
||
|
|
||
|
@Injectable({
|
||
|
providedIn: 'root'
|
||
|
})
|
||
|
export class AuthorizeGuard implements CanActivate {
|
||
|
constructor(private authService: AuthService, private router: Router) {
|
||
|
}
|
||
|
|
||
|
canActivate(
|
||
|
route: ActivatedRouteSnapshot,
|
||
|
state: RouterStateSnapshot
|
||
|
): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree {
|
||
|
console.log(this.authService, route, state);
|
||
|
if(this.authService.profile === undefined) {
|
||
|
console.log("not logged in");
|
||
|
this.router.navigate(['login', state.url.replace('/', '')]);
|
||
|
return false;
|
||
|
} else {
|
||
|
return true;
|
||
|
}
|
||
|
}
|
||
|
}
|