<script>
  import { onMount } from 'svelte';
  import { navigate } from 'svelte-routing'; // Asumiendo que estás usando svelte-routing
  import { writable } from 'svelte/store';

  let isAuthenticated = writable(false);
  let isVerified = writable(false);

 
  export let component;

  let redirect = window.location.pathname;
  const token = localStorage.getItem('token');
  const email = localStorage.getItem('email');

  const handleAccess = async () => {
      try {
          const response = await fetch('http://127.0.0.1:5007/api/login/validar', {
              method: 'POST',
              headers: {
                  'Content-Type': 'application/json',
              },
              body: JSON.stringify( {email, token, redirect} ),
          });

          if (response.ok) {
              isAuthenticated.set(true);
          }
      } finally {
          isVerified.set(true);
      }
  };

  onMount(async () => {
      await handleAccess();
  });
</script>

{#if !$isVerified}
    <div class="spinner-border position-absolute top-50 start-50 translate-middle" role="status">
        <span class="visually-hidden">Cargando</span>
    </div>
{:else}
    {#if $isAuthenticated}
        <svelte:component this={component}/>
    {:else}
        {navigate('/')}
    {/if}
{/if}
