jklink/src/app/service/auth.service.ts

29 lines
767 B
TypeScript

import { Injectable } from '@angular/core';
import {
ActivatedRouteSnapshot,
CanActivate,
GuardResult,
MaybeAsync,
Router,
RouterStateSnapshot
} from '@angular/router';
import PocketBase from 'pocketbase';
import { environment } from '../../environments/environment';
@Injectable({
providedIn: 'root'
})
export class AuthGuard implements CanActivate {
constructor(private router: Router) { };
async canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Promise<boolean> {
const pb = new PocketBase(environment.POCKETBASE);
await pb.collection('users').authRefresh().catch(() => this.router.navigate(['']));
if (pb.authStore.isValid) {
return true;
}
this.router.navigate(['']);
return false;
}
}