From ddb3ce214dd46a4441781d232dd72d42863d8e16 Mon Sep 17 00:00:00 2001 From: Chris Scott <99081550+chriswritescode-dev@users.noreply.github.com> Date: Sat, 24 Jan 2026 18:34:54 -0500 Subject: [PATCH] Add debug logging for auth troubleshooting --- backend/src/auth/index.ts | 2 +- backend/src/auth/middleware.ts | 12 ++++++++++++ backend/src/routes/auth.ts | 12 +++++++++++- shared/src/config/env.ts | 1 + 4 files changed, 25 insertions(+), 2 deletions(-) diff --git a/backend/src/auth/index.ts b/backend/src/auth/index.ts index b94dc2c0..c560c7ee 100644 --- a/backend/src/auth/index.ts +++ b/backend/src/auth/index.ts @@ -71,7 +71,7 @@ export function createAuth(db: Database) { }, advanced: { cookiePrefix: 'opencode', - useSecureCookies: ENV.SERVER.NODE_ENV === 'production', + useSecureCookies: ENV.AUTH.SECURE_COOKIES, }, }) diff --git a/backend/src/auth/middleware.ts b/backend/src/auth/middleware.ts index 70171ee5..1b25711e 100644 --- a/backend/src/auth/middleware.ts +++ b/backend/src/auth/middleware.ts @@ -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) } diff --git a/backend/src/routes/auth.ts b/backend/src/routes/auth.ts index 9e8421d3..e77c733e 100644 --- a/backend/src/routes/auth.ts +++ b/backend/src/routes/auth.ts @@ -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 diff --git a/shared/src/config/env.ts b/shared/src/config/env.ts index 8303c742..859f8d34 100644 --- a/shared/src/config/env.ts +++ b/shared/src/config/env.ts @@ -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),