Skip to content

fix: capture user_id in sightings and generate routes - #27

Merged
clates merged 2 commits into
mainfrom
fix/user-id-capture
Dec 21, 2025
Merged

fix: capture user_id in sightings and generate routes#27
clates merged 2 commits into
mainfrom
fix/user-id-capture

Conversation

@clates

@clates clates commented Dec 21, 2025

Copy link
Copy Markdown
Owner

Fixes an issue where the User ID was not being captured during book generation and sighting creation.
Switches from supabase.auth.getSession() to supabase.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

  • Updated app/api/books/generate/route.ts to use getUser().
  • Updated app/api/sightings/claim/route.ts to use getUser() and await cookies().
  • Added regression test __tests__/api/generate-auth.test.ts.
  • Updated jest.setup.js to polyfill Response.json for tests.

@vercel

vercel Bot commented Dec 21, 2025

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Review Updated (UTC)
lfl-booktracker Ready Ready Preview, Comment Dec 21, 2025 3:49pm

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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() with getUser() 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);

Copilot AI Dec 21, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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);

Copilot uses AI. Check for mistakes.
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"

Copilot AI Dec 21, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unused import NextResponse.

Suggested change
import { NextResponse } from "next/server"

Copilot uses AI. Check for mistakes.
return { insert: mockInsertSightings }
}
return { select: jest.fn() }
})

Copilot AI Dec 21, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Avoid automated semicolon insertion (90% of all statements in the enclosing function have an explicit semicolon).

Suggested change
})
});

Copilot uses AI. Check for mistakes.
@clates
clates merged commit 360acfd into main Dec 21, 2025
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants