how to set cookie after verifiying one-time-token.

hi, i have another desktop client (electron) that authenticates with my remix/react-router v7 server. (this is working fine) With the one-time-token, I am trying to redirect users to my website (frontend of the remix/react-router) so that they can upgrade their plan but they need to be logged on the website. The one-time-token is correctly being verified on redirect, but it doesn't set the cookie on the website correctly. I tried to modify the response headers but the token that is being returned from the one-time-token/verify is not the full token. It's only the first part of it anyone know a way around this to get the full token? I am trying to use an after hook to ctx.setNewSession but that is not workign either for me.
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
}
0 Replies
No replies yetBe the first to reply to this messageJoin

Did you find this page helpful?