0

How to create an user without sign in with Firebase Auth Android ?

It looks like the accepted solution here is a bit outdated : Firebase create user without sign in

Nowadays, is there any other way ?

DamienL
  • 577
  • 1
  • 5
  • 14

1 Answers1

0

Ok, answering myself after some research.

It looks like the situation is the same today. We still have to create an other instance of FirebaseAuth to avoid triggering auth state on the default FirebaseAuth instance you use.

But i suggest a simpler way to do it, without having to provide api key, application id...etc by hand by just using the FirebaseOptions of the default instance.

val firebaseDefaultApp = Firebase.auth.app
val signUpAppName = firebaseDefaultApp.name + "_signUp"

val signUpApp = try {
    FirebaseApp.initializeApp(
        context,
        firebaseDefaultApp.options,
        signUpAppName
    )
} catch (e: IllegalStateException) {
    // IllegalStateException is throw if an app with the same name has already been initialized.
    FirebaseApp.getInstance(signUpAppName)
}

// Here is the instance you can use to sign up without triggering auth state on the default Firebase.auth
val signUpFirebaseAuth = Firebase.auth(signUpApp)

How to use ?

signUpFirebaseAuth
            .createUserWithEmailAndPassword(email, password)
            .addOnSuccessListener {
                // Optional, you can send verification email here if you need

                // As soon as the sign up with sign in is over, we can sign out the current user
                firebaseAuthSignUp.signOut()
            }
            .addOnFailureListener {
                // Log
            }
DamienL
  • 577
  • 1
  • 5
  • 14
  • 1
    If you have an alternative to the approach suggested in answers to the [original question](https://stackoverflow.com/questions/37517208/firebase-kicks-out-current-user) I linked, please post an answer to *that* question rather than posting a new duplicate question with your own answer. – Frank van Puffelen Feb 26 '22 at 15:18
  • This question is more about javascript, not Android. I don't find that so clear having to scroll to a not validated and not upvoted answer for Android. – DamienL Feb 26 '22 at 17:18