Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions infrastructure/eid-wallet/src/routes/(auth)/+layout.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<script lang="ts">
import { goto } from "$app/navigation";
import { page } from "$app/state";
import type { GlobalState } from "$lib/global";
import { getContext, onMount } from "svelte";

let { children } = $props();
let isChecking = $state(true);
let vaultExists = $state(false);
let guardFailed = $state(false);

const getGlobalState = getContext<() => GlobalState>("globalState");

onMount(async () => {
try {
const globalState = getGlobalState();
if (!globalState) {
console.error("Global state is not defined");
guardFailed = true;
return;
}

// Check if user is already authenticated
const vault = await globalState.vaultController.vault;
if (vault) {
vaultExists = true;
console.log("User already authenticated, redirecting to main");
// Use replaceState to prevent infinite back loops
await goto("/main", { replaceState: true });
return;
}

if (globalState.isOnboardingComplete) {
const pinHash = await globalState.securityController.pinHash;
const isAlreadyAtLogin = page.url.pathname === "/login";

if (pinHash && !isAlreadyAtLogin) {
console.log(
"Onboarding already complete, redirecting to login",
);
await goto("/login", { replaceState: true });
return;
}
}
} catch (error) {
console.error("Error in auth layout guard:", error);
guardFailed = true;
} finally {
isChecking = false;
}
});
</script>

{#if isChecking}
<div class="h-screen w-screen bg-background"></div>
{:else if guardFailed}
<div
class="flex h-screen w-screen items-center justify-center bg-background"
>
<p class="text-center text-sm text-foreground-muted">
An unexpected error occurred. Please restart the application.
</p>
</div>
{:else if !vaultExists}
{@render children()}
{/if}