Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions app/generation/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2479,13 +2479,14 @@ Tip: I automatically detect and install npm packages from your code imports (lik
'4. Open http://localhost:5173',
'system'
);
toast.success('Download started');
setLoading(false);
} else {
throw new Error(data.error);
addChatMessage(`Failed to create ZIP: ${data.error || 'unknown error'}`, 'system');
}
setLoading(false);
} catch (error: any) {
log(`Failed to create zip: ${error.message}`, 'error');
addChatMessage(`Failed to create ZIP: ${error.message}`, 'system');
} finally {
setLoading(false);
}
};
Expand Down
6 changes: 3 additions & 3 deletions lib/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { nanoid } from 'nanoid'
import { getDb } from '@/lib/mongo'

type User = { id: string; email: string; passwordHash: string; createdAt: number }
type Session = { id: string; userId: string; createdAt: number; expiresAt: number }
type Session = { id: string; userId: string; createdAt: number; expiresAt: Date }
type Project = { id: string; userId: string; sandboxId: string; url: string; name?: string; createdAt: number; updatedAt: number }

export async function getUserByEmail(email: string): Promise<User | undefined> {
Expand Down Expand Up @@ -30,7 +30,7 @@ export async function createSession(userId: string, ttlHours = 72): Promise<Sess
id: nanoid(),
userId,
createdAt: Date.now(),
expiresAt: Date.now() + ttlHours * 60 * 60 * 1000
expiresAt: new Date(Date.now() + ttlHours * 60 * 60 * 1000)
}
await db.collection<Session>('sessions').insertOne({ _id: session.id as any, ...session } as any)
return session
Expand All @@ -39,7 +39,7 @@ export async function createSession(userId: string, ttlHours = 72): Promise<Sess
export async function getSession(id: string): Promise<Session | undefined> {
const db = await getDb()
const doc = await db.collection<Session>('sessions').findOne({ id })
if (!doc || doc.expiresAt <= Date.now()) return undefined
if (!doc || doc.expiresAt.getTime() <= Date.now()) return undefined
return doc
}

Expand Down
2 changes: 1 addition & 1 deletion lib/mongo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export async function getDb(): Promise<Db> {

// Ensure indexes only once per application lifespan
if (!global._mongoIndexesCreated) {
// Ensure TTL index on 'expiresAt' to auto-delete expired sessions
await Promise.all([
db.collection('users').createIndex({ email: 1 }, { unique: true }),
db.collection('sessions').createIndex({ id: 1 }, { unique: true }),
Expand All @@ -36,4 +37,3 @@ export async function getDb(): Promise<Db> {

return db
}

Loading