Skip to content

Commit d204d6b

Browse files
committed
fix frontend
1 parent 42393f8 commit d204d6b

4 files changed

Lines changed: 68 additions & 29 deletions

File tree

src/lib/helpers/emailVerification.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import type { Models } from '@appwrite.io/console';
22

3+
const EMAIL_VERIFICATION_REQUIRED_ERROR_TYPE = 'user_email_not_verified';
4+
35
export function isEmailVerificationEnabled(
46
consoleVariables: Models.ConsoleVariables | undefined
57
): boolean {
@@ -9,3 +11,7 @@ export function isEmailVerificationEnabled(
911

1012
return String(consoleVariables._APP_CONSOLE_EMAIL_VERIFICATION) === 'true';
1113
}
14+
15+
export function isEmailVerificationRequiredError(type: string | undefined): boolean {
16+
return type === EMAIL_VERIFICATION_REQUIRED_ERROR_TYPE;
17+
}

src/routes/(console)/+error.svelte

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
1-
<script>
2-
import { base } from '$app/paths';
1+
<script lang="ts">
2+
import { goto } from '$app/navigation';
3+
import { base, resolve } from '$app/paths';
34
import { page } from '$app/state';
45
import { Button } from '$lib/elements/forms';
6+
import { isEmailVerificationRequiredError } from '$lib/helpers/emailVerification';
57
import { Container } from '$lib/layout';
68
import { Typography } from '@appwrite.io/pink-svelte';
9+
10+
$effect(() => {
11+
if (isEmailVerificationRequiredError(page.error.type)) {
12+
goto(resolve('/verify-email'), { replaceState: true });
13+
}
14+
});
715
</script>
816

917
<Container>

src/routes/(console)/+layout.ts

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,17 @@ import { sdk } from '$lib/stores/sdk';
22
import { isCloud } from '$lib/system';
33
import type { LayoutLoad } from './$types';
44
import { Dependencies } from '$lib/constants';
5-
import { Platform, Query } from '@appwrite.io/console';
5+
import { AppwriteException, Platform, Query } from '@appwrite.io/console';
66
import { makePlansMap } from '$lib/helpers/billing';
77
import { plansInfo as plansInfoStore } from '$lib/stores/billing';
88
import { normalizeConsoleVariables } from '$lib/helpers/domains';
99
import { syncServerTime } from '$lib/helpers/fingerprint';
1010
import { redirect } from '@sveltejs/kit';
1111
import { resolve } from '$app/paths';
12-
import { isEmailVerificationEnabled } from '$lib/helpers/emailVerification';
12+
import { isEmailVerificationRequiredError } from '$lib/helpers/emailVerification';
1313

14-
export const load: LayoutLoad = async ({ depends, parent, url }) => {
15-
const { organizations, plansInfo, account } = await parent();
14+
export const load: LayoutLoad = async ({ depends, parent }) => {
15+
const { organizations, plansInfo } = await parent();
1616

1717
depends(Dependencies.RUNTIMES);
1818
depends(Dependencies.CONSOLE_VARIABLES);
@@ -41,22 +41,15 @@ export const load: LayoutLoad = async ({ depends, parent, url }) => {
4141
return response.json() as { version?: string };
4242
}),
4343
sdk.forConsole.console.variables()
44-
]);
45-
46-
const consoleVariables = normalizeConsoleVariables(rawConsoleVariables);
47-
48-
if (
49-
isCloud &&
50-
account &&
51-
!account.emailVerification &&
52-
isEmailVerificationEnabled(consoleVariables)
53-
) {
54-
const isVerifyEmailPage = url.pathname === resolve('/verify-email');
55-
56-
if (!isVerifyEmailPage) {
44+
]).catch((error) => {
45+
if (error instanceof AppwriteException && isEmailVerificationRequiredError(error.type)) {
5746
redirect(303, resolve('/verify-email'));
5847
}
59-
}
48+
49+
throw error;
50+
});
51+
52+
const consoleVariables = normalizeConsoleVariables(rawConsoleVariables);
6053

6154
let fallbackPlansInfoArray = plansInfo;
6255
if (!fallbackPlansInfoArray) {
@@ -81,6 +74,10 @@ export const load: LayoutLoad = async ({ depends, parent, url }) => {
8174
})
8275
).total;
8376
} catch (e) {
77+
if (e instanceof AppwriteException && isEmailVerificationRequiredError(e.type)) {
78+
redirect(303, resolve('/verify-email'));
79+
}
80+
8481
projectsCount = 0;
8582
}
8683
}

src/routes/+layout.ts

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,13 @@ import type { LayoutLoad } from './$types';
88
import { redirectTo } from './store';
99
import { resolve } from '$app/paths';
1010
import type { Account } from '$lib/stores/user';
11-
import { type AppwriteException, Platform } from '@appwrite.io/console';
11+
import { AppwriteException, Platform } from '@appwrite.io/console';
1212
import { isCloud } from '$lib/system';
1313
import { checkPricingRefAndRedirect } from '$lib/helpers/pricingRedirect';
1414
import { getTeamOrOrganizationList } from '$lib/stores/organization';
1515
import { makePlansMap } from '$lib/helpers/billing';
1616
import { plansInfo as plansInfoStore } from '$lib/stores/billing';
17+
import { isEmailVerificationRequiredError } from '$lib/helpers/emailVerification';
1718

1819
export const ssr = false;
1920

@@ -32,14 +33,33 @@ export const load: LayoutLoad = async ({ depends, url, route }) => {
3233
}
3334

3435
if (account) {
35-
const plansInfo = await getPlatformPlans();
36-
plansInfoStore.set(plansInfo);
37-
38-
return {
39-
plansInfo,
40-
account: account,
41-
organizations: await getTeamOrOrganizationList()
42-
};
36+
try {
37+
const [plansInfo, organizations] = await Promise.all([
38+
getPlatformPlans(),
39+
getTeamOrOrganizationList()
40+
]);
41+
42+
plansInfoStore.set(plansInfo);
43+
44+
return {
45+
plansInfo,
46+
account: account,
47+
organizations
48+
};
49+
} catch (error) {
50+
if (
51+
error instanceof AppwriteException &&
52+
isEmailVerificationRequiredError(error.type)
53+
) {
54+
const verifyEmailUrl = resolve('/verify-email');
55+
56+
if (url.pathname !== verifyEmailUrl) {
57+
redirect(303, withParams(verifyEmailUrl, url.searchParams));
58+
}
59+
}
60+
61+
throw error;
62+
}
4363
}
4464

4565
const isPublicRoute = route.id?.startsWith('/(public)');
@@ -57,6 +77,14 @@ export const load: LayoutLoad = async ({ depends, url, route }) => {
5777
redirect(303, withParams(mfaUrl, url.searchParams));
5878
}
5979

80+
if (isEmailVerificationRequiredError(error.type)) {
81+
const verifyEmailUrl = resolve('/verify-email');
82+
83+
if (url.pathname !== verifyEmailUrl) {
84+
redirect(303, withParams(verifyEmailUrl, url.searchParams));
85+
}
86+
}
87+
6088
if (!isPublicRoute) {
6189
if (isCloud) {
6290
checkPricingRefAndRedirect(url.searchParams, true);

0 commit comments

Comments
 (0)