Skip to content

Auth event v1 compat - #1953

Draft
shettyvarun268 wants to merge 5 commits into
masterfrom
shettyvarun268/auth-v1-compat
Draft

Auth event v1 compat#1953
shettyvarun268 wants to merge 5 commits into
masterfrom
shettyvarun268/auth-v1-compat

Conversation

@shettyvarun268

@shettyvarun268 shettyvarun268 commented Aug 28, 2026

Copy link
Copy Markdown
Contributor

relnote: none

Description

Adds V1Compat destructuring support to 2nd-Gen Firebase Authentication triggers (onUserCreated and onUserDeleted), bringing Auth to full feature and type parity with firestore, pubsub, storage, database, and remoteConfig.

Key changes:

  • Lazy Getters via addV1Compat: Attaches user (returning event.data) and context (returning getV1AuthContext(event)) via non-enumerable, memoized getters with zero performance/allocation overhead for standard v2 callers.
  • V1 Field Parity (getV1AuthContext): Maps legacy EventContext properties:
    • eventType: Translates v2 CloudEvent types to "providers/firebase.auth/eventTypes/user.create" and "user.delete".
    • resource: Sets service: "firebaseauth.googleapis.com" and normalizes name by stripping //identitytoolkit.googleapis.com/.
    • params: {}, eventId, timestamp.
  • TypeScript Contravariance Overloads: Declares overloads for both (event: AuthEvent<User> & V1Compat<"user", User>) and (event: AuthEvent<User>) to ensure user test suites passing vanilla POJO CloudEvent mocks to .run() compile without type friction.
  • Constants Extraction: Centralizes top-level constants (IDENTITY_TOOLKIT_SOURCE_PREFIX, FIREBASE_AUTH_SERVICE, USER_CREATED_EVENT, USER_DELETED_EVENT).

Scenarios Tested

  • Unit Tests (spec/v2/providers/identity.spec.ts):
    • Verified v1-compatible getters (event.user and all fields of event.context).
    • Verified destructuring assignment syntax: onUserCreated(({ user, context }) => ...).
    • Verified backward compatibility for plain mock POJOs passed to .run().
    • Ran full test suite: 988 passing tests (npm test).
  • Live GCP Deployment:
    • Deployed onUserCreated(({ user, context }) => ...) to test project varun-test-project-auth.
    • Triggered passwordless email link sign-up flow and verified in Cloud Run logs that user.uid, user.email, context.eventId, context.eventType, and context.resource were captured and logged correctly.

@gemini-code-assist gemini-code-assist Bot 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.

Code Review

This pull request introduces v1-compatible getters (user and context) to the v2 onUserCreated and onUserDeleted Identity triggers, enabling developers to use v1-style destructuring. It also adds corresponding unit tests to verify this compatibility. A critical runtime issue was identified where calling the trigger's .run() method in unit tests with a vanilla mock event will crash if destructuring is used, because .run is not wrapped with addV1Compat. It is recommended to wrap func.run to apply the compatibility layer.

Comment thread src/v2/providers/identity.ts
@shettyvarun268

Copy link
Copy Markdown
Contributor Author

/gemini review

@gemini-code-assist gemini-code-assist Bot 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.

Code Review

This pull request adds backward-compatible v1 getters (context and user) to the v2 Firebase Authentication triggers (onUserCreated and onUserDeleted) by wrapping the events using addV1Compat in both the main trigger execution and the .run() method. The feedback suggests adding a defensive check for the event parameter in func.run to prevent potential TypeErrors when developers call .run() with nullish values in unit tests.

Comment thread src/v2/providers/identity.ts
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