fix: capture user_id in sightings and generate routes - #27
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
3ebf6b3 to
ab4b8c4
Compare
bb47a41 to
5327753
Compare
There was a problem hiding this comment.
Pull request overview
This PR fixes an authentication issue where user IDs were not being properly captured during book generation and sighting creation. The fix switches from using supabase.auth.getSession() to supabase.auth.getUser() for more reliable authentication checks and ensures consistent async cookie handling across all affected routes.
Key Changes:
- Replaced
getSession()withgetUser()in authentication flows for better reliability - Ensured uniform
await cookies()usage across affected route handlers - Added comprehensive regression tests to verify user ID capture works correctly for both authenticated and anonymous users
Reviewed changes
Copilot reviewed 7 out of 7 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
app/api/books/generate/route.ts |
Updated authentication to use getUser() instead of getSession() for reliable user ID capture |
app/api/sightings/claim/route.ts |
Updated authentication to use getUser() and ensured proper async cookie handling with await cookies() |
__tests__/app/api/sightings/claim/route.test.ts |
Added getUser() mock to maintain test compatibility with the updated authentication flow |
__tests__/app/api/books/generate/route.test.ts |
Added getUser() mock to support the updated authentication method |
__tests__/api/generate-auth.test.ts |
New regression test verifying that user ID is captured correctly for authenticated users and anonymous ID is used for unauthenticated users |
jest.setup.js |
Added static json() method polyfills to both Request and Response classes for test compatibility |
AGENTS.md |
Added package management documentation specifying yarn as the required tool |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
|
||
| beforeEach(() => { | ||
| jest.clearAllMocks(); | ||
| (cookies as jest.Mock).mockResolvedValue(mockCookies); |
There was a problem hiding this comment.
The cookies() mock should return a Promise to match the actual implementation which uses await cookies(). The mock is currently missing the Promise wrapper, which could cause runtime errors or improper test behavior. Consider changing line 59 to: (cookies as jest.Mock).mockResolvedValue(mockCookies);
| import { POST } from "@/app/api/books/generate/route" | ||
| import { createRouteHandlerClient } from "@supabase/auth-helpers-nextjs" | ||
| import { cookies } from "next/headers" | ||
| import { NextResponse } from "next/server" |
There was a problem hiding this comment.
Unused import NextResponse.
| import { NextResponse } from "next/server" |
| return { insert: mockInsertSightings } | ||
| } | ||
| return { select: jest.fn() } | ||
| }) |
There was a problem hiding this comment.
Avoid automated semicolon insertion (90% of all statements in the enclosing function have an explicit semicolon).
| }) | |
| }); |
Fixes an issue where the User ID was not being captured during book generation and sighting creation.
Switches from
supabase.auth.getSession()tosupabase.auth.getUser()for more reliable authentication checks and ensures uniform cookie handling.Resolves the issue where users were being treated as anonymous even when logged in.
Changes
app/api/books/generate/route.tsto usegetUser().app/api/sightings/claim/route.tsto usegetUser()andawait cookies().__tests__/api/generate-auth.test.ts.jest.setup.jsto polyfillResponse.jsonfor tests.