-
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
<script lang="ts"> | ||
import { browser } from '$app/environment'; | ||
import { onMount, onDestroy } from 'svelte'; | ||
import { Link } from '@appwrite.io/pink-svelte'; | ||
let { | ||
storageKey = 'resend_cooldown_default', | ||
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. lets not have a default, always mark as required. |
||
seconds = 60, | ||
disabled = $bindable(false), | ||
onResend | ||
}: { | ||
storageKey?: string; | ||
seconds?: number; | ||
disabled?: boolean; | ||
onResend: () => Promise<void> | void; | ||
} = $props(); | ||
let remaining = $state(0); | ||
let interval: ReturnType<typeof setInterval> | null = null; | ||
function start(now = Date.now()) { | ||
const end = now + seconds * 1000; | ||
if (browser) { | ||
localStorage.setItem(`${storageKey}:end`, String(end)); | ||
} | ||
tick(end); | ||
startTick(end); | ||
} | ||
function restore() { | ||
if (!browser) return; | ||
const raw = localStorage.getItem(`${storageKey}:end`); | ||
if (!raw) return; | ||
const end = parseInt(raw); | ||
const now = Date.now(); | ||
const rem = Math.max(0, Math.ceil((end - now) / 1000)); | ||
if (rem > 0) { | ||
remaining = rem; | ||
startTick(end); | ||
} else { | ||
localStorage.removeItem(`${storageKey}:end`); | ||
remaining = 0; | ||
} | ||
} | ||
function tick(end: number) { | ||
const now = Date.now(); | ||
remaining = Math.max(0, Math.ceil((end - now) / 1000)); | ||
if (remaining === 0 && browser) { | ||
localStorage.removeItem(`${storageKey}:end`); | ||
} | ||
} | ||
function startTick(end: number) { | ||
clearTick(); | ||
interval = setInterval(() => { | ||
tick(end); | ||
if (remaining === 0) clearTick(); | ||
}, 1000); | ||
} | ||
function clearTick() { | ||
if (interval) { | ||
clearInterval(interval); | ||
interval = null; | ||
} | ||
} | ||
async function handleResend() { | ||
if (disabled || remaining > 0) return; | ||
await onResend?.(); | ||
start(); | ||
} | ||
onMount(restore); | ||
onDestroy(() => clearTick()); | ||
</script> | ||
|
||
{#if remaining > 0} | ||
Try again in {remaining}s | ||
{:else} | ||
<Link.Button on:click={handleResend} {disabled}>Resend code</Link.Button> | ||
{/if} | ||
ItzNotABug marked this conversation as resolved.
Show resolved
Hide resolved
|
Original file line number | Diff line number | Diff line change | ||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -15,9 +15,33 @@ | |||||||||||||||
import { Layout } from '@appwrite.io/pink-svelte'; | ||||||||||||||||
let mail: string, pass: string, disabled: boolean; | ||||||||||||||||
let showPasswordLogin: boolean = false; | ||||||||||||||||
export let data; | ||||||||||||||||
$: showPasswordLogin = pass && pass.length > 0; | ||||||||||||||||
async function sendSignInCode() { | ||||||||||||||||
try { | ||||||||||||||||
disabled = true; | ||||||||||||||||
// use createEmailToken for sign in with code | ||||||||||||||||
const sessionToken = await sdk.forConsole.account.createEmailToken({ | ||||||||||||||||
userId: 'unique', | ||||||||||||||||
email: mail | ||||||||||||||||
}); | ||||||||||||||||
await goto( | ||||||||||||||||
`${base}/login/email-otp?email=${encodeURIComponent(mail)}&userId=${sessionToken.userId}` | ||||||||||||||||
); | ||||||||||||||||
Comment on lines
+34
to
+35
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. Preserve existing query params when switching to the OTP flow Right now we build the OTP URL with only - await goto(
- `${base}/login/email-otp?email=${encodeURIComponent(mail)}&userId=${sessionToken.userId}`
- );
+ const params = new URLSearchParams(window.location.search);
+ params.set('email', mail);
+ params.set('userId', sessionToken.userId);
+
+ await goto(`${base}/login/email-otp?${params.toString()}`); 📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents
|
||||||||||||||||
} catch (error) { | ||||||||||||||||
disabled = false; | ||||||||||||||||
addNotification({ | ||||||||||||||||
type: 'error', | ||||||||||||||||
message: error.message | ||||||||||||||||
}); | ||||||||||||||||
} | ||||||||||||||||
} | ||||||||||||||||
async function login() { | ||||||||||||||||
try { | ||||||||||||||||
disabled = true; | ||||||||||||||||
|
@@ -52,7 +76,6 @@ | |||||||||||||||
return; | ||||||||||||||||
} | ||||||||||||||||
// no specific redirect, so redirect will happen through invalidating the account | ||||||||||||||||
await invalidate(Dependencies.ACCOUNT); | ||||||||||||||||
} catch (error) { | ||||||||||||||||
disabled = false; | ||||||||||||||||
|
@@ -92,22 +115,27 @@ | |||||||||||||||
<Unauthenticated coupon={data?.couponData} campaign={data?.campaign}> | ||||||||||||||||
<svelte:fragment slot="title">Sign in</svelte:fragment> | ||||||||||||||||
<svelte:fragment> | ||||||||||||||||
<Form onSubmit={login}> | ||||||||||||||||
<Form onSubmit={showPasswordLogin ? login : sendSignInCode}> | ||||||||||||||||
<Layout.Stack> | ||||||||||||||||
<InputEmail | ||||||||||||||||
id="email" | ||||||||||||||||
label="Email" | ||||||||||||||||
placeholder="Email" | ||||||||||||||||
autofocus={true} | ||||||||||||||||
required={true} | ||||||||||||||||
bind:value={mail} /> | ||||||||||||||||
<InputPassword | ||||||||||||||||
id="password" | ||||||||||||||||
label="Password" | ||||||||||||||||
placeholder="Password" | ||||||||||||||||
required={true} | ||||||||||||||||
required={false} | ||||||||||||||||
bind:value={pass} /> | ||||||||||||||||
<Button fullWidth submit {disabled}>Sign in</Button> | ||||||||||||||||
|
||||||||||||||||
{#if showPasswordLogin} | ||||||||||||||||
<Button fullWidth submit {disabled}>Sign in</Button> | ||||||||||||||||
{:else} | ||||||||||||||||
<Button fullWidth submit {disabled}>Get sign in code</Button> | ||||||||||||||||
{/if} | ||||||||||||||||
|
||||||||||||||||
{#if isCloud} | ||||||||||||||||
<span class="with-separators eyebrow-heading-3">or</span> | ||||||||||||||||
<Button secondary fullWidth on:click={onGithubLogin} {disabled}> | ||||||||||||||||
|
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.
empty?