import { Injectable } from '@angular/core';
import { ActivatedRouteSnapshot, CanActivate, RouterStateSnapshot } from '@angular/router';
import { Observable } from 'rxjs';

import { AuthService } from '../services/auth.service';


@Injectable({
    providedIn: 'root',
})
export class AuthGuard implements CanActivate {
    constructor(private auth: AuthService) { }

    public canActivate(
        _: ActivatedRouteSnapshot,
        state: RouterStateSnapshot,
    ): Observable<boolean> | Promise<boolean> | boolean {
        if (!this.auth.authenticated) {
            return this.auth.authenticate();
        }
        return this.auth.authenticated;
    }

}

{'avg_line_length': 26.56, 'max_line_length': 91, 'alphanum_fraction': 0.6716867469879518, 'licenses': ['Apache-2.0'], 'repository_name': 'jhutchings1/zitadel', 'path': 'console/src/app/guards/auth.guard.ts', 'size': 664, 'lang': 'TypeScript'}