import { browser } from '$app/environment';
import { redirect } from '@sveltejs/kit';
import { getAccessToken, isJwtExpired, isValidJwtFormat } from '$lib/services/api';

export const ssr = false;

export const load = () => {
  if (!browser) return;

  const token = getAccessToken();
  const isAuthenticated = Boolean(token && isValidJwtFormat(token) && !isJwtExpired(token));

  if (isAuthenticated) {
    throw redirect(307, '/dashboard');
  }
};
