|
1 | 1 | import type { FastifyRequest, FastifyReply, HookHandlerDoneFunction } from 'fastify'; |
2 | 2 | import { getLucia } from '../lib/lucia'; |
3 | | -import { getDbStatus, getSchema } from '../db'; |
| 3 | +import { getDbStatus, getSchema, getDb } from '../db'; |
| 4 | +import { eq } from 'drizzle-orm'; |
4 | 5 | import type { User, Session } from 'lucia'; |
5 | 6 |
|
6 | 7 | // Augment FastifyRequest to include user and session |
@@ -37,23 +38,80 @@ export async function authHook( |
37 | 38 | } |
38 | 39 |
|
39 | 40 | request.log.debug(`Auth hook: Found session ID: ${sessionId}`); |
40 | | - const { session, user } = await lucia.validateSession(sessionId); |
41 | | - |
42 | | - if (session && session.fresh) { |
43 | | - // Session was refreshed, send new cookie |
44 | | - request.log.debug(`Auth hook: Session ${sessionId} is fresh, sending new cookie`); |
45 | | - const sessionCookie = lucia.createSessionCookie(session.id); |
| 41 | + |
| 42 | + // Manual session validation to avoid Lucia SQL syntax issues |
| 43 | + const db = getDb(); |
| 44 | + const schema = getSchema(); |
| 45 | + const authSessionTable = schema.authSession; |
| 46 | + const authUserTable = schema.authUser; |
| 47 | + |
| 48 | + if (!authSessionTable || !authUserTable) { |
| 49 | + request.log.error('Auth tables not found in schema'); |
| 50 | + request.user = null; |
| 51 | + request.session = null; |
| 52 | + return; |
| 53 | + } |
| 54 | + |
| 55 | + // Query session and user manually |
| 56 | + const sessionResult = await db.select({ |
| 57 | + sessionId: authSessionTable.id, |
| 58 | + userId: authSessionTable.user_id, |
| 59 | + expiresAt: authSessionTable.expires_at, |
| 60 | + username: authUserTable.username, |
| 61 | + email: authUserTable.email, |
| 62 | + firstName: authUserTable.first_name, |
| 63 | + lastName: authUserTable.last_name, |
| 64 | + authType: authUserTable.auth_type, |
| 65 | + githubId: authUserTable.github_id |
| 66 | + }) |
| 67 | + .from(authSessionTable) |
| 68 | + .innerJoin(authUserTable, eq(authSessionTable.user_id, authUserTable.id)) |
| 69 | + .where(eq(authSessionTable.id, sessionId)) |
| 70 | + .limit(1); |
| 71 | + |
| 72 | + if (sessionResult.length === 0) { |
| 73 | + request.log.debug(`Auth hook: Session ${sessionId} not found`); |
| 74 | + const sessionCookie = lucia.createBlankSessionCookie(); |
46 | 75 | reply.setCookie(sessionCookie.name, sessionCookie.value, sessionCookie.attributes); |
| 76 | + request.user = null; |
| 77 | + request.session = null; |
| 78 | + return; |
47 | 79 | } |
48 | | - if (!session) { |
49 | | - // Invalid session, clear cookie |
50 | | - request.log.debug(`Auth hook: Session ${sessionId} is invalid, clearing cookie`); |
| 80 | + |
| 81 | + const sessionData = sessionResult[0]; |
| 82 | + |
| 83 | + // Check if session is expired |
| 84 | + if (sessionData.expiresAt < Date.now()) { |
| 85 | + request.log.debug(`Auth hook: Session ${sessionId} is expired`); |
| 86 | + // Delete expired session |
| 87 | + await db.delete(authSessionTable).where(eq(authSessionTable.id, sessionId)); |
51 | 88 | const sessionCookie = lucia.createBlankSessionCookie(); |
52 | 89 | reply.setCookie(sessionCookie.name, sessionCookie.value, sessionCookie.attributes); |
53 | | - } else { |
54 | | - request.log.debug(`Auth hook: Session ${sessionId} is valid for user ${user?.id}`); |
| 90 | + request.user = null; |
| 91 | + request.session = null; |
| 92 | + return; |
55 | 93 | } |
| 94 | + |
| 95 | + // Create user and session objects |
| 96 | + const user = { |
| 97 | + id: sessionData.userId, |
| 98 | + username: sessionData.username, |
| 99 | + email: sessionData.email, |
| 100 | + firstName: sessionData.firstName, |
| 101 | + lastName: sessionData.lastName, |
| 102 | + authType: sessionData.authType, |
| 103 | + githubId: sessionData.githubId |
| 104 | + }; |
| 105 | + |
| 106 | + const session = { |
| 107 | + id: sessionData.sessionId, |
| 108 | + userId: sessionData.userId, |
| 109 | + expiresAt: new Date(sessionData.expiresAt), |
| 110 | + fresh: false |
| 111 | + }; |
56 | 112 |
|
| 113 | + request.log.debug(`Auth hook: Session ${sessionId} is valid for user ${user.id}`); |
| 114 | + |
57 | 115 | request.user = user; |
58 | 116 | request.session = session; |
59 | 117 | // No explicit done() call, Fastify awaits the promise |
|
0 commit comments