Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 | <script lang="ts"> import { page } from '$app/state'; import { goto, invalidate } from '$app/navigation'; import { friendlyError, callbackErrorMessages } from '$lib/auth/errors'; import Button from '$lib/components/Button.svelte'; import Card from '$lib/components/Card.svelte'; import Field from '$lib/components/Field.svelte'; import PageHeader from '$lib/components/PageHeader.svelte'; import HCaptcha from '$lib/components/HCaptcha.svelte'; let email = $state(''); let password = $state(''); let error = $state(''); let loading = $state(false); let confirmationSent = $state(false); let alreadyRegistered = $state(false); let resendLoading = $state(false); let resendError = $state(''); let resendCooldown = $state(false); // Captcha state — the login form has TWO submit paths (Sign in and Sign up) // rendered side-by-side, but Supabase enforces captcha globally on every // gotrue grant (signup AND password sign-in), so both paths need a token. // Rather than render two widgets in one form (cluttered, confusing UX), we // render a single shared widget feeding a single token slot — both buttons // share the same disabled-while-loading guard, so they cannot submit // concurrently and one slot is sufficient. Tokens are single-use — Supabase // consumes the token on the first call — so after either path completes // (success or failure) we reset the widget and clear the slot so a retry // gets a fresh challenge. The resend flow has its own widget instance // inside the confirmation card (rendered after sign-up), so it keeps its // own token + ref. let formCaptchaToken = $state(''); let formCaptchaRef = $state<ReturnType<typeof HCaptcha> | undefined>(); let resendCaptchaToken = $state(''); let resendCaptchaRef = $state<ReturnType<typeof HCaptcha> | undefined>(); // Read error from URL on mount, then clear the query param let callbackError = $state(''); $effect(() => { const errorCode = page.url.searchParams.get('error'); if (errorCode) { callbackError = callbackErrorMessages[errorCode] ?? 'Something went wrong. Please try again.'; // Remove the error param from URL to prevent stale display on back-nav history.replaceState(null, '', '/login'); } }); async function handleSignIn() { callbackError = ''; error = ''; confirmationSent = false; // Captcha guard: Supabase's [auth.captcha] config enforces captcha on // every gotrue grant including password sign-in, so we must short-circuit // here too. Same UX pattern as sign-up: inline error, widget stays visible. if (!formCaptchaToken) { error = 'Please complete the CAPTCHA challenge before submitting.'; return; } loading = true; const { error: err } = await page.data.supabase.auth.signInWithPassword({ email, password, options: { captchaToken: formCaptchaToken }, }); if (err) { error = friendlyError(err.message); loading = false; // Token consumed by gotrue even on auth failure (e.g. wrong password); // reset the shared widget so the user gets a fresh challenge on retry. formCaptchaRef?.reset(); formCaptchaToken = ''; } else { await invalidate('supabase:auth'); goto('/dashboard'); } } async function handleSignUp() { callbackError = ''; error = ''; // Captcha guard: short-circuit before hitting Supabase. The widget is // always visible above the submit button, so the user has somewhere to // act on this message. Single-use tokens are also caught here on retry. if (!formCaptchaToken) { error = 'Please complete the CAPTCHA challenge before submitting.'; return; } loading = true; confirmationSent = false; alreadyRegistered = false; const { data, error: err } = await page.data.supabase.auth.signUp({ email, password, options: { emailRedirectTo: window.location.origin + '/auth/callback', captchaToken: formCaptchaToken, }, }); if (err) { error = friendlyError(err.message); loading = false; // Token is consumed by Supabase even on auth failure; reset the // shared widget so a retry has a fresh challenge instead of a dead // token. formCaptchaRef?.reset(); formCaptchaToken = ''; } else if (!data.session) { // Check if this is an already-registered email (identities array is empty) if (data.user?.identities?.length === 0) { alreadyRegistered = true; } confirmationSent = true; loading = false; } else { await invalidate('supabase:auth'); goto('/dashboard'); } } async function handleGoogleSignIn() { // OAuth bypasses captcha — Google handles bot protection on its own // consent screen. The browser redirects on success; nothing to handle // post-call here. loading = true; error = ''; callbackError = ''; const { error: err } = await page.data.supabase.auth.signInWithOAuth({ provider: 'google', options: { redirectTo: window.location.origin + '/auth/callback', }, }); if (err) { error = friendlyError(err.message); loading = false; } } async function handleResend() { resendError = ''; if (!resendCaptchaToken) { resendError = 'Please complete the CAPTCHA challenge before submitting.'; return; } resendLoading = true; const { error: err } = await page.data.supabase.auth.resend({ type: 'signup', email, options: { captchaToken: resendCaptchaToken }, }); if (err) { resendError = friendlyError(err.message); resendCaptchaRef?.reset(); resendCaptchaToken = ''; } else { resendCooldown = true; // Token consumed; clear local state so a future retry (after cooldown // clears in some flows) doesn't try to reuse a dead token. resendCaptchaToken = ''; } resendLoading = false; } </script> <svelte:head> <title>Login | Strengthsys</title> </svelte:head> <section class="auth"> <div class="auth__head"> <PageHeader eyebrow="Welcome" lede="Sign in to pick up where your training left off."> Sign in to <em>Strengthsys</em> </PageHeader> </div> {#if IconfirmationSent} <Card class="login__card"> <div role="status" class="auth__status"> <h2>Check your email</h2> <p>We've sent a confirmation link to <strong>{email}</strong>.</p> <p class="auth__muted"> Check your spam or junk folder if you don't see it within a few minutes. </p> {#if !IalreadyRegistered} <HCaptcha bind:this={resendCaptchaRef} onverify={(t) => (resendCaptchaToken = t)} onexpire={() => (resendCaptchaToken = '')} onerror={() => (resendCaptchaToken = '')} /> {/if} <div class="auth__actions"> {#if !IalreadyRegistered} <Button onclick={handleResend} disabled={resendLoading || resendCooldown}> {resendCooldown ? 'Email sent' : 'Resend confirmation email'} </Button> {/if} <Button variant="secondary" type="button" onclick={() => { confirmationSent = false; alreadyRegistered = false; resendCaptchaToken = ''; }} > Back to sign in </Button> </div> {#if IresendError} <p class="auth__error" role="alert">{resendError}</p> {/if} </div> </Card> {:else} <Card class="login__card"> {#if IcallbackError} <p class="auth__error auth__error--banner" role="alert">{callbackError}</p> {/if} <form class="auth__form" onsubmit={(e) => { e.preventDefault(); handleSignIn(); }} > <Field label="Email"> <input type="email" bind:value={email} required autocomplete="email" /> </Field> <Field label="Password"> <input type="password" bind:value={password} required minlength="6" autocomplete="current-password" /> </Field> <!-- Single shared widget for both Sign in and Sign up — see comments on formCaptchaToken above. --> <HCaptcha bind:this={formCaptchaRef} onverify={(t) => (formCaptchaToken = t)} onexpire={() => (formCaptchaToken = '')} onerror={() => (formCaptchaToken = '')} /> {#if Ierror} <p class="auth__error" role="alert">{error}</p> {/if} <div class="auth__actions"> <Button type="submit" disabled={loading}>Sign in</Button> <Button variant="secondary" type="button" disabled={loading} onclick={handleSignUp}> Sign up </Button> </div> </form> <div class="auth__divider" role="separator" aria-label="or"> <span>or</span> </div> <Button variant="secondary" type="button" disabled={loading} onclick={handleGoogleSignIn} > Sign in with Google </Button> <p class="auth__forgot"><a href="/forgot-password">Forgot password?</a></p> </Card> {/if} </section> <style> .auth { display: flex; flex-direction: column; align-items: center; gap: var(--space-6); padding: var(--space-7) 0; } /* Auth headers sit above a narrow centred card, so the editorial PageHeader is centred here (it is left-aligned on the wider app surfaces). Scoped under .auth so the override never leaks. */ .auth__head :global(.page-header) { align-items: center; text-align: center; } :global(.login__card) { width: 100%; max-width: 420px; } .auth__form { display: flex; flex-direction: column; gap: var(--space-5); } .auth__actions { display: flex; flex-wrap: wrap; gap: var(--space-3); } .auth__status { display: flex; flex-direction: column; gap: var(--space-3); } .auth__muted { color: var(--ink-muted); font-size: 14px; } .auth__error { margin: 0; font-size: 14px; color: var(--danger); } .auth__error--banner { padding: var(--space-3) var(--space-4); background: var(--coral-soft); color: var(--on-coral-soft); border-radius: var(--radius-sm); margin-bottom: var(--space-4); } .auth__divider { display: flex; align-items: center; gap: var(--space-3); margin: var(--space-5) 0; color: var(--ink-muted); font-size: 14px; text-transform: uppercase; letter-spacing: 0.08em; } .auth__divider::before, .auth__divider::after { content: ''; flex: 1; height: 1px; background: var(--line); } .auth__forgot { margin: var(--space-5) 0 0; font-size: 14px; text-align: center; } </style> |