From 82910e97e91e206385da9202d4646dc208216b65 Mon Sep 17 00:00:00 2001 From: Bart van der Braak Date: Wed, 21 Feb 2024 04:19:09 +0100 Subject: [PATCH] feat: wip auth rework --- .../auth/(components)/login-form.svelte | 118 ++++++------ .../auth/(components)/register-form.svelte | 129 ++++++++----- .../(auth)/auth/(components)/sso-form.svelte | 73 +++++--- src/routes/(auth)/auth/+page.server.ts | 169 +++++++----------- src/routes/(auth)/auth/+page.svelte | 11 +- 5 files changed, 270 insertions(+), 230 deletions(-) diff --git a/src/routes/(auth)/auth/(components)/login-form.svelte b/src/routes/(auth)/auth/(components)/login-form.svelte index 5b569ea..b5cb157 100644 --- a/src/routes/(auth)/auth/(components)/login-form.svelte +++ b/src/routes/(auth)/auth/(components)/login-form.svelte @@ -1,15 +1,44 @@ - - export let form: ActionData; - export let isLoading = false; +
@@ -19,45 +48,32 @@

-
{ - isLoading = true; - return async ({ update }) => { - isLoading = false; - update(); - }; - }} - > -
-
- - -
-
- - -
- -
- {#if form?.notVerified} - - You must verify your email before you can log in. - - {/if} + + + + Username or email + + + + + + + Password + + + + + + {#if isLoading} + + {/if} + Log in +
+ +{#if dev && PUBLIC_DEBUG_FORMS == 'true' && browser} +
+ +
+{/if} diff --git a/src/routes/(auth)/auth/(components)/register-form.svelte b/src/routes/(auth)/auth/(components)/register-form.svelte index f22caef..fc7dff1 100644 --- a/src/routes/(auth)/auth/(components)/register-form.svelte +++ b/src/routes/(auth)/auth/(components)/register-form.svelte @@ -1,11 +1,46 @@ + +
@@ -13,48 +48,46 @@

Enter your details below to create a new account

-
{ - isLoading = true; - return async ({ update }) => { - isLoading = false; - update(); - }; - }} - > -
-
- - -
-
- - -
-
- - -
-
- - -
- -
+ + + + Email + + + + + + + Username + + + + + + + Password + + + + + + + Confirm password + + + + + + {#if isLoading} + + {/if} + Log in +
+ +{#if dev && PUBLIC_DEBUG_FORMS == 'true' && browser} +
+ +
+{/if} diff --git a/src/routes/(auth)/auth/(components)/sso-form.svelte b/src/routes/(auth)/auth/(components)/sso-form.svelte index 2dbc75e..60e5ccf 100644 --- a/src/routes/(auth)/auth/(components)/sso-form.svelte +++ b/src/routes/(auth)/auth/(components)/sso-form.svelte @@ -1,14 +1,49 @@ + + -
{ - isLoading = true; - return async ({ update }) => { - isLoading = false; - update(); - }; - }} -> -
-
- -
-
- Or continue with -
-
+ + + + + + + +
@@ -115,3 +138,9 @@ {/if}
+ +{#if dev && PUBLIC_DEBUG_FORMS == 'true' && browser} +
+ +
+{/if} diff --git a/src/routes/(auth)/auth/+page.server.ts b/src/routes/(auth)/auth/+page.server.ts index 9649e65..15f6c76 100644 --- a/src/routes/(auth)/auth/+page.server.ts +++ b/src/routes/(auth)/auth/+page.server.ts @@ -1,118 +1,79 @@ -import { error, redirect, type Cookies } from '@sveltejs/kit'; -import type { Actions } from './$types'; +import type { PageServerLoad } from './$types'; +import { superValidate } from 'sveltekit-superforms'; +import { zod } from 'sveltekit-superforms/adapters'; +import { loginFormSchema } from './(components)/login-form.svelte'; +import { registerFormSchema } from './(components)/register-form.svelte'; +import { ssoFormSchema } from './(components)/sso-form.svelte'; +import { fail, type Actions, redirect } from '@sveltejs/kit'; -export const actions = { - login: async ({ - request, - locals, - cookies - }: { - request: Request; - locals: App.Locals; - cookies: Cookies; - }) => { - const body = Object.fromEntries(await request.formData()); +export const load: PageServerLoad = async () => { + return { + loginForm: await superValidate(zod(loginFormSchema)), + registerForm: await superValidate(zod(registerFormSchema)), + ssoForm: await superValidate(zod(ssoFormSchema)) + }; +}; + +export const actions: Actions = { + login: async ({ request, locals, cookies }) => { + const form = await superValidate(request, zod(loginFormSchema)); + if (!form.valid) { + return fail(400, { + form + }); + } try { - const emailUsername = body.emailUsername.toString(); - const password = body.password.toString(); - await locals.pocketBase.collection('users').authWithPassword(emailUsername, password); - if (!locals.pocketBase?.authStore?.model?.verified) { - locals.pocketBase.authStore.clear(); - return { - notVerified: true - }; - } + await locals.pocketBase + .collection('users') + .authWithPassword(form.data.usernameEmail, form.data.password); } catch (err) { - console.log('Error: ', err); - throw error(500, 'Something went wrong logging in'); + return fail(500, { + form + }); } cookies.set('pb_auth', JSON.stringify({ token: locals.pocketBase.authStore.token }), { path: '/' }); - throw redirect(303, '/'); }, - register: async ({ request, locals }: { request: Request; locals: App.Locals }) => { - if (locals.pocketBase.authStore.isValid) { - return; - } - - const formData = await request.formData(); - - const name = formData.get('name'); - const email = formData.get('email'); - const password = formData.get('password'); - const passwordConfirm = formData.get('passwordConfirm'); - - try { - if (typeof name !== 'string') { - throw new Error('Name must be a string'); - } - - if (name.length === 0) { - throw new Error('Please enter a valid name'); - } - - if (typeof email !== 'string') { - throw new Error('Email must be a string'); - } - - if (email.length < 5) { - throw new Error('Please enter a valid e-mail address'); - } - - if (typeof password !== 'string') { - throw new Error('Password must be a string'); - } - - if (password.length < 8) { - throw new Error('Password must be at least 8 characters in length'); - } - - if (password !== passwordConfirm) { - throw new Error('Passwords do not match'); - } - - await locals.pocketBase.collection('users').create({ - name, - email, - password, - passwordConfirm + register: async ({ request, locals }) => { + const form = await superValidate(request, zod(registerFormSchema)); + if (!form.valid) { + return fail(400, { + form + }); + } + try { + await locals.pocketBase.collection('users').create(form.data); + return { + form + }; + } catch (err) { + return fail(500, { + form }); - - await locals.pocketBase.collection('users').authWithPassword(email, password); - if (!locals.pocketBase?.authStore?.model?.verified) { - locals.pocketBase.authStore.clear(); - return { - showLogin: true, - isLoading: false, - notVerified: true - }; - } - } catch (error) { - console.error(error); - - if (!(error instanceof Error)) { - return { - name, - email, - password, - error: 'Unknown error occured when signing up user' - }; - } - - return { error: error.message, name, email, password }; } - - throw redirect(303, '/'); }, - oauth2: async ({ request, cookies }) => { - const form = await request.formData(); - const token = form.get('token'); - if (!token || typeof token !== 'string') { - throw redirect(303, '/auth'); + sso: async ({ request, cookies }) => { + const form = await superValidate(request, zod(ssoFormSchema)); + if (!form.valid) { + return fail(400, { + form + }); + } + const token = form.data.token; + try { + if (!token || typeof token !== 'string') { + throw redirect(303, '/auth'); + } + cookies.set('pb_auth', JSON.stringify({ token: token }), { path: '/' }); + return { + form + }; + } catch (err) { + return fail(500, { + form + }); } - cookies.set('pb_auth', JSON.stringify({ token: token }), { path: '/' }); - throw redirect(303, '/'); } -} satisfies Actions; +}; diff --git a/src/routes/(auth)/auth/+page.svelte b/src/routes/(auth)/auth/+page.svelte index 944a22f..12c1646 100644 --- a/src/routes/(auth)/auth/+page.svelte +++ b/src/routes/(auth)/auth/+page.svelte @@ -4,8 +4,9 @@ import * as Tabs from '$lib/components/ui/tabs'; import type { PageData } from './$types.js'; import { LoginForm, RegisterForm, SSOForm } from './(components)'; - export let form; + export let data: PageData; + const { providers } = data; const providersWithIcons = providers.map((provider) => ({ ...provider, @@ -22,7 +23,7 @@
@@ -30,16 +31,16 @@ Register - + - +

Forgot password? Reset password.

{#if providers.length} - + {/if}