Skip to content

add authcontxt params to the DatabaseEvent object#1770

Merged
kushalpalesha merged 13 commits intomasterfrom
kp/rtdb
Mar 10, 2026
Merged

add authcontxt params to the DatabaseEvent object#1770
kushalpalesha merged 13 commits intomasterfrom
kp/rtdb

Conversation

@kushalpalesha
Copy link
Copy Markdown
Contributor

@kushalpalesha kushalpalesha commented Nov 13, 2025

Description

Add authctxt fields so users can access them in their v2 functions.

Code sample

// The Cloud Functions for Firebase SDK to setup triggers and logging.
const {onValueWritten} = require("firebase-functions/v2/database");
const {logger} = require("firebase-functions");

exports.dbtrigger = onValueWritten("/messages/{pushId}/original", (event) => {
  // Log the AuthContext
  logger.log(`Auth type: ${event.authType}`);
  logger.log(`Auth ID: ${event.authId}`);
``
  // Grab the current value of what was written to the Realtime Database.
  const original = event.data.after.val();
  logger.log("Uppercasing", event.params.pushId, original);
  const uppercase = original.toUpperCase();

  // You must return a Promise when performing asynchronous tasks inside a function.
  return event.data.after.ref.parent.child("uppercase").set(uppercase);
});

@gemini-code-assist
Copy link
Copy Markdown
Contributor

Summary of Changes

Hello @kushalpalesha, 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 introduces significant enhancements to the DatabaseEvent object within Firebase Functions v2 for Realtime Database triggers. By adding authType and authId fields, it empowers developers to build more sophisticated and secure functions that can react differently based on the authentication context of the user or service that initiated a database change. This change improves the expressiveness and control available to function authors without altering existing functionality.

Highlights

  • Enhanced DatabaseEvent Interface: The DatabaseEvent interface has been updated to include optional authType and authId properties, providing authentication context for Realtime Database events.
  • User Access to Auth Context: Developers can now access the type of principal (authType) and its unique identifier (authId) directly within their Firebase Realtime Database functions (e.g., onValueWritten).
  • New Test Coverage: A new test case has been added to database.spec.ts to ensure that the authId and authType are correctly passed into the DatabaseEvent object for onValueWritten triggers.
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
Contributor

@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 adds authId and authType to the DatabaseEvent interface, allowing developers to access user authentication information in Realtime Database triggers. The changes are correct and include an initial test for onValueWritten. My feedback focuses on improving test coverage and robustness to ensure high quality for this new feature.

@kushalpalesha kushalpalesha requested a review from taeold November 13, 2025 20:37
@kushalpalesha kushalpalesha marked this pull request as draft November 19, 2025 20:06
@kushalpalesha kushalpalesha marked this pull request as ready for review March 3, 2026 17:09
@kushalpalesha kushalpalesha requested review from shettyvarun268 and removed request for taeold March 3, 2026 17:11
@kushalpalesha
Copy link
Copy Markdown
Contributor Author

/gemini review

Copy link
Copy Markdown
Contributor

@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 adds authentication context fields (authType and authId) to the DatabaseEvent object for v2 database functions, allowing developers to access this information in their function triggers. The changes are well-implemented, including updates to the necessary interfaces, the addition of a new AuthType type, and corresponding tests to validate the new functionality. My feedback focuses on improving the readability and type-safety of the event transformation logic by refactoring to use modern JavaScript/TypeScript features like object destructuring, which avoids type casting and property deletion.

@kushalpalesha
Copy link
Copy Markdown
Contributor Author

/gemini review

Copy link
Copy Markdown
Contributor

@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 adds authentication context (authId and authType) to Realtime Database events in v2 functions. The changes are functionally correct and include corresponding tests. My feedback focuses on improving the implementation's clarity and maintainability by refactoring the event creation logic to be more idiomatic and type-safe. I've also pointed out some test code duplication that could be refactored for better maintainability.

@kushalpalesha
Copy link
Copy Markdown
Contributor Author

/gemini review

Copy link
Copy Markdown
Contributor

@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 adds authId and authType to the DatabaseEvent for v2 database triggers, along with corresponding tests. The changes are logical and the test refactoring to reduce duplication is a good improvement. I've provided a few suggestions to enhance code clarity and type safety, including one for better typing in the new tests and a recommendation to refactor an object creation pattern to be more robust.

...RAW_RTDB_EVENT,
};

const func = (handler as any)("foo/bar", (event: any) => {
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.

medium

For better type safety and readability in this test, you can provide a more specific type for the event parameter instead of any.

Suggested change
const func = (handler as any)("foo/bar", (event: any) => {
const func = (handler as any)("foo/bar", (event: database.DatabaseEvent<any>) => {

Comment on lines +406 to +411
authType: event.authtype,
authId: event.authid,
} as any;
delete (databaseEvent as any).firebasedatabasehost;
delete (databaseEvent as any).authtype;
delete (databaseEvent as any).authid;
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.

medium

This pattern of spreading the raw event, casting to any, and then deleting properties can be brittle and reduces type safety. It would be cleaner and more type-safe to use object destructuring to construct the event object. This would involve picking only the necessary properties from the raw event and renaming them as needed, avoiding the need for as any and delete.

For example:

const {
  firebasedatabasehost,
  authtype,
  authid,
  data, // to be replaced
  ...rest
} = event;

return {
  ...rest,
  firebaseDatabaseHost: firebasedatabasehost,
  authType: authtype,
  authId: authid,
  data: snapshot,
  params,
};

While this might be a larger refactoring that cannot be applied as a simple suggestion due to file boundaries, it's worth considering for improving code quality and maintainability.

Comment on lines +436 to +441
authType: event.authtype,
authId: event.authid,
} as any;
delete (databaseEvent as any).firebasedatabasehost;
delete (databaseEvent as any).authtype;
delete (databaseEvent as any).authid;
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.

medium

Similar to the other function, this pattern of using ...event, as any, and delete could be improved. Using object destructuring would lead to cleaner, more type-safe code.

For example:

const {
  firebasedatabasehost,
  authtype,
  authid,
  data, // to be replaced
  ...rest
} = event;

return {
  ...rest,
  firebaseDatabaseHost: firebasedatabasehost,
  authType: authtype,
  authId: authid,
  data: { before, after },
  params,
};

Copy link
Copy Markdown
Contributor

@shettyvarun268 shettyvarun268 left a comment

Choose a reason for hiding this comment

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

Put in a couple of comments at like 406 and 436 which I think might break stuff(unless I am missing something).

data: snapshot,
params,
};
authType: event.authtype,
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.

Since authtype might be undefined on legacy events (or older versions), but DatabaseEvent.authType is typed as a required field, should we provide a default fallback here to ensure runtime safety? Something like authType: event.authtype ?? "unknown" .

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

done!

},
params,
};
authType: event.authtype,
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.

Same here. Please correct me if I'm missing something.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

done!

Copy link
Copy Markdown
Contributor

@shettyvarun268 shettyvarun268 left a comment

Choose a reason for hiding this comment

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

LGTM

@kushalpalesha kushalpalesha added this pull request to the merge queue Mar 10, 2026
Merged via the queue into master with commit 5b4c4a6 Mar 10, 2026
32 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.

3 participants