bitangel84
bitangel84
BABetter Auth
Created by bitangel84 on 4/16/2025 in #help
Drizzle + PG + Hyperdrive + Cloudflare Workers doesn't work
Try my repo (d1 branch) works very well
24 replies
BABetter Auth
Created by bitangel84 on 4/16/2025 in #help
Drizzle + PG + Hyperdrive + Cloudflare Workers doesn't work
24 replies
BABetter Auth
Created by bitangel84 on 4/16/2025 in #help
Drizzle + PG + Hyperdrive + Cloudflare Workers doesn't work
@bekacru I published a minimum reproducible https://nuxt-better-auth-cf.bitsong.workers.dev/ REPO: https://github.com/angelorc/nuxt-better-auth-cf 1. If you try to login with Github, you will get an error The error is The script will never generate a response. 2. If you try to refresh, then you can see your session
24 replies
BABetter Auth
Created by bitangel84 on 4/16/2025 in #help
Drizzle + PG + Hyperdrive + Cloudflare Workers doesn't work
I still get issues
# SERVER_ERROR: Error: Cannot perform I/O on behalf of a different request. I/O objects (such as streams, request/response bodies, and others) created in the context of one request handler cannot be accessed from a different request's handler. This is a limitation of Cloudflare Workers which allows us to improve overall performance. (I/O type: Writable)
# SERVER_ERROR: Error: Cannot perform I/O on behalf of a different request. I/O objects (such as streams, request/response bodies, and others) created in the context of one request handler cannot be accessed from a different request's handler. This is a limitation of Cloudflare Workers which allows us to improve overall performance. (I/O type: Writable)
24 replies
BABetter Auth
Created by bitangel84 on 4/16/2025 in #help
Drizzle + PG + Hyperdrive + Cloudflare Workers doesn't work
let _auth: ReturnType<typeof betterAuth>
export function serverAuth() {
if (!_auth) {
_auth = betterAuth({
// database: drizzleAdapter(useDb(), {
// provider: 'pg',
// schema
// }),
database: {
db: useKyselyDb(),
type: 'postgres',
},
.........
} satisfies BetterAuthOptions)
}
return _auth
}
let _auth: ReturnType<typeof betterAuth>
export function serverAuth() {
if (!_auth) {
_auth = betterAuth({
// database: drizzleAdapter(useDb(), {
// provider: 'pg',
// schema
// }),
database: {
db: useKyselyDb(),
type: 'postgres',
},
.........
} satisfies BetterAuthOptions)
}
return _auth
}
24 replies
BABetter Auth
Created by bitangel84 on 4/16/2025 in #help
Drizzle + PG + Hyperdrive + Cloudflare Workers doesn't work
is there any example?
24 replies
BABetter Auth
Created by bitangel84 on 4/16/2025 in #help
Drizzle + PG + Hyperdrive + Cloudflare Workers doesn't work
export function usePostgres() {
// @ts-expect-error globalThis.__env__ is not defined
const hyperdrive = process.env.POSTGRES || globalThis.__env__?.POSTGRES || globalThis.POSTGRES as Hyperdrive | undefined
const dbUrl = hyperdrive?.connectionString || process.env.NUXT_POSTGRES_URL
if (!dbUrl) {
throw createError('Missing `POSTGRES` hyperdrive binding or `NUXT_POSTGRES_URL` env variable')
}

return postgres(dbUrl, {
ssl: !hyperdrive ? 'require' : undefined
})
}

export function useKyselyDb() {
return new Kysely<KyselyDatabase>({
dialect: new PostgresJSDialect({
postgres: usePostgres(),
}),
})
}

export const auth = betterAuth({
database: useKyselyDb(),
//... the rest of your config
});
export function usePostgres() {
// @ts-expect-error globalThis.__env__ is not defined
const hyperdrive = process.env.POSTGRES || globalThis.__env__?.POSTGRES || globalThis.POSTGRES as Hyperdrive | undefined
const dbUrl = hyperdrive?.connectionString || process.env.NUXT_POSTGRES_URL
if (!dbUrl) {
throw createError('Missing `POSTGRES` hyperdrive binding or `NUXT_POSTGRES_URL` env variable')
}

return postgres(dbUrl, {
ssl: !hyperdrive ? 'require' : undefined
})
}

export function useKyselyDb() {
return new Kysely<KyselyDatabase>({
dialect: new PostgresJSDialect({
postgres: usePostgres(),
}),
})
}

export const auth = betterAuth({
database: useKyselyDb(),
//... the rest of your config
});
24 replies
BABetter Auth
Created by bitangel84 on 4/16/2025 in #help
Drizzle + PG + Hyperdrive + Cloudflare Workers doesn't work
Failed to initialize database adapter
24 replies
BABetter Auth
Created by bitangel84 on 4/16/2025 in #help
Drizzle + PG + Hyperdrive + Cloudflare Workers doesn't work
at the end I don't need to modify neither
24 replies
BABetter Auth
Created by bitangel84 on 4/16/2025 in #help
Drizzle + PG + Hyperdrive + Cloudflare Workers doesn't work
I can try...
24 replies
BABetter Auth
Created by bitangel84 on 4/16/2025 in #help
Drizzle + PG + Hyperdrive + Cloudflare Workers doesn't work
Custom Plugin
// /signin/web3

async (ctx) => {
const data = await validateLogin(ctx.body)
if (data.error) {
return ctx.json(null, {
status: 400,
body: {
message: data.error,
status: '400',
},
})
}

const address = data.data!.addresses['chainx']

let wallet = await ctx.context.adapter.findOne<{
userId: string
chainType: string
chainName: string
coinType: number
address: string
pubkey: string
walletType: string
createdAt: Date
}>({
model: 'wallets',
where: [{
field: 'address',
value: address,
}, {
field: 'chainName',
value: 'chainx',
}]
})
...

---> here the db close the connection <---
// /signin/web3

async (ctx) => {
const data = await validateLogin(ctx.body)
if (data.error) {
return ctx.json(null, {
status: 400,
body: {
message: data.error,
status: '400',
},
})
}

const address = data.data!.addresses['chainx']

let wallet = await ctx.context.adapter.findOne<{
userId: string
chainType: string
chainName: string
coinType: number
address: string
pubkey: string
walletType: string
createdAt: Date
}>({
model: 'wallets',
where: [{
field: 'address',
value: address,
}, {
field: 'chainName',
value: 'chainx',
}]
})
...

---> here the db close the connection <---
Db
import type { Hyperdrive } from '@cloudflare/workers-types'
import { drizzle } from 'drizzle-orm/postgres-js';
import postgres from 'postgres'

export function usePostgres() {
// @ts-expect-error globalThis.__env__ is not defined
const hyperdrive = process.env.POSTGRES || globalThis.__env__?.POSTGRES || globalThis.POSTGRES as Hyperdrive | undefined
const dbUrl = hyperdrive?.connectionString || process.env.NUXT_POSTGRES_URL
if (!dbUrl) {
throw createError('Missing `POSTGRES` hyperdrive binding or `NUXT_POSTGRES_URL` env variable')
}

const queryClient = postgres(dbUrl, {
ssl: !hyperdrive ? 'require' : undefined
})

return drizzle({ client: queryClient });
}
import type { Hyperdrive } from '@cloudflare/workers-types'
import { drizzle } from 'drizzle-orm/postgres-js';
import postgres from 'postgres'

export function usePostgres() {
// @ts-expect-error globalThis.__env__ is not defined
const hyperdrive = process.env.POSTGRES || globalThis.__env__?.POSTGRES || globalThis.POSTGRES as Hyperdrive | undefined
const dbUrl = hyperdrive?.connectionString || process.env.NUXT_POSTGRES_URL
if (!dbUrl) {
throw createError('Missing `POSTGRES` hyperdrive binding or `NUXT_POSTGRES_URL` env variable')
}

const queryClient = postgres(dbUrl, {
ssl: !hyperdrive ? 'require' : undefined
})

return drizzle({ client: queryClient });
}
Config
import { betterAuth } from "better-auth";
import { drizzleAdapter } from "better-auth/adapters/drizzle";

export const auth = betterAuth({
database: drizzleAdapter(usePostgres(), {
provider: "pg",
}),
//... the rest of your config
});
import { betterAuth } from "better-auth";
import { drizzleAdapter } from "better-auth/adapters/drizzle";

export const auth = betterAuth({
database: drizzleAdapter(usePostgres(), {
provider: "pg",
}),
//... the rest of your config
});
24 replies
BABetter Auth
Created by bitangel84 on 4/16/2025 in #help
Drizzle + PG + Hyperdrive + Cloudflare Workers doesn't work
this is a migration from vercel to cf. The app use drizzle as adapter
24 replies
BABetter Auth
Created by bitangel84 on 4/16/2025 in #help
Drizzle + PG + Hyperdrive + Cloudflare Workers doesn't work
Drizzle
24 replies
BABetter Auth
Created by bitangel84 on 4/16/2025 in #help
Drizzle + PG + Hyperdrive + Cloudflare Workers doesn't work
I used pg and hyperdrive then I tried neon serverless... same issue
24 replies
BABetter Auth
Created by bitangel84 on 4/16/2025 in #help
Drizzle + PG + Hyperdrive + Cloudflare Workers doesn't work
The db close the connection
24 replies
BABetter Auth
Created by stu on 4/13/2025 in #help
Hanging timeout server-side requests using Cloudflare Hyperdrive
I also tried @neondatabase/serverless but I ended to get one more error
NeonDbError: Error connecting to database: Network connection lost.
NeonDbError: Error connecting to database: Network connection lost.
7 replies
BABetter Auth
Created by stu on 4/13/2025 in #help
Hanging timeout server-side requests using Cloudflare Hyperdrive
better auth and cloudflare hyperdrive doesn't work
7 replies
BABetter Auth
Created by bitangel84 on 3/1/2025 in #help
Update session on hooks
I've found the issue... 1. Use ctx.context.newSession 2. Use newSession.session.token to update the session this is the code that works
hooks: {
after: createAuthMiddleware(async (ctx) => {
if (ctx.path === '/callback/:id') {
if (ctx.params?.id === 'google') {
console.log('google callback, attaching wallet....')

const newSession = ctx.context.newSession
if (newSession?.session?.token === undefined) {
console.log('no session found')
return
}

console.log('session is', newSession)

await ctx.context.internalAdapter.updateSession(
newSession.session.token,
{
selectedWallet: 'test',
},
)

console.log('session updated')
}
}
}),
},
hooks: {
after: createAuthMiddleware(async (ctx) => {
if (ctx.path === '/callback/:id') {
if (ctx.params?.id === 'google') {
console.log('google callback, attaching wallet....')

const newSession = ctx.context.newSession
if (newSession?.session?.token === undefined) {
console.log('no session found')
return
}

console.log('session is', newSession)

await ctx.context.internalAdapter.updateSession(
newSession.session.token,
{
selectedWallet: 'test',
},
)

console.log('session updated')
}
}
}),
},
5 replies
BABetter Auth
Created by bitangel84 on 3/1/2025 in #help
Update session on hooks
newSession works, but I'm not able to update the session
hooks: {
after: createAuthMiddleware(async (ctx) => {
if (ctx.path === '/callback/:id') {
if (ctx.params?.id === 'google') {
console.log('google callback, attaching wallet....')

const newSession = ctx.context.newSession
if (newSession?.session?.id === undefined) {
console.log('no session found')
return
}

console.log('session is', newSession)

await ctx.context.internalAdapter.updateSession(
newSession.session.id,
{
selectedWallet: 'test',
},
)

console.log('session updated')
}
}
}),
},
hooks: {
after: createAuthMiddleware(async (ctx) => {
if (ctx.path === '/callback/:id') {
if (ctx.params?.id === 'google') {
console.log('google callback, attaching wallet....')

const newSession = ctx.context.newSession
if (newSession?.session?.id === undefined) {
console.log('no session found')
return
}

console.log('session is', newSession)

await ctx.context.internalAdapter.updateSession(
newSession.session.id,
{
selectedWallet: 'test',
},
)

console.log('session updated')
}
}
}),
},
5 replies
BABetter Auth
Created by ぱいなぷー on 2/28/2025 in #help
How to Retrieve DATABASE_URL in Cloudflare?
I think you should use environment variables, you can add it in workers -> settings
4 replies