mirror of
https://github.com/bartvdbraak/omnidash.git
synced 2025-04-28 07:51:20 +00:00
feat: wip auth rework
This commit is contained in:
parent
f6c274b3cc
commit
82910e97e9
5 changed files with 270 additions and 230 deletions
|
@ -1,15 +1,44 @@
|
||||||
<script lang="ts">
|
<script lang="ts" context="module">
|
||||||
import { enhance } from '$app/forms';
|
import { z } from 'zod';
|
||||||
import { Icons } from '$lib/components/site/icons';
|
export const loginFormSchema = z.object({
|
||||||
import { Button } from '$lib/components/ui/button';
|
usernameEmail: z.string(),
|
||||||
import { Input } from '$lib/components/ui/input';
|
password: z.string()
|
||||||
import { Label } from '$lib/components/ui/label';
|
});
|
||||||
import * as Alert from '$lib/components/ui/alert';
|
export type LoginFormSchema = typeof loginFormSchema;
|
||||||
import { cn } from '$lib/utils';
|
</script>
|
||||||
import type { ActionData } from '../$types';
|
|
||||||
|
|
||||||
export let form: ActionData;
|
<script lang="ts">
|
||||||
export let isLoading = false;
|
import * as Form from '$lib/components/ui/form';
|
||||||
|
import { Input } from '$lib/components/ui/input';
|
||||||
|
import { type SuperValidated, type Infer, superForm } from 'sveltekit-superforms';
|
||||||
|
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';
|
||||||
|
|
||||||
|
export let data: SuperValidated<Infer<LoginFormSchema>>;
|
||||||
|
let isLoading = false;
|
||||||
|
|
||||||
|
const form = superForm(data, {
|
||||||
|
validators: zodClient(loginFormSchema),
|
||||||
|
onSubmit: () => {
|
||||||
|
isLoading = true;
|
||||||
|
toast.loading('Logging in...');
|
||||||
|
},
|
||||||
|
onUpdated: ({ form: f }) => {
|
||||||
|
isLoading = false;
|
||||||
|
if (f.valid) {
|
||||||
|
toast.success('Succesfully logged in.');
|
||||||
|
} else {
|
||||||
|
toast.error('Please fix the errors.');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
const { form: formData, enhance } = form;
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<div class="flex flex-col space-y-2 text-center">
|
<div class="flex flex-col space-y-2 text-center">
|
||||||
|
@ -19,45 +48,32 @@
|
||||||
</p>
|
</p>
|
||||||
</div>
|
</div>
|
||||||
<div class={cn('grid gap-6')} {...$$restProps}>
|
<div class={cn('grid gap-6')} {...$$restProps}>
|
||||||
<form
|
<form method="POST" action="?/login" class="grid gap-2" use:enhance>
|
||||||
method="POST"
|
<Form.Field {form} name="usernameEmail" class="grid gap-2">
|
||||||
action="?/login"
|
<Form.Control let:attrs>
|
||||||
use:enhance={() => {
|
<Form.Label>Username or email</Form.Label>
|
||||||
isLoading = true;
|
<Input {...attrs} bind:value={$formData.usernameEmail} />
|
||||||
return async ({ update }) => {
|
</Form.Control>
|
||||||
isLoading = false;
|
<Form.FieldErrors />
|
||||||
update();
|
</Form.Field>
|
||||||
};
|
<Form.Field {form} name="password" class="grid gap-2">
|
||||||
}}
|
<Form.Control let:attrs>
|
||||||
>
|
<Form.Label>Password</Form.Label>
|
||||||
<div class="grid gap-2">
|
<Input {...attrs} bind:value={$formData.password} type="password" />
|
||||||
<div class="grid gap-2">
|
</Form.Control>
|
||||||
<Label for="emailUsername">Email or username</Label>
|
<Form.FieldErrors />
|
||||||
<Input
|
</Form.Field>
|
||||||
id="emailUsername"
|
<Form.Button disabled={isLoading}>
|
||||||
name="emailUsername"
|
{#if isLoading}
|
||||||
type="text"
|
<Icons.spinner class="mr-2 h-4 w-4 animate-spin" />
|
||||||
autocapitalize="none"
|
{/if}
|
||||||
autocomplete="email"
|
Log in
|
||||||
autocorrect="off"
|
</Form.Button>
|
||||||
disabled={isLoading}
|
|
||||||
/>
|
|
||||||
</div>
|
|
||||||
<div class="grid gap-2">
|
|
||||||
<Label for="password">Password</Label>
|
|
||||||
<Input id="password" name="password" type="password" disabled={isLoading} />
|
|
||||||
</div>
|
|
||||||
<Button type="submit" disabled={isLoading}>
|
|
||||||
{#if isLoading}
|
|
||||||
<Icons.spinner class="mr-2 h-4 w-4 animate-spin" />
|
|
||||||
{/if}
|
|
||||||
Sign In
|
|
||||||
</Button>
|
|
||||||
</div>
|
|
||||||
{#if form?.notVerified}
|
|
||||||
<Alert.Root class="mt-4">
|
|
||||||
<Alert.Description>You must verify your email before you can log in.</Alert.Description>
|
|
||||||
</Alert.Root>
|
|
||||||
{/if}
|
|
||||||
</form>
|
</form>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
{#if dev && PUBLIC_DEBUG_FORMS == 'true' && browser}
|
||||||
|
<div class="pt-4">
|
||||||
|
<SuperDebug data={$formData} />
|
||||||
|
</div>
|
||||||
|
{/if}
|
||||||
|
|
|
@ -1,11 +1,46 @@
|
||||||
|
<script lang="ts" context="module">
|
||||||
|
import { z } from 'zod';
|
||||||
|
export const registerFormSchema = z.object({
|
||||||
|
username: z.string().min(3).max(24),
|
||||||
|
email: z.string().email(),
|
||||||
|
password: z.string().min(8),
|
||||||
|
passwordConfirm: z.string().min(8)
|
||||||
|
});
|
||||||
|
export type RegisterFormSchema = typeof registerFormSchema;
|
||||||
|
</script>
|
||||||
|
|
||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
import { enhance } from '$app/forms';
|
import * as Form from '$lib/components/ui/form';
|
||||||
import { Icons } from '$lib/components/site/icons';
|
|
||||||
import { Button } from '$lib/components/ui/button';
|
|
||||||
import { Input } from '$lib/components/ui/input';
|
import { Input } from '$lib/components/ui/input';
|
||||||
import { Label } from '$lib/components/ui/label';
|
import { type SuperValidated, type Infer, superForm } from 'sveltekit-superforms';
|
||||||
|
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';
|
import { cn } from '$lib/utils';
|
||||||
export let isLoading = false;
|
|
||||||
|
export let data: SuperValidated<Infer<RegisterFormSchema>>;
|
||||||
|
let isLoading = false;
|
||||||
|
|
||||||
|
const form = superForm(data, {
|
||||||
|
validators: zodClient(registerFormSchema),
|
||||||
|
onSubmit: () => {
|
||||||
|
isLoading = true;
|
||||||
|
toast.loading('Creating account...');
|
||||||
|
},
|
||||||
|
onUpdated: ({ form: f }) => {
|
||||||
|
isLoading = false;
|
||||||
|
if (f.valid) {
|
||||||
|
toast.success('Account created succesfully.');
|
||||||
|
} else {
|
||||||
|
toast.error('Please fix the errors.');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
const { form: formData, enhance } = form;
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<div class="flex flex-col space-y-2 text-center">
|
<div class="flex flex-col space-y-2 text-center">
|
||||||
|
@ -13,48 +48,46 @@
|
||||||
<p class="pb-6 text-sm text-muted-foreground">Enter your details below to create a new account</p>
|
<p class="pb-6 text-sm text-muted-foreground">Enter your details below to create a new account</p>
|
||||||
</div>
|
</div>
|
||||||
<div class={cn('grid gap-6')} {...$$restProps}>
|
<div class={cn('grid gap-6')} {...$$restProps}>
|
||||||
<form
|
<form method="POST" action="?/register" class="grid gap-2" use:enhance>
|
||||||
method="POST"
|
<Form.Field {form} name="email" class="grid gap-2">
|
||||||
action="?/register"
|
<Form.Control let:attrs>
|
||||||
use:enhance={() => {
|
<Form.Label>Email</Form.Label>
|
||||||
isLoading = true;
|
<Input {...attrs} bind:value={$formData.email} />
|
||||||
return async ({ update }) => {
|
</Form.Control>
|
||||||
isLoading = false;
|
<Form.FieldErrors />
|
||||||
update();
|
</Form.Field>
|
||||||
};
|
<Form.Field {form} name="username" class="grid gap-2">
|
||||||
}}
|
<Form.Control let:attrs>
|
||||||
>
|
<Form.Label>Username</Form.Label>
|
||||||
<div class="grid gap-2">
|
<Input {...attrs} bind:value={$formData.username} />
|
||||||
<div class="grid gap-2">
|
</Form.Control>
|
||||||
<Label for="email">Name</Label>
|
<Form.FieldErrors />
|
||||||
<Input id="name" name="name" type="name" disabled={isLoading} />
|
</Form.Field>
|
||||||
</div>
|
<Form.Field {form} name="password" class="grid gap-2">
|
||||||
<div class="grid gap-2">
|
<Form.Control let:attrs>
|
||||||
<Label for="email">Email</Label>
|
<Form.Label>Password</Form.Label>
|
||||||
<Input
|
<Input {...attrs} bind:value={$formData.password} type="password" />
|
||||||
id="email"
|
</Form.Control>
|
||||||
name="email"
|
<Form.FieldErrors />
|
||||||
type="email"
|
</Form.Field>
|
||||||
autocapitalize="none"
|
<Form.Field {form} name="passwordConfirm" class="grid gap-2">
|
||||||
autocomplete="email"
|
<Form.Control let:attrs>
|
||||||
autocorrect="off"
|
<Form.Label>Confirm password</Form.Label>
|
||||||
disabled={isLoading}
|
<Input {...attrs} bind:value={$formData.passwordConfirm} type="password" />
|
||||||
/>
|
</Form.Control>
|
||||||
</div>
|
<Form.FieldErrors />
|
||||||
<div class="grid gap-2">
|
</Form.Field>
|
||||||
<Label for="password">Password</Label>
|
<Form.Button disabled={isLoading}>
|
||||||
<Input id="password" name="password" type="password" disabled={isLoading} />
|
{#if isLoading}
|
||||||
</div>
|
<Icons.spinner class="mr-2 h-4 w-4 animate-spin" />
|
||||||
<div class="grid gap-2">
|
{/if}
|
||||||
<Label for="passwordConfirm">Confirm password</Label>
|
Log in
|
||||||
<Input id="passwordConfirm" name="passwordConfirm" type="password" disabled={isLoading} />
|
</Form.Button>
|
||||||
</div>
|
|
||||||
<Button type="submit" disabled={isLoading}>
|
|
||||||
{#if isLoading}
|
|
||||||
<Icons.spinner class="mr-2 h-4 w-4 animate-spin" />
|
|
||||||
{/if}
|
|
||||||
Register
|
|
||||||
</Button>
|
|
||||||
</div>
|
|
||||||
</form>
|
</form>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
{#if dev && PUBLIC_DEBUG_FORMS == 'true' && browser}
|
||||||
|
<div class="pt-4">
|
||||||
|
<SuperDebug data={$formData} />
|
||||||
|
</div>
|
||||||
|
{/if}
|
||||||
|
|
|
@ -1,14 +1,49 @@
|
||||||
|
<script lang="ts" context="module">
|
||||||
|
import { z } from 'zod';
|
||||||
|
export const ssoFormSchema = z.object({
|
||||||
|
token: z.string()
|
||||||
|
});
|
||||||
|
export type SsoFormSchema = typeof ssoFormSchema;
|
||||||
|
</script>
|
||||||
|
|
||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
|
import * as Form from '$lib/components/ui/form';
|
||||||
|
import { Input } from '$lib/components/ui/input';
|
||||||
|
import { type SuperValidated, type Infer, superForm } from 'sveltekit-superforms';
|
||||||
|
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 { Icons } from '$lib/components/site';
|
||||||
|
import { cn } from '$lib/utils';
|
||||||
import PocketBase from 'pocketbase';
|
import PocketBase from 'pocketbase';
|
||||||
import { PUBLIC_CLIENT_PB } from '$env/static/public';
|
import { PUBLIC_CLIENT_PB } from '$env/static/public';
|
||||||
import { enhance } from '$app/forms';
|
|
||||||
import { Button } from '$lib/components/ui/button';
|
import { Button } from '$lib/components/ui/button';
|
||||||
import { Separator } from '$lib/components/ui/separator';
|
import { Separator } from '$lib/components/ui/separator';
|
||||||
import * as DropdownMenu from '$lib/components/ui/dropdown-menu';
|
import * as DropdownMenu from '$lib/components/ui/dropdown-menu';
|
||||||
import { ChevronDown } from 'radix-icons-svelte';
|
import { ChevronDown } from 'radix-icons-svelte';
|
||||||
|
|
||||||
export let isLoading = false;
|
export let data: SuperValidated<Infer<SsoFormSchema>>;
|
||||||
|
let isLoading = false;
|
||||||
|
|
||||||
|
const form = superForm(data, {
|
||||||
|
validators: zodClient(ssoFormSchema),
|
||||||
|
onSubmit: () => {
|
||||||
|
isLoading = true;
|
||||||
|
toast.loading('Logging in...');
|
||||||
|
},
|
||||||
|
onUpdated: ({ form: f }) => {
|
||||||
|
isLoading = false;
|
||||||
|
if (f.valid) {
|
||||||
|
toast.success('Succesfully logged in.');
|
||||||
|
} else {
|
||||||
|
toast.error('Please fix the errors.');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
const { form: formData, enhance } = form;
|
||||||
|
|
||||||
/* eslint-disable @typescript-eslint/no-explicit-any */
|
/* eslint-disable @typescript-eslint/no-explicit-any */
|
||||||
export let providers: { name: string; icon?: any; displayName: string }[];
|
export let providers: { name: string; icon?: any; displayName: string }[];
|
||||||
|
@ -31,26 +66,14 @@
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<form
|
<form method="POST" action="?/sso" class="grid gap-2" use:enhance>
|
||||||
method="POST"
|
<Form.Field {form} name="token" class="grid gap-2">
|
||||||
action="?/oauth2"
|
<Form.Control let:attrs>
|
||||||
bind:this={oauth2Form}
|
<Input {...attrs} bind:value={$formData.token} class="hidden" />
|
||||||
use:enhance={() => {
|
</Form.Control>
|
||||||
isLoading = true;
|
<Form.FieldErrors />
|
||||||
return async ({ update }) => {
|
</Form.Field>
|
||||||
isLoading = false;
|
|
||||||
update();
|
|
||||||
};
|
|
||||||
}}
|
|
||||||
>
|
|
||||||
<div class="relative">
|
|
||||||
<div class="absolute inset-0 flex items-center">
|
|
||||||
<span class="w-full border-t" />
|
|
||||||
</div>
|
|
||||||
<div class="relative flex justify-center text-xs uppercase">
|
|
||||||
<span class="bg-background px-2 py-4 text-muted-foreground"> Or continue with </span>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
<div
|
<div
|
||||||
class="flex items-center justify-between whitespace-nowrap rounded-md border border-input bg-transparent shadow-sm transition-colors hover:bg-accent hover:text-accent-foreground focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring disabled:pointer-events-none disabled:opacity-50"
|
class="flex items-center justify-between whitespace-nowrap rounded-md border border-input bg-transparent shadow-sm transition-colors hover:bg-accent hover:text-accent-foreground focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring disabled:pointer-events-none disabled:opacity-50"
|
||||||
>
|
>
|
||||||
|
@ -115,3 +138,9 @@
|
||||||
{/if}
|
{/if}
|
||||||
</div>
|
</div>
|
||||||
</form>
|
</form>
|
||||||
|
|
||||||
|
{#if dev && PUBLIC_DEBUG_FORMS == 'true' && browser}
|
||||||
|
<div class="pt-4">
|
||||||
|
<SuperDebug data={$formData} />
|
||||||
|
</div>
|
||||||
|
{/if}
|
||||||
|
|
|
@ -1,118 +1,79 @@
|
||||||
import { error, redirect, type Cookies } from '@sveltejs/kit';
|
import type { PageServerLoad } from './$types';
|
||||||
import type { Actions } 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 = {
|
export const load: PageServerLoad = async () => {
|
||||||
login: async ({
|
return {
|
||||||
request,
|
loginForm: await superValidate(zod(loginFormSchema)),
|
||||||
locals,
|
registerForm: await superValidate(zod(registerFormSchema)),
|
||||||
cookies
|
ssoForm: await superValidate(zod(ssoFormSchema))
|
||||||
}: {
|
};
|
||||||
request: Request;
|
};
|
||||||
locals: App.Locals;
|
|
||||||
cookies: Cookies;
|
export const actions: Actions = {
|
||||||
}) => {
|
login: async ({ request, locals, cookies }) => {
|
||||||
const body = Object.fromEntries(await request.formData());
|
const form = await superValidate(request, zod(loginFormSchema));
|
||||||
|
if (!form.valid) {
|
||||||
|
return fail(400, {
|
||||||
|
form
|
||||||
|
});
|
||||||
|
}
|
||||||
try {
|
try {
|
||||||
const emailUsername = body.emailUsername.toString();
|
await locals.pocketBase
|
||||||
const password = body.password.toString();
|
.collection('users')
|
||||||
await locals.pocketBase.collection('users').authWithPassword(emailUsername, password);
|
.authWithPassword(form.data.usernameEmail, form.data.password);
|
||||||
if (!locals.pocketBase?.authStore?.model?.verified) {
|
|
||||||
locals.pocketBase.authStore.clear();
|
|
||||||
return {
|
|
||||||
notVerified: true
|
|
||||||
};
|
|
||||||
}
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.log('Error: ', err);
|
return fail(500, {
|
||||||
throw error(500, 'Something went wrong logging in');
|
form
|
||||||
|
});
|
||||||
}
|
}
|
||||||
cookies.set('pb_auth', JSON.stringify({ token: locals.pocketBase.authStore.token }), {
|
cookies.set('pb_auth', JSON.stringify({ token: locals.pocketBase.authStore.token }), {
|
||||||
path: '/'
|
path: '/'
|
||||||
});
|
});
|
||||||
|
|
||||||
throw redirect(303, '/');
|
throw redirect(303, '/');
|
||||||
},
|
},
|
||||||
register: async ({ request, locals }: { request: Request; locals: App.Locals }) => {
|
register: async ({ request, locals }) => {
|
||||||
if (locals.pocketBase.authStore.isValid) {
|
const form = await superValidate(request, zod(registerFormSchema));
|
||||||
return;
|
if (!form.valid) {
|
||||||
}
|
return fail(400, {
|
||||||
|
form
|
||||||
const formData = await request.formData();
|
});
|
||||||
|
}
|
||||||
const name = formData.get('name');
|
try {
|
||||||
const email = formData.get('email');
|
await locals.pocketBase.collection('users').create(form.data);
|
||||||
const password = formData.get('password');
|
return {
|
||||||
const passwordConfirm = formData.get('passwordConfirm');
|
form
|
||||||
|
};
|
||||||
try {
|
} catch (err) {
|
||||||
if (typeof name !== 'string') {
|
return fail(500, {
|
||||||
throw new Error('Name must be a string');
|
form
|
||||||
}
|
|
||||||
|
|
||||||
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
|
|
||||||
});
|
});
|
||||||
|
|
||||||
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 }) => {
|
sso: async ({ request, cookies }) => {
|
||||||
const form = await request.formData();
|
const form = await superValidate(request, zod(ssoFormSchema));
|
||||||
const token = form.get('token');
|
if (!form.valid) {
|
||||||
if (!token || typeof token !== 'string') {
|
return fail(400, {
|
||||||
throw redirect(303, '/auth');
|
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;
|
};
|
||||||
|
|
|
@ -4,8 +4,9 @@
|
||||||
import * as Tabs from '$lib/components/ui/tabs';
|
import * as Tabs from '$lib/components/ui/tabs';
|
||||||
import type { PageData } from './$types.js';
|
import type { PageData } from './$types.js';
|
||||||
import { LoginForm, RegisterForm, SSOForm } from './(components)';
|
import { LoginForm, RegisterForm, SSOForm } from './(components)';
|
||||||
export let form;
|
|
||||||
export let data: PageData;
|
export let data: PageData;
|
||||||
|
|
||||||
const { providers } = data;
|
const { providers } = data;
|
||||||
const providersWithIcons = providers.map((provider) => ({
|
const providersWithIcons = providers.map((provider) => ({
|
||||||
...provider,
|
...provider,
|
||||||
|
@ -22,7 +23,7 @@
|
||||||
|
|
||||||
<div class="lg:p-8">
|
<div class="lg:p-8">
|
||||||
<Tabs.Root
|
<Tabs.Root
|
||||||
value={form?.showLogin ? 'login' : tab}
|
value={tab}
|
||||||
class="mx-auto flex w-full flex-col justify-center space-y-6 sm:w-[350px]"
|
class="mx-auto flex w-full flex-col justify-center space-y-6 sm:w-[350px]"
|
||||||
>
|
>
|
||||||
<Tabs.List class="grid w-full grid-cols-2">
|
<Tabs.List class="grid w-full grid-cols-2">
|
||||||
|
@ -30,16 +31,16 @@
|
||||||
<Tabs.Trigger value="register">Register</Tabs.Trigger>
|
<Tabs.Trigger value="register">Register</Tabs.Trigger>
|
||||||
</Tabs.List>
|
</Tabs.List>
|
||||||
<Tabs.Content value="login">
|
<Tabs.Content value="login">
|
||||||
<LoginForm {form} />
|
<LoginForm data={data.loginForm} />
|
||||||
</Tabs.Content>
|
</Tabs.Content>
|
||||||
<Tabs.Content value="register">
|
<Tabs.Content value="register">
|
||||||
<RegisterForm />
|
<RegisterForm data={data.registerForm} />
|
||||||
</Tabs.Content>
|
</Tabs.Content>
|
||||||
<p class="px-8 text-center text-xs text-muted-foreground">
|
<p class="px-8 text-center text-xs text-muted-foreground">
|
||||||
Forgot password? <a class="text-primary underline" href="/reset-password">Reset password.</a>
|
Forgot password? <a class="text-primary underline" href="/reset-password">Reset password.</a>
|
||||||
</p>
|
</p>
|
||||||
{#if providers.length}
|
{#if providers.length}
|
||||||
<SSOForm providers={providersWithIcons} />
|
<SSOForm data={data.ssoForm} providers={providersWithIcons} />
|
||||||
{/if}
|
{/if}
|
||||||
</Tabs.Root>
|
</Tabs.Root>
|
||||||
</div>
|
</div>
|
||||||
|
|
Loading…
Reference in a new issue