kopia lustrzana https://github.com/cloudflare/wildebeest
39 wiersze
1.4 KiB
TypeScript
39 wiersze
1.4 KiB
TypeScript
// Screen after the first login to let the user configure the account (username
|
|
// especially)
|
|
import type { Env } from 'wildebeest/backend/src/types/env'
|
|
import type { ContextData } from 'wildebeest/backend/src/types/context'
|
|
import { createPerson } from 'wildebeest/backend/src/activitypub/actors'
|
|
|
|
export const onRequestPost: PagesFunction<Env, any, ContextData> = async ({ request, env }) => {
|
|
return handlePostRequest(request, env.DATABASE, env.userKEK)
|
|
}
|
|
|
|
// FIXME: move this behind Cloudflare Access. We can find the JWT in the cookies
|
|
export async function handlePostRequest(request: Request, db: D1Database, userKEK: string): Promise<Response> {
|
|
const url = new URL(request.url)
|
|
// TODO: email is in the JWT, should be parsed, verified and passed in the
|
|
// request context.
|
|
const email = url.searchParams.get('email') || ''
|
|
const domain = url.hostname
|
|
|
|
const formData = await request.formData()
|
|
const properties: Record<string, string> = {}
|
|
|
|
if (formData.has('username')) {
|
|
properties.preferredUsername = formData.get('username') || ''
|
|
}
|
|
|
|
if (formData.has('name')) {
|
|
properties.name = formData.get('name') || ''
|
|
}
|
|
|
|
await createPerson(domain, db, userKEK, email, properties)
|
|
|
|
if (!url.searchParams.has('redirect_uri')) {
|
|
return new Response('', { status: 400 })
|
|
}
|
|
|
|
const redirect_uri = decodeURIComponent(url.searchParams.get('redirect_uri') || '')
|
|
return Response.redirect(redirect_uri, 302)
|
|
}
|