import { isPlatformBrowser } from '@angular/common';
import { inject, PLATFORM_ID } from '@angular/core';
import { ResolveFn } from '@angular/router';
import { KeycloakService } from '../services/keycloak.service';

export const authResolver: ResolveFn<boolean> = async (route, state) => {
  const keycloakService: KeycloakService = inject(KeycloakService);
  const platformId = inject(PLATFORM_ID);
  const isBrowser = isPlatformBrowser(platformId);

  if (!keycloakService.isLoggedIn() && isBrowser) {
    await keycloakService.login({
      redirectUri: window.location.href,
    });
  }

  return true;
};
