-
-
Notifications
You must be signed in to change notification settings - Fork 7
fix: authentication, chat history toggle, and UI improvements #443
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -152,6 +152,23 @@ export async function saveChat(chat: Chat, userId: string): Promise<string | nul | |
| return null | ||
| } | ||
|
|
||
| // Ensure user exists in the database before saving chat | ||
| const { db } = await import('@/lib/db'); | ||
| const { users } = await import('@/lib/db/schema'); | ||
| const { eq } = await import('drizzle-orm'); | ||
|
|
||
| const dbUser = await db.query.users.findFirst({ | ||
| where: eq(users.id, effectiveUserId) | ||
| }); | ||
|
|
||
| if (!dbUser) { | ||
| await db.insert(users).values({ | ||
| id: effectiveUserId, | ||
| credits: 0, | ||
| tier: 'free' | ||
| }); | ||
| } | ||
|
|
||
|
Comment on lines
+155
to
+171
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Doing a sequence of Since this is server-side code, prefer static imports at module scope and make the insert idempotent. SuggestionRefactor to static imports and an idempotent insert:
import { db } from '@/lib/db'
import { users } from '@/lib/db/schema'
import { eq } from 'drizzle-orm'
await db
.insert(users)
.values({ id: effectiveUserId, credits: 0, tier: 'free' })
.onConflictDoNothing({ target: users.id })(Or catch unique-constraint errors.) Reply with "@CharlieHelps yes please" if you’d like me to add a commit with this refactor. |
||
| const { data, error } = await supabaseSaveChat(chat, effectiveUserId) | ||
|
|
||
| if (error) { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,6 @@ | ||
| /// <reference types="next" /> | ||
| /// <reference types="next/image-types/global" /> | ||
| import "./.next/dev/types/routes.d.ts"; | ||
| import "./.next/types/routes.d.ts"; | ||
|
|
||
| // NOTE: This file should not be edited | ||
| // see https://nextjs.org/docs/app/api-reference/config/typescript for more information. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The “create user if missing” flow can still throw a 500 (or fail the request) if two requests race and both attempt to insert the same
users.idconcurrently (e.g., multiple tabs hitting credits fetch on first login). This can reintroduce the auth/credits error you’re trying to fix.Consider making the insert idempotent (upsert / on-conflict-do-nothing) and then re-select, or handle unique-constraint violations explicitly.
Suggestion
Make user creation race-safe by using an idempotent insert (if supported by your Drizzle dialect) and then re-fetch:
onConflictDoNothing/onDuplicateKeyUpdatestyle APIs (dialect-dependent)Example (Postgres-style Drizzle):
Reply with "@CharlieHelps yes please" if you’d like me to add a commit with this change.