Skip to content

Commit bed6300

Browse files
authored
Fix: prevent back to auth on eID Wallet (#666)
* feat: implement authentication layout guard to prevent unauthorized access * format * fix: improve global state handling in authentication layout * sort imports * fix: handle guard failure in authentication layout * fix: ensure guard failure state is set on error in authentication layout
1 parent 2fef289 commit bed6300

1 file changed

Lines changed: 66 additions & 0 deletions

File tree

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<script lang="ts">
2+
import { goto } from "$app/navigation";
3+
import { page } from "$app/state";
4+
import type { GlobalState } from "$lib/global";
5+
import { getContext, onMount } from "svelte";
6+
7+
let { children } = $props();
8+
let isChecking = $state(true);
9+
let vaultExists = $state(false);
10+
let guardFailed = $state(false);
11+
12+
const getGlobalState = getContext<() => GlobalState>("globalState");
13+
14+
onMount(async () => {
15+
try {
16+
const globalState = getGlobalState();
17+
if (!globalState) {
18+
console.error("Global state is not defined");
19+
guardFailed = true;
20+
return;
21+
}
22+
23+
// Check if user is already authenticated
24+
const vault = await globalState.vaultController.vault;
25+
if (vault) {
26+
vaultExists = true;
27+
console.log("User already authenticated, redirecting to main");
28+
// Use replaceState to prevent infinite back loops
29+
await goto("/main", { replaceState: true });
30+
return;
31+
}
32+
33+
if (globalState.isOnboardingComplete) {
34+
const pinHash = await globalState.securityController.pinHash;
35+
const isAlreadyAtLogin = page.url.pathname === "/login";
36+
37+
if (pinHash && !isAlreadyAtLogin) {
38+
console.log(
39+
"Onboarding already complete, redirecting to login",
40+
);
41+
await goto("/login", { replaceState: true });
42+
return;
43+
}
44+
}
45+
} catch (error) {
46+
console.error("Error in auth layout guard:", error);
47+
guardFailed = true;
48+
} finally {
49+
isChecking = false;
50+
}
51+
});
52+
</script>
53+
54+
{#if isChecking}
55+
<div class="h-screen w-screen bg-background"></div>
56+
{:else if guardFailed}
57+
<div
58+
class="flex h-screen w-screen items-center justify-center bg-background"
59+
>
60+
<p class="text-center text-sm text-foreground-muted">
61+
An unexpected error occurred. Please restart the application.
62+
</p>
63+
</div>
64+
{:else if !vaultExists}
65+
{@render children()}
66+
{/if}

0 commit comments

Comments
 (0)