Skip to content

Commit d226706

Browse files
authored
Fetch identity info data in load function (#3464)
* Add auth method to authentication state. * Identity data in load function. * Identity data in load function.
1 parent ef429fa commit d226706

File tree

3 files changed

+54
-8
lines changed

3 files changed

+54
-8
lines changed

src/frontend/src/routes/(new-styling)/login/+page.svelte

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
import { AuthLastUsedFlow } from "$lib/flows/authLastUsedFlow.svelte";
1616
import Header from "$lib/components/layout/Header.svelte";
1717
import Footer from "$lib/components/layout/Footer.svelte";
18-
import { goto } from "$app/navigation";
18+
import { goto, preloadData } from "$app/navigation";
1919
import ProgressRing from "$lib/components/ui/ProgressRing.svelte";
2020
import { AuthWizard } from "$lib/components/wizards/auth";
2121
import type { PageProps } from "./$types";
@@ -30,23 +30,30 @@
3030
3131
const { data }: PageProps = $props();
3232
33-
const gotoNext = () => goto(data.next ?? "/manage", { replaceState: true });
33+
let redirectingIdentity = $state<bigint>();
3434
35+
// Show loading indicator until load function of next page has been preloaded
36+
const gotoNext = async (identityNumber: bigint) => {
37+
redirectingIdentity = identityNumber;
38+
const next = data.next ?? "/manage";
39+
await preloadData(next);
40+
await goto(next, { replaceState: true });
41+
};
3542
const onMigration = async (identityNumber: bigint) => {
3643
lastUsedIdentitiesStore.selectIdentity(identityNumber);
3744
toaster.success({
3845
title: $t`Migration completed successfully`,
3946
duration: 4000,
4047
});
4148
isAuthDialogOpen = false;
42-
await goto("/manage");
49+
await gotoNext(identityNumber);
4350
};
4451
const onSignIn = async (identityNumber: bigint) => {
4552
lastUsedIdentitiesStore.selectIdentity(identityNumber);
4653
isAuthDialogOpen = false;
4754
authenticationV2Funnel.trigger(AuthenticationV2Events.GoToDashboard);
4855
authenticationV2Funnel.close();
49-
await gotoNext();
56+
await gotoNext(identityNumber);
5057
};
5158
const onSignUp = async (identityNumber: bigint) => {
5259
toaster.success({
@@ -57,7 +64,7 @@
5764
isAuthDialogOpen = false;
5865
authenticationV2Funnel.trigger(AuthenticationV2Events.GoToDashboard);
5966
authenticationV2Funnel.close();
60-
await gotoNext();
67+
await gotoNext(identityNumber);
6168
};
6269
6370
const lastUsedIdentities = $derived(
@@ -111,10 +118,12 @@
111118
<li class="contents">
112119
<ButtonCard
113120
onclick={() => handleContinueAs(identity)}
114-
disabled={nonNullish(authLastUsedFlow.authenticatingIdentity)}
121+
disabled={nonNullish(
122+
authLastUsedFlow.authenticatingIdentity,
123+
) || nonNullish(redirectingIdentity)}
115124
>
116125
<Avatar size="sm">
117-
{#if identity.identityNumber === authLastUsedFlow.authenticatingIdentity}
126+
{#if identity.identityNumber === authLastUsedFlow.authenticatingIdentity || identity.identityNumber === redirectingIdentity}
118127
<ProgressRing />
119128
{:else}
120129
<UserIcon class="size-5" />
@@ -134,7 +143,8 @@
134143
</ul>
135144
<ButtonCard
136145
onclick={() => (isAuthDialogOpen = true)}
137-
disabled={nonNullish(authLastUsedFlow.authenticatingIdentity)}
146+
disabled={nonNullish(authLastUsedFlow.authenticatingIdentity) ||
147+
nonNullish(redirectingIdentity)}
138148
>
139149
<FeaturedIcon size="sm">
140150
<PlusIcon class="size-5" />
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import type { LayoutLoad } from "./$types";
2+
import { redirect } from "@sveltejs/kit";
3+
import { get } from "svelte/store";
4+
import { authenticationStore } from "$lib/stores/authentication.store";
5+
import { lastUsedIdentitiesStore } from "$lib/stores/last-used-identities.store";
6+
import { isNullish } from "@dfinity/utils";
7+
import { throwCanisterError } from "$lib/utils/utils";
8+
9+
// This load function will later move to /manage root
10+
export const load: LayoutLoad = async ({ url }) => {
11+
const authentication = get(authenticationStore);
12+
const selectedIdentity = get(lastUsedIdentitiesStore).selected;
13+
if (
14+
isNullish(authentication) ||
15+
isNullish(selectedIdentity) ||
16+
authentication.identityNumber !== selectedIdentity.identityNumber
17+
) {
18+
// Add original target URL as next search param,
19+
// if it's not the default target URL (/manage).
20+
const next = url.pathname + url.search;
21+
const location = new URL("/login", url.origin);
22+
if (next !== "/manage") {
23+
location.searchParams.set("next", next);
24+
}
25+
throw redirect(307, location);
26+
}
27+
28+
const identityInfo = await authentication.actor
29+
.identity_info(selectedIdentity.identityNumber)
30+
.then(throwCanisterError);
31+
32+
return { identityInfo, identityNumber: selectedIdentity.identityNumber };
33+
};
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<script lang="ts">
2+
// Placeholder until next PR(s)
3+
</script>

0 commit comments

Comments
 (0)