authentication
This commit is contained in:
36
client/src/app/core/auth/auth.guard.ts
Normal file
36
client/src/app/core/auth/auth.guard.ts
Normal 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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user