Repository: firebase/firebase-tools (ts)

Issue:
Incomplete event context when creating a new user
<!-- DO NOT DELETE
validate_template=true
template_path=.github/ISSUE_TEMPLATE/bug_report.md
-->

<!--
Thank you for contributing to the Firebase community!

Think you found a bug?
=======================
Yeah, we're definitely not perfect! Please use this template and include a minimal repro when opening the issue. If you know how to solve the issue, please create a Pull Request, and we'd be happy to review it!

Have a feature request?
========================
Great, we love hearing how we can improve our products! However, do not use this template to submit a feature request. Please submit your feature requests to: https://firebase.google.com/support/contact/bugs-features/

Have a usage question?
=======================
We get lots of those and we love helping you, but GitHub is not the best place for them and they will be closed. Please take a look at the guide first: https://firebase.google.com/docs/cli/

If the official documentation doesn't help, try asking through our official support channel: https://firebase.google.com/support/

Additional locations to check for solutions or assistance from the community:
- Stack Overflow: https://stackoverflow.com/
- Firebase Slack Community: https://firebase.community/

*Please avoid duplicate posting across multiple channels!*
-->

### [REQUIRED] Environment info

<!-- What version of the Firebase CLI (`firebase-tools`) are you using? Note that your issue may already be fixed in the latest versions. The latest version can be found at https://github.com/firebase/firebase-tools/releases -->

<!-- Output of `firebase --version` -->

**firebase-tools:** 9.14.0

<!-- e.g. macOS, Windows, Ubuntu -->

**Platform:** Mac

### [REQUIRED] Test case

<!-- Provide a minimal, complete, and verifiable example (http://stackoverflow.com/help/mcve) -->

Creating a new user (either using google, magic link, or anonymous) which in its turn trigger firebase functions are such:

![image](https://user-images.githubusercontent.com/309389/123595740-5e37f280-d845-11eb-9e01-31326e9bc449.png)

Receives an incomplete auth event context as such:

![image](https://user-images.githubusercontent.com/309389/123595830-7a3b9400-d845-11eb-9784-c14f778ea7af.png)

The event is missing among other things the timestamp which I use to determine if the event is older than 10 minutes if so, stop to retry (fail-safe circuit for retriable functions).

### [REQUIRED] Steps to reproduce

Described above.

<!-- Provide the steps needed to reproduce the issue with the above test case. -->

### [REQUIRED] Expected behavior

The event context should return eventId and timestamp.

<!-- What is the expected behavior? -->

### [REQUIRED] Actual behavior

EventId and timestamp are missing.

<!-- Run the command with --debug flag, and include the logs below. -->
Debug log is attached.

Thanks
[log.txt](https://github.com/firebase/firebase-tools/files/6724251/log.txt)



Pull request:
Add missing properties to Auth functions context
<!--

Thank you for contributing to the Firebase community! Please fill out the form below.

Run the linter and test suite
==============================
Run `npm test` to make sure your changes compile properly and the tests all pass on your local machine. We've hooked up this repo with continuous integration to double check those things for you.

-->

### Description

Fixes #3536

### Scenarios Tested

I created a simple function and triggered it in the Emulators UI:

```js
const functions = require("firebase-functions");

exports.helloWorld = functions.auth.user().onCreate(async (user, ctx) => {
  console.log(JSON.stringify(ctx, undefined, 2));
});
```

The logs are correct:

```
>  {
>    "eventId": "d01d812f-03b5-4731-98c2-d539f4cb606f",
>    "eventType": "google.firebase.auth.user.create",
>    "resource": {
>      "service": "firebaseauth.googleapis.com",
>      "name": "projects/demo-project-12345"
>    },
>    "params": {},
>    "timestamp": "2021-06-30T13:45:49.161Z"
>  }
```

I also deployed a function to production to see the behavior and here is the `context` payload:

```
{
  "eventId":"2bcd79bb-dd0d-45b7-85f7-1a51b0b00e5a",
  "eventType":"google.firebase.auth.user.create",
  "notSupported":{},
  "resource":{
    "service":"firebaseauth.googleapis.com",
    "name":"projects/fir-dumpster"
  },
  "timestamp":"2021-07-01T16:51:17.729Z",
  "params":{}
} 
```


### Sample Commands

N/A

Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index c40d2936d..723dd83e1 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,2 +1,3 @@
 - Support loading Firebase Functions packaged as an ES module. (#3485)
-- Fixes Cloud Storage emulator missing classes (#3541)
+- Fixes Cloud Storage emulator missing classes. (#3541)
+- Add missing properties to Authentication functions `context` in the Functions emulator. (#3536)
diff --git a/src/emulator/auth/cloudFunctions.ts b/src/emulator/auth/cloudFunctions.ts
index 828f5a3ab..7804d012b 100644
--- a/src/emulator/auth/cloudFunctions.ts
+++ b/src/emulator/auth/cloudFunctions.ts
@@ -1,3 +1,6 @@
+import * as uuid from "uuid";
+
+import { EventContext } from "firebase-functions";
 import { Client } from "../../apiv2";
 
 import { EmulatorInfo, Emulators } from "../types";
@@ -7,6 +10,10 @@ import { UserInfo } from "./state";
 
 type AuthCloudFunctionAction = "create" | "delete";
 
+type CreateEvent = EventContext & {
+  data: UserInfoPayload;
+};
+
 export class AuthCloudFunction {
   private logger = EmulatorLogger.forEmulator(Emulators.AUTH);
   private functionsEmulatorInfo?: EmulatorInfo;
@@ -54,9 +61,16 @@ export class AuthCloudFunction {
   private createEventRequestBody(
     action: AuthCloudFunctionAction,
     userInfoPayload: UserInfoPayload
-  ): { eventType: string; data: UserInfoPayload } {
+  ): CreateEvent {
     return {
+      eventId: uuid.v4(),
       eventType: `providers/firebase.auth/eventTypes/user.${action}`,
+      resource: {
+        name: `projects/${this.projectId}`,
+        service: "firebaseauth.googleapis.com",
+      },
+      params: {},
+      timestamp: new Date().toISOString(),
       data: userInfoPayload,
     };
   }


Test patch:
diff --git a/src/test/emulators/cloudFunctions.spec.ts b/src/test/emulators/cloudFunctions.spec.ts
index ec4b59c85..83a9571a4 100644
--- a/src/test/emulators/cloudFunctions.spec.ts
+++ b/src/test/emulators/cloudFunctions.spec.ts
@@ -24,7 +24,14 @@ describe("cloudFunctions", () => {
     it("should make a request to the functions emulator", async () => {
       nock("http://1.1.1.1:4")
         .post("/functions/projects/project-foo/trigger_multicast", {
-          eventType: `providers/firebase.auth/eventTypes/user.create`,
+          eventId: /.*/,
+          eventType: "providers/firebase.auth/eventTypes/user.create",
+          resource: {
+            name: "projects/project-foo",
+            service: "firebaseauth.googleapis.com",
+          },
+          params: {},
+          timestamp: /.*/,
           data: { uid: "foobar", metadata: {}, customClaims: {} },
         })
         .reply(200, {});