authentication

This commit is contained in:
Bastian Wagner
2024-09-13 21:14:09 +02:00
parent c00aad559d
commit b4a5f04505
65 changed files with 1140 additions and 77 deletions

View File

@@ -0,0 +1,36 @@
import { inject, Injectable } from "@angular/core";
import { ActivatedRouteSnapshot, Router } from "@angular/router";
import { HotToastService } from "@ngxpert/hot-toast";
import { AuthService } from "./auth.service";
@Injectable({
providedIn: 'root'
})
export class AuthenticatedGuard {
public isLoading = false;
private router = inject(Router);
private toast = inject(HotToastService);
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;
}
}