-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(app/sveltekit-example-app): add sign-up flow (#39)
- Loading branch information
1 parent
1bb686d
commit 8c3a578
Showing
11 changed files
with
532 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { lucia } from '@packages/lucia-auth' | ||
|
||
import type { Handle } from '@sveltejs/kit' | ||
|
||
export const handle: Handle = async ({ event, resolve }) => { | ||
const sessionId = event.cookies.get(lucia.sessionCookieName) | ||
if (!sessionId) { | ||
event.locals.user = null | ||
event.locals.session = null | ||
return resolve(event) | ||
} | ||
|
||
const { user, session } = await lucia.validateSession(sessionId) | ||
|
||
if (session?.fresh) { | ||
const sessionCookie = lucia.createSessionCookie(session.id) | ||
event.cookies.set(sessionCookie.name, sessionCookie.value, { | ||
path: '.', | ||
...sessionCookie.attributes, | ||
}) | ||
} | ||
|
||
if (!session) { | ||
const sessionCookie = lucia.createBlankSessionCookie() | ||
event.cookies.set(sessionCookie.name, sessionCookie.value, { | ||
path: '.', | ||
...sessionCookie.attributes, | ||
}) | ||
} | ||
|
||
event.locals.user = user | ||
event.locals.session = session | ||
|
||
return resolve(event) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,15 @@ | ||
// place files you want to import through the `$lib` alias in this folder. | ||
export {} | ||
import { z } from 'zod' | ||
|
||
const password = z.string().min(6).max(255) | ||
|
||
export const schema = z | ||
.object({ | ||
email: z.string().email(), | ||
password: password, | ||
password_confirm: password, | ||
}) | ||
.refine((data) => data.password === data.password_confirm, { | ||
message: 'Passwords do not match', | ||
path: ['password_confirm'], | ||
}) |
41 changes: 41 additions & 0 deletions
41
apps/sveltekit-example-app/src/routes/sign-up/+page.server.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { fail } from '@sveltejs/kit' | ||
import { message, setError, superValidate } from 'sveltekit-superforms' | ||
import { zod } from 'sveltekit-superforms/adapters' | ||
|
||
import { schema } from '$lib' | ||
import { lucia } from '@packages/lucia-auth' | ||
import { createUser } from '@packages/lucia-auth/repository' | ||
|
||
import type { Actions, PageServerLoad } from './$types.js' | ||
|
||
export const load = (async () => { | ||
const form = await superValidate(zod(schema)) | ||
return { form } | ||
}) satisfies PageServerLoad | ||
|
||
export const actions = { | ||
default: async ({ request, cookies }) => { | ||
const form = await superValidate(request, zod(schema)) | ||
|
||
if (!form.valid) { | ||
return fail(400, { form }) | ||
} | ||
|
||
try { | ||
const userId = await createUser({ username: form.data.email, password: form.data.password }) | ||
if (userId) { | ||
const session = await lucia.createSession(userId, {}) | ||
const sessionCookie = lucia.createSessionCookie(session.id) | ||
cookies.set(sessionCookie.name, sessionCookie.value, { | ||
path: '.', | ||
...sessionCookie.attributes, | ||
}) | ||
return message(form, 'Form posted successfully!') | ||
} | ||
} catch (err) { | ||
if (err instanceof Error) { | ||
return setError(form, 'email', 'E-mail already exists.') | ||
} | ||
} | ||
}, | ||
} satisfies Actions |
56 changes: 56 additions & 0 deletions
56
apps/sveltekit-example-app/src/routes/sign-up/+page.svelte
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<script lang="ts"> | ||
import SuperDebug, { superForm } from 'sveltekit-superforms' | ||
import { zodClient } from 'sveltekit-superforms/adapters' | ||
import { schema } from '$lib' | ||
import type { PageData } from './$types.js' | ||
export let data: PageData | ||
const { form, message, errors, constraints, enhance } = superForm(data.form, { | ||
validators: zodClient(schema), | ||
validationMethod: 'auto', | ||
}) | ||
</script> | ||
|
||
{#if $message}<h3>{$message}</h3>{/if} | ||
|
||
<form method="POST" novalidate use:enhance> | ||
<label for="email">Email</label> | ||
<input | ||
name="email" | ||
type="email" | ||
aria-invalid={$errors.email ? 'true' : undefined} | ||
bind:value={$form.email} | ||
{...$constraints.email} /> | ||
|
||
{#if $errors.email}<span class="text-red-500">{$errors.email}</span>{/if} | ||
|
||
<label for="password">Password</label> | ||
<input | ||
name="password" | ||
type="password" | ||
aria-invalid={$errors.password ? 'true' : undefined} | ||
bind:value={$form.password} | ||
{...$constraints.password} /> | ||
{#if $errors.password}<span class="text-red-500">{$errors.password}</span>{/if} | ||
|
||
<label for="password-confirm">Password</label> | ||
<input | ||
name="password_confirm" | ||
type="password" | ||
aria-invalid={$errors.password_confirm ? 'true' : undefined} | ||
bind:value={$form.password_confirm} | ||
{...$constraints.password_confirm} /> | ||
{#if $errors.password_confirm} | ||
<span class="text-red-500">{$errors.password_confirm}</span> | ||
{/if} | ||
<div> | ||
<button>Submit</button> | ||
</div> | ||
</form> | ||
|
||
<div class="w-1/2 pt-10"> | ||
<SuperDebug data={$form} collapsible={true} /> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,12 @@ | ||
import forms from '@tailwindcss/forms' | ||
|
||
/** @type {import('tailwindcss').Config} */ | ||
const config = { | ||
content: ['./src/**/*.{html,js,svelte,ts}'], | ||
theme: { | ||
extend: {}, | ||
}, | ||
plugins: [], | ||
plugins: [forms], | ||
} | ||
|
||
export default config |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { Argon2id } from 'oslo/password' | ||
|
||
import { db } from '@packages/drizzledb-pg/client' | ||
import * as schema from '@packages/drizzledb-pg/schema' | ||
import { users } from '@packages/drizzledb-pg/schema' | ||
|
||
export type NewUser = Omit<typeof schema.users.$inferInsert, 'id' | 'hashedPassword'> & { | ||
password: string | ||
} | ||
|
||
export type User = typeof schema.users.$inferSelect | ||
|
||
export const createUser = async (user: NewUser): Promise<string | undefined> => { | ||
const hashedPassword = await new Argon2id().hash(user.password) | ||
return db | ||
.insert(schema.users) | ||
.values({ username: user.username, hashedPassword }) | ||
.returning({ id: users.id }) | ||
.then((res) => res[0]?.id) | ||
.catch((err) => { | ||
throw err | ||
}) | ||
} |
Oops, something went wrong.