From 389fddc32e9118df7cd53a32e691998c50ecb779 Mon Sep 17 00:00:00 2001 From: Bradley Shellnut Date: Mon, 8 Apr 2024 17:47:54 -0700 Subject: [PATCH] Manually adding totp error and converting to use shadcn form on login. --- src/lib/validations/auth.ts | 2 +- src/routes/(auth)/login/+page.server.ts | 13 ++++--- src/routes/(auth)/login/+page.svelte | 47 +++++++++++++++---------- 3 files changed, 38 insertions(+), 24 deletions(-) diff --git a/src/lib/validations/auth.ts b/src/lib/validations/auth.ts index c656e75..77908a8 100644 --- a/src/lib/validations/auth.ts +++ b/src/lib/validations/auth.ts @@ -26,4 +26,4 @@ export const signInSchema = z.object({ .string({ required_error: 'Password is required' }) .trim(), totpToken: z.string().trim().min(6).max(6).optional() -}) \ No newline at end of file +}); diff --git a/src/routes/(auth)/login/+page.server.ts b/src/routes/(auth)/login/+page.server.ts index 1074e15..1585ddd 100644 --- a/src/routes/(auth)/login/+page.server.ts +++ b/src/routes/(auth)/login/+page.server.ts @@ -84,11 +84,10 @@ export const actions: Actions = { .onConflictDoNothing(); if (user?.two_factor_enabled && user?.two_factor_secret && !form?.data?.totpToken) { - return setError( + return fail(400, { form, - 'totpToken', - 'Two factor authentication is enabled. Please enter your 2FA code.', - ); + twoFactorRequired: true, + }); } else if (user?.two_factor_enabled && user?.two_factor_secret && form?.data?.totpToken) { console.log('totpToken', form.data.totpToken); const validOTP = await new TOTPController().verify( @@ -96,8 +95,12 @@ export const actions: Actions = { decodeHex(user.two_factor_secret) ); console.log('validOTP', validOTP); + form.errors.totpToken = ['Invalid TOTP code']; if (!validOTP) { - return setError(form, 'totpToken', 'Invalid 2FA code'); + return fail(400, { + form, + twoFactorRequired: true, + }); } } console.log('ip', locals.ip); diff --git a/src/routes/(auth)/login/+page.svelte b/src/routes/(auth)/login/+page.svelte index ce30c45..79bf9a5 100644 --- a/src/routes/(auth)/login/+page.svelte +++ b/src/routes/(auth)/login/+page.svelte @@ -4,6 +4,7 @@ import * as flashModule from 'sveltekit-flash-message/client'; import { AlertCircle } from "lucide-svelte"; import { signInSchema } from '$lib/validations/auth'; + import * as Form from '$lib/components/ui/form'; import { Label } from '$components/ui/label'; import { Input } from '$components/ui/input'; import { Button } from '$components/ui/button'; @@ -11,7 +12,9 @@ import { boredState } from '$lib/stores/boredState.js'; export let data; - const { form, errors, enhance } = superForm(data.form, { + export let form; + + const superLoginForm = superForm(data.form, { onSubmit: () => boredState.update((n) => ({ ...n, loading: true })), onResult: () => boredState.update((n) => ({ ...n, loading: false })), flashMessage: { @@ -30,6 +33,8 @@ validationMethod: 'oninput', delayMs: 0, }); + + const { form: loginForm, errors, enhance } = superLoginForm; @@ -43,24 +48,30 @@ > Log into your account - - - - - {#if $errors.totpToken} - - - {/if} - - {#if $errors._errors} - - - Error - - {$errors._errors} - - + + + Username + + + + + + + Password + + + + + {#if form?.twoFactorRequired} + + + 2FA Code + + + + {/if} + Login

By clicking continue, you agree to our