Files
keyvault/client/src/app/core/auth/auth.guard.ts
2026-03-11 14:41:57 +01:00

34 lines
871 B
TypeScript

import { inject, Injectable } from "@angular/core";
import { ActivatedRouteSnapshot, Router } from "@angular/router";
import { AuthService } from "./auth.service";
@Injectable({
providedIn: 'root'
})
export class AuthenticatedGuard {
public isLoading = false;
private router = inject(Router);
private authService = inject(AuthService);
async canActivate(route: ActivatedRouteSnapshot):
Promise<boolean> {
this.isLoading = true;
if (this.authService.authenticated) { return true; }
const s = await this.authService.getMe();
if (s) {
return true;
}
const authCode = route.queryParams["code"];
if (authCode) {
const success = await this.authService.authenticateWithCode(authCode);
if (success) { return true; }
}
this.router.navigateByUrl('/login');
return false;
}
}