export async function action({ request, context }: ActionFunctionArgs) {
if (request.method === 'OPTIONS') return new Response(null, { status: 204 })
const body = await request.clone().json() // { email, calledFromDesktop, ... }
// 2️⃣ Decide what URL you want in the magic link
const desktop = (body as { fromDesktop?: boolean }).fromDesktop === true
const magicLinkDomain = desktop ? 'http://import-magic://auth' : undefined
context.cloudflare.ctx.props['magicLinkDomain'] = magicLinkDomain
const auth = getAuth(context.cloudflare.env, context.cloudflare.ctx)
if (!auth) throw new Error('Auth could not start')
const res = await auth.handler(request)
if (request.url.includes('one-time-token/verify')) {
const resBody = await res.clone().json() // biome-ignore lint/suspicious/noExplicitAny: any to just build the schema
console.log({ resBody: resBody ?? {} })
// @ts-ignore
const token = resBody?.session?.token
console.log({ token })
if (token) {
res.headers.append(
'Set-Cookie',
`better-auth.session_token=${token}; Path=/; HttpOnly; SameSite=Lax`,
)
}
console.log(res.headers)
}
return res
}
export async function action({ request, context }: ActionFunctionArgs) {
if (request.method === 'OPTIONS') return new Response(null, { status: 204 })
const body = await request.clone().json() // { email, calledFromDesktop, ... }
// 2️⃣ Decide what URL you want in the magic link
const desktop = (body as { fromDesktop?: boolean }).fromDesktop === true
const magicLinkDomain = desktop ? 'http://import-magic://auth' : undefined
context.cloudflare.ctx.props['magicLinkDomain'] = magicLinkDomain
const auth = getAuth(context.cloudflare.env, context.cloudflare.ctx)
if (!auth) throw new Error('Auth could not start')
const res = await auth.handler(request)
if (request.url.includes('one-time-token/verify')) {
const resBody = await res.clone().json() // biome-ignore lint/suspicious/noExplicitAny: any to just build the schema
console.log({ resBody: resBody ?? {} })
// @ts-ignore
const token = resBody?.session?.token
console.log({ token })
if (token) {
res.headers.append(
'Set-Cookie',
`better-auth.session_token=${token}; Path=/; HttpOnly; SameSite=Lax`,
)
}
console.log(res.headers)
}
return res
}