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.
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
}
}Solution
Fixed it by transforming the headers when passing to the routes
export const getHeaders = (headers: Headers) => {
const newHeaders = Object.fromEntries(headers as any)
const headerObject: Record<string, any> = {}
for (const key in newHeaders) {
const isAuthorization =
key.toLowerCase() === 'authorization' && newHeaders[key]?.Length > 7
if (isAuthorization) {
if (key !== 'cookie') {
headerObject[key] = newHeaders[key]
}
} else {
if (key !== 'authorization') {
headerObject[key] = newHeaders[key]
}
}
}
return headerObject as any as Headers
}export const getHeaders = (headers: Headers) => {
const newHeaders = Object.fromEntries(headers as any)
const headerObject: Record<string, any> = {}
for (const key in newHeaders) {
const isAuthorization =
key.toLowerCase() === 'authorization' && newHeaders[key]?.Length > 7
if (isAuthorization) {
if (key !== 'cookie') {
headerObject[key] = newHeaders[key]
}
} else {
if (key !== 'authorization') {
headerObject[key] = newHeaders[key]
}
}
}
return headerObject as any as Headers
}