From b95766ea4bb16b9c873d6bf19789e7760a3e6e95 Mon Sep 17 00:00:00 2001 From: Bart van der Braak Date: Thu, 22 Feb 2024 09:54:35 +0100 Subject: [PATCH] refactor: support OAuth2Form component --- src/routes/(auth)/auth/(components)/index.ts | 2 +- .../auth/(components)/login-form.svelte | 1 - .../{sso-form.svelte => oauth2-form.svelte} | 81 +++++++------------ src/routes/(auth)/auth/+page.server.ts | 31 ++----- src/routes/(auth)/auth/+page.svelte | 4 +- 5 files changed, 40 insertions(+), 79 deletions(-) rename src/routes/(auth)/auth/(components)/{sso-form.svelte => oauth2-form.svelte} (69%) diff --git a/src/routes/(auth)/auth/(components)/index.ts b/src/routes/(auth)/auth/(components)/index.ts index 14e9bda..f70a7d6 100644 --- a/src/routes/(auth)/auth/(components)/index.ts +++ b/src/routes/(auth)/auth/(components)/index.ts @@ -1,3 +1,3 @@ export { default as LoginForm } from './login-form.svelte'; export { default as RegisterForm } from './register-form.svelte'; -export { default as SSOForm } from './sso-form.svelte'; +export { default as Oauth2Form } from './oauth2-form.svelte'; diff --git a/src/routes/(auth)/auth/(components)/login-form.svelte b/src/routes/(auth)/auth/(components)/login-form.svelte index b244264..012b634 100644 --- a/src/routes/(auth)/auth/(components)/login-form.svelte +++ b/src/routes/(auth)/auth/(components)/login-form.svelte @@ -14,7 +14,6 @@ import SuperDebug from 'sveltekit-superforms'; import { zodClient } from 'sveltekit-superforms/adapters'; import { browser, dev } from '$app/environment'; - // import { PUBLIC_DEBUG_FORMS } from '$env/static/public'; import { toast } from 'svelte-sonner'; import { Icons } from '$lib/components/site'; import { cn } from '$lib/utils'; diff --git a/src/routes/(auth)/auth/(components)/sso-form.svelte b/src/routes/(auth)/auth/(components)/oauth2-form.svelte similarity index 69% rename from src/routes/(auth)/auth/(components)/sso-form.svelte rename to src/routes/(auth)/auth/(components)/oauth2-form.svelte index 14962dd..293b942 100644 --- a/src/routes/(auth)/auth/(components)/sso-form.svelte +++ b/src/routes/(auth)/auth/(components)/oauth2-form.svelte @@ -1,49 +1,14 @@ - - -
- - - - - - - + { + isLoading = true; + return async ({ update }) => { + isLoading = false; + update(); + }; + }} +> +
+
+ +
+
+ Or continue with +
+
@@ -137,9 +120,3 @@ {/if}
- -{#if dev && debugForms && browser} -
- -
-{/if} diff --git a/src/routes/(auth)/auth/+page.server.ts b/src/routes/(auth)/auth/+page.server.ts index 15f6c76..9e39e50 100644 --- a/src/routes/(auth)/auth/+page.server.ts +++ b/src/routes/(auth)/auth/+page.server.ts @@ -3,14 +3,12 @@ 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 load: PageServerLoad = async () => { return { loginForm: await superValidate(zod(loginFormSchema)), - registerForm: await superValidate(zod(registerFormSchema)), - ssoForm: await superValidate(zod(ssoFormSchema)) + registerForm: await superValidate(zod(registerFormSchema)) }; }; @@ -54,26 +52,13 @@ export const actions: Actions = { }); } }, - 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 - }); + oauth2: async ({ request, cookies }) => { + const form = await request.formData(); + const token = form.get('token'); + if (!token || typeof token !== 'string') { + throw redirect(303, '/auth'); } + cookies.set('pb_auth', JSON.stringify({ token: token }), { path: '/' }); + throw redirect(303, '/'); } }; diff --git a/src/routes/(auth)/auth/+page.svelte b/src/routes/(auth)/auth/+page.svelte index 19c21ef..71d0fd1 100644 --- a/src/routes/(auth)/auth/+page.svelte +++ b/src/routes/(auth)/auth/+page.svelte @@ -3,7 +3,7 @@ import { Icons } from '$lib/components/site/index.js'; import * as Tabs from '$lib/components/ui/tabs'; import type { PageData } from './$types.js'; - import { LoginForm, RegisterForm, SSOForm } from './(components)'; + import { LoginForm, RegisterForm, Oauth2Form } from './(components)'; export let data: PageData; @@ -37,7 +37,7 @@ Forgot password? Reset password.

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