Authenticate with Firebase anonymously on Android

Manual approach

Firebase approach

Show me the code!

Get anonymous user:

private lateinit var mAuth: FirebaseAuth

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

mAuth = FirebaseAuth.getInstance()
//...
}
override fun onStart() {
super.onStart()
val currentUser = mAuth.currentUser
if (currentUser != null) {
updateUi(currentUser)
} else {
mAuth.signInAnonymously()
.addOnCompleteListener(this) { task ->
if (task.isSuccessful) {
val user = mAuth.currentUser
user?.let { updateUi(it) }
} else {
Toast.makeText(
this@MainActivity,
"Authentication failed.",
Toast.LENGTH_SHORT)
.show()
}
}
}
}

Link the actual user:

mAuth.getCurrentUser().linkWithCredential(credential)

Android Developer @Teckro.com

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store