firebase / firebase-tools

The Firebase Command Line Tools
MIT License
3.97k stars 915 forks source link

Auth emulator: send metadata timestamps as int instead of string #7310

Open valeriangalliat opened 2 weeks ago

valeriangalliat commented 2 weeks ago

Description

In the auth emulator sign in blocking function, send metadata.last_sign_in_time and metadata.creation_time as numbers and not strings so that firebase-functions can handle them.

This is already implemented for calling Cloud Functions associated with other auth events: https://github.com/firebase/firebase-tools/blob/d01da701554ac3c33786dd6486469293e0c93253/src/emulator/auth/cloudFunctions.ts#L77-L84

But it's missing for the sign in blocking function call.

This results in sending string timestamps in the metadata:

metadata: {
  last_sign_in_time: '1665001706543',
  creation_time: '1697150491509'
}

This is unsupported by the firebase-functions framework on the other side, that expects a number: https://github.com/firebase/firebase-functions/blob/9c818713db511895a33378859ab1b9f2eef99179/src/common/providers/identity.ts#L363-L366

In particular, the way it uses it: https://github.com/firebase/firebase-functions/blob/9c818713db511895a33378859ab1b9f2eef99179/src/common/providers/identity.ts#L516-L521

    const creationTime = metadata?.creation_time
      ? new Date(metadata.creation_time).toUTCString()
      : null;
    const lastSignInTime = metadata?.last_sign_in_time
      ? new Date(metadata.last_sign_in_time).toUTCString()
      : null;

This results in e.g.:

    new Date('1697150491509').toUTCString()
    => 'Invalid Date'

By sending a number, we make sure that the receiving end can process the timestamps.

Scenarios Tested