Imtiaj
Imtiaj
Explore posts from servers
BABetter Auth
Created by Imtiaj on 4/12/2025 in #help
Hono on Cloudflare Workers with Better Auth always returning 401 Unauthorized
Using the better auth handler in the restricted endpoints like list sessions. It always returns unauthorized even after sign in with email. I'm using Scalar docs to test the api. The cookie is being set I've checked by logging it. But still it's returning unauthorized.
//auth.ts

import { betterAuth } from 'better-auth'
import { mongodbAdapter } from 'better-auth/adapters/mongodb'
import {
admin,
apiKey,
bearer,
jwt,
openAPI,
organization,
} from 'better-auth/plugins'
import DBClient from './db'

export const auth = () => {
const client = DBClient.getClient()
const db = client.db('__appflare__')
return betterAuth({
basePath: '/api/v1/auth',
database: mongodbAdapter(db),
emailAndPassword: {
enabled: true,
},
user: {
additionalFields: {
teamId: {
type: 'string',
required: false,
references: {
model: 'team',
field: 'id',
},
},
roles: {
type: 'string',
required: false,
},
tenantId: {
type: 'string',
required: false,
},
},
},
plugins: [
jwt(),
bearer(),
apiKey(),
organization(),
admin({
adminRoles: ['__superadmin__'],
}),
openAPI(),
],
})
}

type AuthType = ReturnType<typeof auth>['$Infer']
type UserType = AuthType['Session']['user'] | null
type SessionType = AuthType['Session']['session'] | null
export type HonoVariables = {
Variables: {
user: UserType
session: SessionType
}
}
//auth.ts

import { betterAuth } from 'better-auth'
import { mongodbAdapter } from 'better-auth/adapters/mongodb'
import {
admin,
apiKey,
bearer,
jwt,
openAPI,
organization,
} from 'better-auth/plugins'
import DBClient from './db'

export const auth = () => {
const client = DBClient.getClient()
const db = client.db('__appflare__')
return betterAuth({
basePath: '/api/v1/auth',
database: mongodbAdapter(db),
emailAndPassword: {
enabled: true,
},
user: {
additionalFields: {
teamId: {
type: 'string',
required: false,
references: {
model: 'team',
field: 'id',
},
},
roles: {
type: 'string',
required: false,
},
tenantId: {
type: 'string',
required: false,
},
},
},
plugins: [
jwt(),
bearer(),
apiKey(),
organization(),
admin({
adminRoles: ['__superadmin__'],
}),
openAPI(),
],
})
}

type AuthType = ReturnType<typeof auth>['$Infer']
type UserType = AuthType['Session']['user'] | null
type SessionType = AuthType['Session']['session'] | null
export type HonoVariables = {
Variables: {
user: UserType
session: SessionType
}
}
6 replies
CDCloudflare Developers
Created by Imtiaj on 5/24/2024 in #workers-help
When will cloudflare d1 replication be implemented?
It seems cloudflare d1 replication hasn't been implemented yet. Is it in progress or when can we expect it to be functional?
3 replies