Can't access stsTokenManager from firebase response of createUserWithEmailAndPassword

Viewed 977

I am using react native signup form that invokes firbase.auth().createUserWithEmailAndPassword(email, password); I am using in code as follows in a trycatch:

const result = await firebase
      .auth()
      .createUserWithEmailAndPassword(email, password);

    console.log(result.user.stsTokenManager);

If i console log result.user i see an stsTokenManager object that has info about the provided token but if i try to console log result.user.stsTokenManager i get undefined. Would be great if anyone knows the reason this is happening and why i cant access a specific object in the result.

3 Answers

For the below request, the variable credential is a UserCredential object.

const credential = await firebase
  .auth()
  .createUserWithEmailAndPassword(email, password);

A UserCredential is made up of:

{
  additionalUserInfo?: null | {
    isNewUser: boolean;
    profile: Object | null;
    providerId: string;
    username?: string | null
  };
  credential: AuthCredential | null;
  operationType?: string | null;
  user: User | null
}

Documentation: User and AuthCredential

As you can see from the documentation for User, the property stsTokenManager doesn't exist as it is an internal property.

This means you shouldn't be using it.

When you look at the source code of fireauth.AuthUser, the property is actually called stsTokenManager_, as defined here. It is accessed using user.getStsTokenManager(), as defined here.

So then, why does the console log it as stsTokenManager? This is because the fireauth.AuthUser.prototype.toJSON() method is called, which calls the fireauth.AuthUser.prototype.toPlainObject() method where it is exported as stsTokenManager, as defined here.

So you should be able to log the access token using either of the following:

console.log(result.user.getStsTokenManager().accessToken);

console.log(result.user.toPlainObject().stsTokenManager.accessToken);

I solved this by transforming response to JSON

result.user.toJSON().stsTokenManager.accessToken

Very straightforward I'd say, but this is how I resolved it regardless of whether firestore is not exposing it as a public property or method.

const result = await firebase
    .auth()
    .createUserWithEmailAndPassword(email, password);


    console.log(result.user['stsTokenManager']);
Related