feat: add auth and database foundation
This commit is contained in:
@@ -0,0 +1,11 @@
|
||||
<script lang="ts">
|
||||
import './layout.css';
|
||||
import favicon from '$lib/assets/favicon.svg';
|
||||
import { Toaster } from '$lib/components/ui/sonner/index.js';
|
||||
|
||||
let { children } = $props();
|
||||
</script>
|
||||
|
||||
<svelte:head><link rel="icon" href={favicon} /></svelte:head>
|
||||
{@render children()}
|
||||
<Toaster position="top-center" richColors />
|
||||
@@ -0,0 +1,6 @@
|
||||
import { redirect } from '@sveltejs/kit';
|
||||
import type { PageServerLoad } from './$types';
|
||||
|
||||
export const load: PageServerLoad = () => {
|
||||
redirect(303, '/dashboard');
|
||||
};
|
||||
@@ -0,0 +1,2 @@
|
||||
<h1>Welcome to SvelteKit</h1>
|
||||
<p>Visit <a href="https://svelte.dev/docs/kit">svelte.dev/docs/kit</a> to read the documentation</p>
|
||||
@@ -0,0 +1,64 @@
|
||||
import { redirect } from '@sveltejs/kit';
|
||||
import { APIError } from 'better-auth/api';
|
||||
import { message, superValidate } from 'sveltekit-superforms/server';
|
||||
import { zod4 } from 'sveltekit-superforms/adapters';
|
||||
import { auth } from '$lib/server/auth';
|
||||
import { loginSchema } from '$lib/schemas/auth';
|
||||
import type { Actions, PageServerLoad } from './$types';
|
||||
|
||||
function getSafeRedirect(url: URL) {
|
||||
const redirectTo = url.searchParams.get('redirectTo');
|
||||
|
||||
if (!redirectTo || !redirectTo.startsWith('/') || redirectTo.startsWith('//')) {
|
||||
return '/dashboard';
|
||||
}
|
||||
|
||||
return redirectTo;
|
||||
}
|
||||
|
||||
export const load: PageServerLoad = async ({ locals, url }) => {
|
||||
if (locals.user) {
|
||||
redirect(303, getSafeRedirect(url));
|
||||
}
|
||||
|
||||
return {
|
||||
form: await superValidate(
|
||||
{
|
||||
email: url.searchParams.get('email') ?? '',
|
||||
password: ''
|
||||
},
|
||||
zod4(loginSchema),
|
||||
{ id: 'login' }
|
||||
)
|
||||
};
|
||||
};
|
||||
|
||||
export const actions: Actions = {
|
||||
default: async (event) => {
|
||||
const form = await superValidate(event, zod4(loginSchema), { id: 'login' });
|
||||
|
||||
if (!form.valid) {
|
||||
return message(form, 'Check the highlighted fields.', { status: 400 });
|
||||
}
|
||||
|
||||
try {
|
||||
await auth.api.signInEmail({
|
||||
body: {
|
||||
email: form.data.email,
|
||||
password: form.data.password,
|
||||
callbackURL: getSafeRedirect(event.url)
|
||||
}
|
||||
});
|
||||
} catch (error) {
|
||||
if (error instanceof APIError) {
|
||||
return message(form, error.message || 'The email or password you entered is incorrect.', {
|
||||
status: 400
|
||||
});
|
||||
}
|
||||
|
||||
return message(form, 'Something went wrong while signing you in.', { status: 500 });
|
||||
}
|
||||
|
||||
redirect(303, getSafeRedirect(event.url));
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,12 @@
|
||||
<script lang="ts">
|
||||
import LoginForm from '$lib/components/login-form.svelte';
|
||||
import type { PageData } from './$types';
|
||||
|
||||
let { data }: { data: PageData } = $props();
|
||||
</script>
|
||||
|
||||
<div class="flex min-h-svh flex-col items-center justify-center gap-6 bg-background p-6 md:p-10">
|
||||
<div class="w-full max-w-sm">
|
||||
<LoginForm form={data.form} />
|
||||
</div>
|
||||
</div>
|
||||
Reference in New Issue
Block a user