
Gateway to Firebase: Authentication
Did you know that you can use Firebase simply for user management? Let me show you how to get started with just a few lines of code.
Actually, I’d like to limit the scope of the article to just be talking about Firebase for Android. Chances are that I got you here on false expectations if you are developing for the web or iOS. That’s how life is sometimes.
Don’t leave yet. I think that the article is on such a conceptual level that you can appreciate it anyway.
If you’ve ever developed an app where you implemented your own management and flow for username/password authentication, you probably know how much time it can consume. And dare I say, it was likely quite unimportant for the actual value proposition of your app.
User management is basic needs. It’s expected to be there, it’s expected to feel safe and it’s expected to be easy to sign up or in. But it’s hard work to implement all that. That’s not to mention also implementing support to sign in with Facebook, Google, Twitter et.al.

If you’re just about to implement user authentication for the first time, you might feel a bit concerned. I’m here to tell you that you shouldn’t. We’re about to get a robust authentication flow that is neither hard or time consuming to implement and maintain.
One of the things that I think that the Firebase team does really well, is to simplify things that should be easy to work with. Complex functionality does not need to be complicated to use.
Firebase UI
The Firebase UI library is doing a lot of the heavy lifting that we shouldn’t be wasting time on ourselves. But be warned, there’s a bit of magic involved and the design and login flow is only customizable to certain extent. That’s the trade-off for getting a robust and mature sign in/up flow with just a few of lines of code.
The library itself contains a lot of other features for making ListAdapters etc also. But we’ll only be talking about the authentication here and it’s as simple as just adding these few lines of code to your app to get a pretty looking authentication flow for your application.

Let’s walk through the snippet above.
- Upon creating the launch activity, we’ll check whether the user is already logged in
- If the user is not logged in, launch Firebase UI auth activity
- After the auth activity finishes we get the result in onActivityResult method with the result of the authentication
- If you have implemented onAuthStateChanged it will be called with a non-null user object if the login succeeded (this method is always called at least once when the listener is registered and user object is null if the user is not logged in).
And that’s all there is to it if you’re using the Firebase suite.
Now your user will have access to the Firebase realtime database according to the security rules that you’ve setup. If you haven’t already, please read the article below about Firebase security and rules.
Here’s the beautiful part
Even if you’re not using the Firebase database, you can still use the authentication library. Perfectly fine! Go ahead and submit the user’s token in an API request and verify it with Firebase Admin SDK on your custom backend implementation.
Applying it to your application
I like splash screens. Not to look at, but I use them almost as a rule without exception in the flow of activities for my applications. The splash screen (launch activity) doesn’t necessary need to display any UI components, but it’s nice to show a application icon or something so that the user get some kind of confirmation of that the application is starting.

The nice thing with splash screens is that you have a gateway to your application’s features and UI elements where you can ensure that once it has passed through, you know that the application is fulfilling the required initial state.
The most obvious responsibility of the splash screen is to handle the authentication logic. By preventing the user from even reaching the main application UI before logging in, there are far less checks that needs to be made before you present the user’s name in a menu, etc.
It is very likely that your application also contain more dependencies for a valid “ready state”. For example, the you might have user data stored in the realtime database that need to be retrieved before starting the app.
Articles in the same series
Finally
I hope you found this article useful and that it got you started with Firebase. This article is the second in a series of articles that will help you to get started with Firebase in the most simple way . I am hoping that it will be a gateway to more advanced features and usages.
Feel free to drop me a line if you need help with your project. There’s a contact form on my website.

