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
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
}
6 replies
BABetter Auth
Created by Imtiaj on 4/12/2025 in #help
Hono on Cloudflare Workers with Better Auth always returning 401 Unauthorized
It seems it has something to do with cloudflare workers + headers issue. Tried same code on node it seemed to work perfectly
6 replies
BABetter Auth
Created by Imtiaj on 4/12/2025 in #help
Hono on Cloudflare Workers with Better Auth always returning 401 Unauthorized
//auth.route.ts
import { Hono } from 'hono'
import { auth } from 'lib/auth'

const authRoute = new Hono().on(['POST', 'GET'], '/*', (c) => {
return auth().handler(c.req.raw)
})
export default authRoute
//auth.route.ts
import { Hono } from 'hono'
import { auth } from 'lib/auth'

const authRoute = new Hono().on(['POST', 'GET'], '/*', (c) => {
return auth().handler(c.req.raw)
})
export default authRoute
//app.ts
import { OpenAPIHono } from '@hono/zod-openapi'
import { auth, HonoVariables } from 'lib/auth'
import authRoute from 'routes/auth.route'
import databaseRoute from 'routes/database.route'

import { cors } from 'hono/cors'
import { getHeaders } from 'lib/header'
const app = new OpenAPIHono<HonoVariables>()
.basePath('/api/v1')
.route('/auth', authRoute)
export default app
//app.ts
import { OpenAPIHono } from '@hono/zod-openapi'
import { auth, HonoVariables } from 'lib/auth'
import authRoute from 'routes/auth.route'
import databaseRoute from 'routes/database.route'

import { cors } from 'hono/cors'
import { getHeaders } from 'lib/header'
const app = new OpenAPIHono<HonoVariables>()
.basePath('/api/v1')
.route('/auth', authRoute)
export default app
when using
auth.api.getSession
auth.api.getSession
directly by sending the headers with
c.req.raw.headers
c.req.raw.headers
also is not returning the session
6 replies
CDCloudflare Developers
Created by ImDarkk on 5/2/2024 in #general-help
Connecting github is failing
if its connected to another account it won't work?
3 replies