34 lines
871 B
TypeScript
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;
|
|
}
|
|
} |