DZone
Mobile Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Mobile Zone > How to Get More Sign-Ups With Firebase Authentication on Android

How to Get More Sign-Ups With Firebase Authentication on Android

Firebase Authentication for your Android app lets users try the app first, then automatically create an account through Google, boosting user signup and retention.

Nick Skelton user avatar by
Nick Skelton
·
Jul. 10, 17 · Mobile Zone · Tutorial
Like (1)
Save
Tweet
4.58K Views

Join the DZone community and get the full member experience.

Join For Free

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.

Image title

(1) Don’t hide the option to skip, it creates mistrust. (2) Don’t use an email/password option unless you have to, even my Mum has Facebook.

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.

Image title

Management console for Firebase Authentication.

The FirebaseAuth singleton provides you with a global reference to your logged in user. His email, display name, phone number, and profile picture may also be available depending on the federate identity he has 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 signup 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.

Firebase authentication app Android (robot)

Published at DZone with permission of Nick Skelton, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • How to Submit a Post to DZone
  • What Are Protocol Buffers?
  • A Brief History of JavaScript
  • DZone's Article Submission Guidelines

Comments

Mobile Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo