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()
}
}
}


The fact that you aren't writing the email and name in your database, isn't an issue because when you are trying to authenticate an user anonymous, the FirebaseUser object does not contain any values for those properties. So this is the reason why those properties are empty in your database. If you want to have them populated, you should link the anonymous account with a real account as explained here.
The reason that you get no user added in your Firebase Console is because you aren't passing to the Task's addOnCompleteListener method an activity and and a OnCompleteListener object you are only passing this, which is not correct. To solve this, please use the following code:
firebaseAuth!!.signInAnonymously()
.addOnCompleteListener(this, OnCompleteListener<AuthResult> { 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 = firebaseAuth!!.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()
}
})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With