-
Notifications
You must be signed in to change notification settings - Fork 191
feat: Implement email OTP authentication #2414
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
Open
HarshMN2345
wants to merge
3
commits into
main
Choose a base branch
from
feat-SER-191-Implement-Email-OTP-Flow
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+293
−98
Open
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
118 changes: 118 additions & 0 deletions
118
src/routes/(public)/(guest)/login/email-otp/+page.svelte
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. svelte5 runes. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
<script lang="ts"> | ||
import { goto, invalidate } from '$app/navigation'; | ||
import { base } from '$app/paths'; | ||
import { InputDigits, Button } from '$lib/elements/forms'; | ||
import { addNotification } from '$lib/stores/notifications'; | ||
import { sdk } from '$lib/stores/sdk'; | ||
import { Unauthenticated } from '$lib/layout'; | ||
import { Dependencies } from '$lib/constants'; | ||
import { Submit, trackEvent, trackError } from '$lib/actions/analytics'; | ||
import { redirectTo } from '$routes/store'; | ||
import { user } from '$lib/stores/user'; | ||
import { Layout, Typography, Link } from '@appwrite.io/pink-svelte'; | ||
import { page } from '$app/state'; | ||
|
||
let otpCode: string = ''; | ||
let disabled: boolean = false; | ||
let email: string = ''; | ||
let userId: string = ''; | ||
|
||
export let data; | ||
|
||
// Get email and userId from URL params | ||
$: email = page.url.searchParams.get('email') || ''; | ||
$: userId = page.url.searchParams.get('userId') || ''; | ||
|
||
async function verifyOTP() { | ||
try { | ||
disabled = true; | ||
// Use createSession with the userId from createEmailToken | ||
await sdk.forConsole.account.createSession({ userId: userId, secret: otpCode }); | ||
|
||
if ($user) { | ||
trackEvent(Submit.AccountLogin, { mfa_used: 'none' }); | ||
addNotification({ | ||
type: 'success', | ||
message: 'Successfully logged in.' | ||
}); | ||
} | ||
|
||
if (data?.couponData?.code) { | ||
trackEvent(Submit.AccountCreate, { | ||
campaign_name: data?.couponData?.code, | ||
email: email, | ||
name: $user?.name | ||
}); | ||
await goto(`${base}/apply-credit?code=${data?.couponData?.code}`); | ||
return; | ||
} | ||
if (data?.campaign?.$id) { | ||
await goto(`${base}/apply-credit?campaign=${data.campaign?.$id}`); | ||
return; | ||
} | ||
if ($redirectTo) { | ||
window.location.href = $redirectTo; | ||
return; | ||
} | ||
|
||
await invalidate(Dependencies.ACCOUNT); | ||
} catch (error) { | ||
disabled = false; | ||
addNotification({ | ||
type: 'error', | ||
message: error.message | ||
}); | ||
trackError(error, Submit.AccountLogin); | ||
} | ||
} | ||
|
||
async function resendCode() { | ||
try { | ||
disabled = true; | ||
const sessionToken = await sdk.forConsole.account.createEmailToken({ | ||
userId: 'unique', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use |
||
email: email | ||
}); | ||
userId = sessionToken.userId; | ||
|
||
addNotification({ | ||
type: 'success', | ||
message: 'New sign in code sent to your email.' | ||
}); | ||
} catch (error) { | ||
addNotification({ | ||
type: 'error', | ||
message: error.message | ||
}); | ||
} finally { | ||
disabled = false; | ||
} | ||
} | ||
</script> | ||
|
||
<svelte:head> | ||
<title>Enter your sign-in code - Appwrite</title> | ||
</svelte:head> | ||
|
||
<Unauthenticated coupon={data?.couponData} campaign={data?.campaign} align="center"> | ||
<svelte:fragment slot="title">Enter your sign-in code</svelte:fragment> | ||
<svelte:fragment> | ||
<Layout.Stack gap="l" justifyContent="center" alignContent="center" alignItems="center"> | ||
<Typography.Text align="center"> | ||
We sent a 6-digit code to {email} | ||
</Typography.Text> | ||
|
||
<form on:submit|preventDefault={verifyOTP}> | ||
<Layout.Stack align="center"> | ||
<InputDigits bind:value={otpCode} required /> | ||
<Button submit {disabled} class="verify-button">Verify</Button> | ||
</Layout.Stack> | ||
</form> | ||
|
||
<Typography.Text align="center"> | ||
Didn't get it? | ||
<Link.Button on:click={resendCode} {disabled}>Resend code</Link.Button> | ||
</Typography.Text> | ||
</Layout.Stack> | ||
</svelte:fragment> | ||
</Unauthenticated> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { base } from '$app/paths'; | ||
import type { Campaign } from '$lib/stores/campaigns'; | ||
import { sdk } from '$lib/stores/sdk'; | ||
import { redirect } from '@sveltejs/kit'; | ||
import type { PageLoad } from './$types'; | ||
|
||
export const load: PageLoad = async ({ url }) => { | ||
if (!url.searchParams.has('email')) { | ||
redirect(303, `${base}/login`); | ||
} | ||
|
||
if (url.searchParams.has('code')) { | ||
const code = url.searchParams.get('code'); | ||
let campaign: Campaign; | ||
try { | ||
const couponData = await sdk.forConsole.billing.getCoupon(code); | ||
if (couponData.campaign) { | ||
campaign = await sdk.forConsole.billing.getCampaign(couponData.campaign); | ||
return { | ||
couponData, | ||
campaign | ||
}; | ||
} else redirect(303, `${base}/login`); | ||
} catch (e) { | ||
redirect(303, `${base}/login`); | ||
} | ||
} | ||
if (url.searchParams.has('campaign')) { | ||
const campaignId = url.searchParams.get('campaign'); | ||
let campaign: Campaign; | ||
try { | ||
campaign = await sdk.forConsole.billing.getCampaign(campaignId); | ||
return { campaign }; | ||
} catch (e) { | ||
redirect(303, `${base}/login`); | ||
} | ||
} | ||
return; | ||
}; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Preserve existing query params when switching to the OTP flow
Right now we build the OTP URL with only
email
anduserId
, which strips any pre-existing query params (code
,campaign
,redirect
, etc.). The new loader inemail-otp/+page.ts
expects those params to rehydratedata
, so dropping them prevents us from ever hitting the coupon/campaign apply-credit paths and also loses any redirect target after a successful OTP login. Please carry forward the current search params before addingemail
/userId
.📝 Committable suggestion
🤖 Prompt for AI Agents