firebase-mock icon indicating copy to clipboard operation
firebase-mock copied to clipboard

"Auth token shows user authenticating in the future" if authTime not specified

Open amosyuen opened this issue 4 years ago • 2 comments

If authTime is not specified when creating a MockFirebaseUser, the code has a race condition when setting the date at https://github.com/dmurvihill/firebase-mock/blob/master/src/user.js#L170.

Added inline comments on the code from that file

function _tokenValidity(data) {
  const now = new Date(); // Storing "now"
  // If "data.authTime" is not defined, we set it to a new date, which will usually be later than "now"
  const authTime = data.authTime ?
    data.authTime : new Date(); 
 ...
  // If "data.authTime" is not defined, this will most likely trigger 
  } else if (now < authTime) {
    throw new Error(MockFirebaseUser.msg_tokenAuthedInTheFuture);

amosyuen avatar Jun 08 '20 05:06 amosyuen

I added this PR to try and resolve: https://github.com/dmurvihill/firebase-mock/pull/66

jfrumar-infinitusai avatar Jul 18 '20 00:07 jfrumar-infinitusai

Found a workaround until #66 gets merged:

    await firebaseAdmin.auth().createUser({
        // Your normal fields here
        uid,
        email,
        password,
        emailVerified: true,
        // Workaround
        _tokenValidity: {
            issuedAtTime: new Date(),
            authTime: new Date(1970, 1, 1),
            expirationTime: new Date(3000, 1, 1),
        },
    });

AudunWA avatar Sep 24 '20 08:09 AudunWA