Skip to content

Commit b0d0474

Browse files
committed
fix: improve session handling in email login route with manual session creation and error logging
1 parent 5574547 commit b0d0474

File tree

1 file changed

+23
-2
lines changed

1 file changed

+23
-2
lines changed

services/backend/src/routes/auth/loginEmail.ts

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,29 @@ export default async function loginEmailRoute(fastify: FastifyInstance) {
5252
return reply.status(400).send({ error: 'Invalid email/username or password.' });
5353
}
5454

55-
const session = await getLucia().createSession(user.id, {});
56-
const sessionCookie = getLucia().createSessionCookie(session.id);
55+
// Check if user ID exists
56+
if (!user.id) {
57+
fastify.log.error('User ID is null or undefined:', user.id);
58+
return reply.status(500).send({ error: 'User ID not found.' });
59+
}
60+
61+
// Use manual session creation like in registration to avoid Lucia adapter issues
62+
const { generateId } = await import('lucia');
63+
const sessionId = generateId(40); // Generate session ID
64+
const expiresAt = Date.now() + 1000 * 60 * 60 * 24 * 30; // 30 days
65+
66+
const authSessionTable = schema.authSession;
67+
68+
// Insert session directly into database
69+
await (db as any).insert(authSessionTable).values({
70+
id: sessionId,
71+
user_id: user.id,
72+
expires_at: expiresAt
73+
});
74+
75+
fastify.log.info(`Session created successfully for user: ${user.id}`);
76+
77+
const sessionCookie = getLucia().createSessionCookie(sessionId);
5778

5879
reply.setCookie(sessionCookie.name, sessionCookie.value, sessionCookie.attributes);
5980
return reply.status(200).send({ message: 'Logged in successfully.' });

0 commit comments

Comments
 (0)