forked from passlock-dev/passlock
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path+page.server.ts
57 lines (48 loc) · 1.85 KB
/
+page.server.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// +page.server.ts
import { registrationFormSchema } from '$lib/schemas'
import { TokenVerifier } from '@passlock/sveltekit'
import { superValidate } from 'sveltekit-superforms'
import { valibot } from 'sveltekit-superforms/adapters'
import type { PageServerLoad } from './$types'
import { PASSLOCK_API_KEY } from '$env/static/private'
import { PUBLIC_PASSLOCK_ENDPOINT, PUBLIC_PASSLOCK_TENANCY_ID } from '$env/static/public'
import { app, verifyEmailAwaitLink, verifyEmailCode } from '$lib/routes'
import { lucia } from '$lib/server/auth'
import { createUser } from '$lib/server/db'
import { fail, redirect } from '@sveltejs/kit'
import type { Actions } from './$types'
const tokenVerifier = new TokenVerifier({
tenancyId: PUBLIC_PASSLOCK_TENANCY_ID,
apiKey: PASSLOCK_API_KEY,
endpoint: PUBLIC_PASSLOCK_ENDPOINT
})
export const load: PageServerLoad = async () => {
return {
form: await superValidate(valibot(registrationFormSchema))
}
}
export const actions = {
default: async ({ request, cookies }) => {
const form = await superValidate(request, valibot(registrationFormSchema))
if (!form.valid) {
return fail(400, { form })
}
const principal = await tokenVerifier.exchangeToken(form.data.token)
const user = await createUser(principal.user)
const session = await lucia.createSession(user.id, {})
const sessionCookie = lucia.createSessionCookie(session.id)
cookies.set(sessionCookie.name, sessionCookie.value, {
path: '/',
...sessionCookie.attributes
})
const authType = form.data.authType
const verifyEmail = form.data.verifyEmail
if (authType === 'passkey' && verifyEmail === 'code') {
redirect(302, verifyEmailCode)
} else if (authType === 'passkey' && verifyEmail === 'link') {
redirect(302, verifyEmailAwaitLink)
} else {
redirect(302, app)
}
}
} satisfies Actions