First of all, I've implemented Firebase authentication anonymous sign in without an issue when there's a sign in button. But when I put the sign in code under onCreate() to simulate automatic sign in, it does show sign in anonymously success and I'm able to read and write but the Console doesn't create the user. Why is it? Here's my code:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val config = AndroidApplicationConfiguration()
initialize(Main(this), config)
mAuth = FirebaseAuth.getInstance()
mAuth.signInAnonymously()
.addOnCompleteListener(this) { task ->
if (task.isSuccessful) {
// Sign in success, update UI with the signed-in user's information
Log.d(TAG, "signInAnonymously:success")
Toast.makeText(this, "Authentication succeeded.",
Toast.LENGTH_SHORT).show()
/* Assign 'user' */
user = mAuth.currentUser
// Write a message to the database
val database = FirebaseDatabase.getInstance()
val uidPath: String? = user?.uid
val uidRef = database.getReference("users")
val nameRef = database.getReference("users/$uidPath/name")
val emailRef = database.getReference("users/$uidPath/email")
val authProviderRef = database.getReference("users/$uidPath/authProvider")
uidRef.setValue("${user?.uid}")
nameRef.setValue("${user?.displayName}")
emailRef.setValue("${user?.email}")
user?.providerData?.forEach {
authProviderRef.setValue(it.providerId)
}
} else {
// If sign in fails, display a message to the user.
Log.w(TAG, "signInAnonymously:failure", task.exception)
Toast.makeText(this, "Authentication failed.",
Toast.LENGTH_SHORT).show()
}
}
}

