Add debug logging for auth troubleshooting

This commit is contained in:
Chris Scott committed 2026-01-24 18:34:54 -05:00
1 parent ae09fa8492
commit ddb3ce214d
4 files changed
+25 -2

No files matched your search

+1 -1
View File
@@ -71,7 +71,7 @@ export function createAuth(db: Database) {
},
advanced: {
cookiePrefix: 'opencode',
useSecureCookies: ENV.SERVER.NODE_ENV === 'production',
useSecureCookies: ENV.AUTH.SECURE_COOKIES,
},
})
+12
View File
@@ -1,5 +1,6 @@
import { createMiddleware } from 'hono/factory'
import type { AuthInstance, Session } from './index'
import { logger } from '../utils/logger'
export function createAuthMiddleware(auth: AuthInstance) {
return createMiddleware<{
@@ -8,10 +9,21 @@ export function createAuthMiddleware(auth: AuthInstance) {
user: Session['user']
}
}>(async (c, next) => {
const cookies = c.req.header('cookie')
const origin = c.req.header('origin')
logger.debug(`Auth check - Path: ${c.req.path}, Origin: ${origin}, Has cookies: ${!!cookies}`)
if (cookies) {
const cookieNames = cookies.split(';').map(c => c.trim().split('=')[0]).join(', ')
logger.debug(`Cookie names: ${cookieNames}`)
}
const session = await auth.api.getSession({
headers: c.req.raw.headers,
})
logger.debug(`Session result: ${session ? 'found' : 'not found'}`)
if (!session) {
return c.json({ error: 'Unauthorized' }, 401)
}
+11 -1
View File
@@ -8,7 +8,17 @@ export function createAuthRoutes(auth: AuthInstance, _db: Database) {
const app = new Hono()
app.all('/*', async (c) => {
return auth.handler(c.req.raw)
const response = await auth.handler(c.req.raw)
const setCookie = response.headers.get('set-cookie')
if (c.req.path.includes('sign-in')) {
logger.info(`Sign-in response - Status: ${response.status}, Set-Cookie: ${setCookie ? 'present' : 'missing'}`)
if (setCookie) {
logger.debug(`Set-Cookie header: ${setCookie.substring(0, 100)}...`)
}
}
return response
})
return app
+1
View File
@@ -90,6 +90,7 @@ export const ENV = {
AUTH: {
SECRET: getEnvString('AUTH_SECRET', process.env.NODE_ENV === 'production' ? '' : generateDefaultSecret()),
TRUSTED_ORIGINS: getEnvString('AUTH_TRUSTED_ORIGINS', 'http://localhost:5173,http://localhost:5003'),
SECURE_COOKIES: getEnvBoolean('AUTH_SECURE_COOKIES', getEnvString('NODE_ENV', 'development') === 'production'),
ADMIN_EMAIL: process.env.ADMIN_EMAIL,
ADMIN_PASSWORD: process.env.ADMIN_PASSWORD,
ADMIN_PASSWORD_RESET: getEnvBoolean('ADMIN_PASSWORD_RESET', false),