How to get more sign ups with Firebase Authentication on Android
A quick guide to Android Firebase Authentication, signing up new users with their social media account in Kotlin.
At some stage in your app’s lifecycle, you are going to ask the user to sign up. Signing up is a big step for the user - it’s a bit of a commitment. So don’t ask until it’s absolutely necessary, you don’t want to scare them away before they’ve had a look at how awesome your app is.
When the time comes and the user is ready to sign up, making it as painless as possible ensures that you retain 100% of those users who decide to take the plunge.
The Firebase website provides great guides for setting your app up to use Firebase, I won’t replicate them here. What I will do is share some code to make implementation, especially in Kotlin a little faster.
OpenAuth has been around for many years, IMHO it is still quite painful despite the many iterations it has undergone. There are detailed guides for OpenAuth on Google/Facebook/Twitter. Unfortunately there is no way to bury the authentication process in the communication layer of the app where it belongs, you must put the code into your Activity. If you have any fans of Uncle Bob in your team, untidy code will send them into a temper tantrum, and we don’t want that. A compromise is to abstract the code into a series of parent classes and at least separate the code.
class LoginActivity : LoginGoogleActivity() {}abstract class LoginGoogleActivity : LoginTwitterActivity() {
//Google related OAuth Code
}abstract class LoginTwitterActivity : LoginFacebookActivity() {
//Twitter related OAuth Code
}abstract class LoginFacebookActivity : LoginOAuthActivity() {
//Facebook related OAuth Code
}abstract class LoginFacebookActivity : AppCompatActivity(){
abstract fun onError(errorMessage:String)
abstract fun authoriseWithFirebase(credential: AuthCredential)
}
Full code available here: https://github.com/shredderskelton/365salads
Once you have the Open Auth API working, Firebase steps in and takes over user management for you.
The FirebaseAuth singleton provides you with a global reference to your logged in user. His email, display name, phone number, profile picture may also be available depending on the federate identity they have used to sign in. This is all fairly elementary OAuth, let’s get to the cool stuff.
The most valuable feature of the Firebase Authentication, besides the automatic user creation and management, is the ability to let users try your app before they create an account. It’s not a new concept but it is a feature of custom built user management systems (ie. companies that have a backend team) that tends to be perpetually pushed to the bottom of the backlog, or worse, built into the client side code. Firebase achieves this using a neat concept called Anonymous users.
private fun updateUI() {
val currentUser = firebaseAuth.currentUser
if (currentUser == null) {
//first use
firebaseAuth.signInAnonymously().addOnCompleteListener {
updateUI()
}
return
}
if (currentUser.isAnonymous) {
updateUIanonymous()
} else {
updateUIloggedIn(currentUser)
}
}
When the user first opens the app, use FirebaseAuth to log the user in anonymously. Behind the scenes, this creates a User object in the Firebase backend. Now you have a unique identifier with which to record information about the user’s experience as you would with any other logged in user. Simply use :
currentUser.isAnonymous
wherever you need to check if a real user is logged in.
A classic scenario is a brand new user who collects items in the shopping cart, and only when he tries to checkout is he asked to create an account. He clicks on ‘Continue with Google’ and creates his account, but it would be a terrible experience if his shopping cart was suddenly empty. With Firebase, the credentials from Google can be merged with the initial anonymous user, essentially meaning that the unique user id doesn’t change:
firebaseAuth.currentUser.linkWithCredential(credential)
If you’ve ever worked on a project where the product owner insists on presenting the user with a sign up screen on first use of the app, anonymous users are a great alternative built in a ready to use in Firebase. Firebase also limits the creation of the anonymous users based on location and IP address so you don’t have to worry about flooding your User Management console.
In the next article, we'll delve into the Firebase Database.