Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add verify email flow for MFA #1308

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion src/routes/(console)/account/updateEmail.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
async function updateEmail() {
try {
await sdk.forConsole.account.updateEmail(email, emailPassword);
await invalidate(Dependencies.ACCOUNT);
await Promise.all([invalidate(Dependencies.ACCOUNT), invalidate(Dependencies.FACTORS)]);
addNotification({
message: 'Email has been updated',
type: 'success'
Expand Down
81 changes: 79 additions & 2 deletions src/routes/(console)/account/updateMfa.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
import type { Models } from '@appwrite.io/console';
import MfaRegenerateCodes from './mfaRegenerateCodes.svelte';
import { Pill } from '$lib/elements';
import { page } from '$app/stores';
import { onMount } from 'svelte';

let showSetup: boolean = false;
let showDelete: boolean = false;
Expand All @@ -26,6 +28,56 @@
.map(([factor, _]) => factor);
$: enabledMethods = enabledFactors.filter((factor) => factor !== 'recoveryCode');

$: cleanUrl = $page.url.origin + $page.url.pathname;

let creatingVerification = false;

async function createVerification() {
creatingVerification = true;

try {
await sdk.forConsole.account.createVerification(cleanUrl);
addNotification({
message: 'Verification email has been sent',
type: 'success'
});
} catch (error) {
addNotification({
message: error.message,
type: 'error'
});
} finally {
creatingVerification = false;
}
}

async function updateEmailVerification() {
const searchParams = $page.url.searchParams;
const userId = searchParams.get('userId');
const secret = searchParams.get('secret');

history.replaceState(null, '', cleanUrl);

if (userId && secret) {
try {
await sdk.forConsole.account.updateVerification(userId, secret);
addNotification({
message: 'Email verified successfully',
type: 'success'
});
await Promise.all([
invalidate(Dependencies.ACCOUNT),
invalidate(Dependencies.FACTORS)
]);
} catch (error) {
addNotification({
message: error.message,
type: 'error'
});
}
}
}

async function updateMfa() {
try {
await sdk.forConsole.account.updateMFA(!$user.mfa);
Expand Down Expand Up @@ -77,6 +129,10 @@
$: if (!showRecoveryCodes) {
codes = null;
}

onMount(() => {
updateEmailVerification();
});
</script>

<CardGrid>
Expand Down Expand Up @@ -109,7 +165,7 @@
<div
class="method u-flex u-flex-vertical-mobile u-gap-16 u-main-space-between u-sep-block-end"
style="padding-block-end: 16px">
<div class="u-flex u-gap-8">
<div class="u-flex u-gap-8 u-cross-baseline">
<div class="avatar is-size-x-small">
<span class="icon-device-mobile" aria-hidden="true" />
</div>
Expand Down Expand Up @@ -154,13 +210,34 @@
</div>
</div>
</div>
{:else if !$user.emailVerification}
<div
class="u-flex u-main-space-between u-sep-block-end"
style="padding-block-end: 16px">
<div class="u-flex u-gap-8 u-cross-baseline">
<div class="avatar is-size-x-small">
<span class="icon-mail" aria-hidden="true" />
</div>
<div class="u-flex-vertical u-gap-4">
<div class="u-flex u-gap-4 u-cross-center">
<span class="body-text-2 u-bold">Email</span>
<Pill>unverified</Pill>
</div>
<span>Verify your email to receive login MFA codes.</span>
</div>
</div>
<Button
secondary
disabled={creatingVerification}
on:click={() => createVerification()}>Verify</Button>
</div>
{/if}

{#if $factors.phone}
<div
class="u-flex u-main-space-between u-sep-block-end"
style="padding-block-end: 16px">
<div class="u-flex u-gap-8">
<div class="u-flex u-gap-8 u-cross-baseline">
<div class="avatar is-size-x-small">
<span class="icon-send" aria-hidden="true" />
</div>
Expand Down