15 lines
670 B
TypeScript
15 lines
670 B
TypeScript
import { inject, Injectable } from '@angular/core';
|
|
import { ActivatedRouteSnapshot, CanActivate, Router, RouterStateSnapshot } from '@angular/router';
|
|
import { environment } from '../environments/environment';
|
|
|
|
@Injectable()
|
|
export class AuthGuard implements CanActivate {
|
|
private readonly router = inject(Router);
|
|
|
|
canActivate(_route: ActivatedRouteSnapshot, _state: RouterStateSnapshot): boolean {
|
|
const token = environment.apiKey??localStorage.getItem('api-key');
|
|
if (!environment.production && !token)
|
|
this.router.navigate(['login'], { queryParams: { redirectTo: btoa(window.location.pathname + window.location.search) } });
|
|
return true;
|
|
}
|
|
} |