Firebase - additionalInfo.isNewUser always returns "false"

Viewed 499

I create new users in Firebase with the Firebase Admin SDK by using Node.js.

This process works well, and I see in my Firebase console the new user who has of course never had a login date ("Angemeldet" in German): Console

Now I want to check on iOS-login of the user, whether it is his first login:

Auth.auth().signIn(withEmail: email, password: password) { (user, error) in

    if let error = error
    {
        self.errorText = error.localizedDescription
        self.shouldAnimate = false
        return
    }

    guard user != nil else { return }

    if let user = user, let additionalInfo = user.additionalUserInfo {
        print(additionalInfo.isNewUser) // Always "false"
        self.userViewModel.firstLogin()
    }
}

additionalInfo.isNewUser seems to be exactly what I need, but it always returns "false".

In Node.js I create users with this command:

  await admin.auth().createUser({
  uid: uid.toLowerCase(),
  email: uid.toLowerCase(),
  emailVerified: true,
  password: 'notSetYet',
  disabled: false
})

Is this a bug, or what am I doing wrong?

1 Answers

The isNewUser property essentially compares the values of creationDate and lastSignInDate and returns true if they are equal. Since your user was created on the server before you sign then in on the client, the timestamps will never be the same and isNewUser will always be false.

The two main options I can think of are to:

  1. Check for a certain range between lastSignInDate and creationDate where you consider the user to be "new".
  2. To store whether you've seen the user before in a cloud database (such as Firebase's Realtime Database or Cloud Firestore) from your Swift code, and check to trigger any "new user" behavior you need.
Related