Skip to content

fix(AuthContext): validate API response shape at runtime#9

Merged
alichherawalla merged 1 commit into
mainfrom
fix/auth-context-response-validation
Feb 18, 2026
Merged

fix(AuthContext): validate API response shape at runtime#9
alichherawalla merged 1 commit into
mainfrom
fix/auth-context-response-validation

Conversation

@alichherawalla
Copy link
Copy Markdown
Contributor

Summary

  • Addresses Gemini PR fix: auth security — bcrypt, JWT from env, rate limiting, zod, localStorage #8 medium-priority comment on AuthContext.tsx
  • Adds isAuthResponse() type guard that validates the server response has token: string and user: object before updating state
  • Replaces the blind as { token: string; user: User } TypeScript cast in both login and signup with a runtime check that throws if the shape is unexpected

Test plan

  • AuthContext.test.tsx — all 3 tests still pass (login stores, logout clears, rehydrate on mount)
  • Manual: login and signup flows work correctly in the browser

🤖 Generated with Claude Code

Addresses Gemini PR #8 medium-priority comment: the blind
`as { token: string; user: User }` cast is replaced with a
runtime type guard (isAuthResponse). If the server returns an
unexpected shape, an error is thrown before state is updated.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@gemini-code-assist
Copy link
Copy Markdown

Summary of Changes

Hello @alichherawalla, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request enhances the robustness and type safety of the authentication context by adding runtime validation for API responses. It ensures that data received from the login and signup endpoints conforms to the expected structure before being processed, preventing potential runtime errors due to malformed server responses.

Highlights

  • Runtime Type Validation: Introduced an isAuthResponse() type guard to validate the shape of authentication API responses, ensuring they contain token: string and user: object.
  • Improved Type Safety: Replaced TypeScript as casts in login and signup functions with a runtime check using the new type guard, throwing an error if the response format is unexpected.
  • Addressing Review Feedback: Implemented changes to address a medium-priority comment from a previous review on AuthContext.tsx.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Changelog
  • packages/web/src/AuthContext.tsx
    • Added AuthResponse interface and isAuthResponse type guard function.
    • Modified login function to use isAuthResponse for runtime validation of the API response.
    • Modified signup function to use isAuthResponse for runtime validation of the API response.
Activity
  • The author provided a detailed test plan, including unit tests for AuthContext.test.tsx and manual browser testing for login and signup flows.
  • The pull request was generated using Claude Code.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request improves runtime safety by validating the API response shape in AuthContext, replacing a TypeScript cast with a type guard. This is a solid improvement. I've added one comment to make the validation even more robust by checking the nested user object's structure, which will prevent potential runtime errors if the API response for the user is malformed.

function isAuthResponse(data: unknown): data is AuthResponse {
if (typeof data !== 'object' || data === null) return false;
const d = data as Record<string, unknown>;
return typeof d.token === 'string' && typeof d.user === 'object' && d.user !== null;
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

The isAuthResponse type guard is a great addition for runtime safety. However, it only checks that user is a non-null object. It doesn't validate the shape of the user object itself. If the API returns a user object with missing properties (e.g., no name), it could lead to runtime errors in other components that consume this context (like Header.tsx which accesses user.name).

To make this type guard more robust, I suggest also validating the properties of the user object. Based on its usage elsewhere, the User type seems to be { id: number; name: string; email: string; }.

Suggested change
return typeof d.token === 'string' && typeof d.user === 'object' && d.user !== null;
return (
typeof d.token === 'string' &&
typeof d.user === 'object' &&
d.user !== null &&
typeof (d.user as Record<string, unknown>).id === 'number' &&
typeof (d.user as Record<string, unknown>).name === 'string' &&
typeof (d.user as Record<string, unknown>).email === 'string'
);

@alichherawalla alichherawalla merged commit 881472e into main Feb 18, 2026
1 check 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.

1 participant