Home > Enterprise >  React-Native-Firebase Spread auth().currentUser Object {...auth().currentUser}
React-Native-Firebase Spread auth().currentUser Object {...auth().currentUser}

Time:04-01

when I Console.Log(auth().currentUser) i get the following result:

Object {
  "displayName": "Test",
  "email": "[email protected]",
  "emailVerified": false,
  "isAnonymous": false,
  "metadata": Object {
    "creationTime": 1638471731312,
    "lastSignInTime": 1648765363821,
  },
  "phoneNumber": null,
  "photoURL": "www.google.com",
  "providerData": Array [
    Object {
      "displayName": "Test",
      "email": "[email protected]",
      "photoURL": "www.google.com",
      "providerId": "password",
      "uid": "[email protected]",
    },
  ],
  "providerId": "firebase",
  "refreshToken": "...",
  "tenantId": null,
  "uid": "...",
}

When I Console.Log({...auth().currentUser}) I expect the same result. Instead I get the following:

Object {
"_auth": FirebaseAuthModule {
  "_app": FirebaseApp {
    "_automaticDataCollectionEnabled": true,
    "_deleteApp": [Function bound deleteApp],
    "_deleted": false,
    "_initialized": true,
    "_name": "[DEFAULT]",
    "_nativeInitialized": true,
    "_options": Object {
      "apiKey": "...",
      "appId": "...",
      "clientId": "...",
      "databaseURL": "...",
      "messagingSenderId": "...",
      "projectId": "...",
      "storageBucket": "...",
    },
  },
  "_authResult": true,
  "_config": Object {
    "ModuleClass": [Function FirebaseAuthModule],
    "hasCustomUrlOrRegionSupport": false,
    "hasMultiAppSupport": true,
    "namespace": "auth",
    "nativeEvents": Array [
      "auth_state_changed",
      "auth_id_token_changed",
      "phone_auth_state_changed",
    ],
    "nativeModuleName": "RNFBAuthModule",
    "statics": Object {
      "AppleAuthProvider": [Function AppleAuthProvider],
      "EmailAuthProvider": [Function EmailAuthProvider],
      "FacebookAuthProvider": [Function FacebookAuthProvider],
      "GithubAuthProvider": [Function GithubAuthProvider],
      "GoogleAuthProvider": [Function GoogleAuthProvider],
      "OAuthProvider": [Function OAuthProvider],
      "PhoneAuthProvider": [Function PhoneAuthProvider],
      "PhoneAuthState": Object {
        "AUTO_VERIFIED": "verified",
        "AUTO_VERIFY_TIMEOUT": "timeout",
        "CODE_SENT": "sent",
        "ERROR": "error",
      },
      "TwitterAuthProvider": [Function TwitterAuthProvider],
    },
    "version": "14.7.0",
  },
  "_customUrlOrRegion": undefined,
  "_languageCode": undefined,
  "_nativeModule": Object {
    "APP_LANGUAGE": Object {},
    "APP_USER": Object {
      "[DEFAULT]": Object {
        "displayName": "Test",
        "email": "[email protected]",
        "emailVerified": false,
        "isAnonymous": false,
        "metadata": Object {
          "creationTime": 1638471731312,
          "lastSignInTime": 1648765363821,
        },
        "phoneNumber": null,
        "photoURL": "www.google.com",
        "providerData": Array [
          Object {
            "displayName": "Test",
            "email": "[email protected]",
            "photoURL": "www.google.com",
            "providerId": "password",
            "uid": "[email protected]",
          },
        ],
        "providerId": "firebase",
        "refreshToken": "...",
        "tenantId": null,
        "uid": "...",
      },
    },
    "addAuthStateListener": [Function anonymous],
    "addIdTokenListener": [Function anonymous],
    "applyActionCode": [Function anonymous],
    "checkActionCode": [Function anonymous],
    "confirmPasswordReset": [Function anonymous],
    "confirmationResultConfirm": [Function anonymous],
    "createUserWithEmailAndPassword": [Function anonymous],
    "delete": [Function anonymous],
    "fetchSignInMethodsForEmail": [Function anonymous],
    "getConstants": [Function anonymous],
    "getIdToken": [Function anonymous],
    "getIdTokenResult": [Function anonymous],
    "linkWithCredential": [Function anonymous],
    "reauthenticateWithCredential": [Function anonymous],
    "reload": [Function anonymous],
    "removeAuthStateListener": [Function anonymous],
    "removeIdTokenListener": [Function anonymous],
    "sendEmailVerification": [Function anonymous],
    "sendPasswordResetEmail": [Function anonymous],
    "sendSignInLinkToEmail": [Function anonymous],
    "setAppVerificationDisabledForTesting": [Function anonymous],
    "setLanguageCode": [Function anonymous],
    "setTenantId": [Function anonymous],
    "signInAnonymously": [Function anonymous],
    "signInWithCredential": [Function anonymous],
    "signInWithCustomToken": [Function anonymous],
    "signInWithEmailAndPassword": [Function anonymous],
    "signInWithEmailLink": [Function anonymous],
    "signInWithPhoneNumber": [Function anonymous],
    "signOut": [Function anonymous],
    "unlink": [Function anonymous],
    "updateEmail": [Function anonymous],
    "updatePassword": [Function anonymous],
    "updatePhoneNumber": [Function anonymous],
    "updateProfile": [Function anonymous],
    "useDeviceLanguage": [Function anonymous],
    "useEmulator": [Function anonymous],
    "useUserAccessGroup": [Function anonymous],
    "verifyBeforeUpdateEmail": [Function anonymous],
    "verifyPasswordResetCode": [Function anonymous],
    "verifyPhoneNumber": [Function anonymous],
  },
  "_settings": null,
  "_tenantId": null,
  "_user": Object {
    "displayName": "Test",
    "email": "[email protected]",
    "emailVerified": false,
    "isAnonymous": false,
    "metadata": Object {
      "creationTime": 1638471731312,
      "lastSignInTime": 1648765363821,
    },
    "phoneNumber": null,
    "photoURL": "www.google.com",
    "providerData": Array [
      Object {
        "displayName": "Test",
        "email": "[email protected]",
        "photoURL": "www.google.com",
        "providerId": "password",
        "uid": "[email protected]",
      },
    ],
    "providerId": "firebase",
    "refreshToken": "...",
    "tenantId": null,
    "uid": "...",
  },
},
"_user": Object {
  "displayName": "Test",
  "email": "[email protected]",
  "emailVerified": false,
  "isAnonymous": false,
  "metadata": Object {
    "creationTime": 1638471731312,
    "lastSignInTime": 1648765363821,
  },
  "phoneNumber": null,
  "photoURL": "www.google.com",
  "providerData": Array [
    Object {
      "displayName": "Test",
      "email": "[email protected]",
      "photoURL": "www.google.com",
      "providerId": "password",
      "uid": "[email protected]",
    },
  ],
  "providerId": "firebase",
  "refreshToken": "...",
  "tenantId": null,
  "uid": "...",
},

Is this the expected behaviour? Why is this happening? The spread operator should clone the object passed into it, shouldn't it?

Like this:

let obj1 = { foo: 'bar', x: 42 };
let obj2 = { foo: 'baz', y: 13 };

let clonedObj = { ...obj1 };
// Object { foo: "bar", x: 42 }
let mergedObj = { ...obj1, ...obj2 };
// Object { foo: "baz", x: 42, y: 13 }

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax

CodePudding user response:

The auth().currentUser is actually a User account object that has many properties and methods in it.

From MDN,

The spread operator copies own enumerable properties from a provided object onto a new object.

You can use .toJSON() to get a JSON-serializable representation of this user account object.

console.log({ ...auth().currentUser.toJSON() })
  • Related