Many apps need safe, fast and easy user authentication to save user personal data. To achieve this, you do not have to build your own authentication system and you do not want to care much about backend.
With Firebase Authentication you can write in some lines of code your whole authentication system for your app.
Firebase is a product by Google, which offers tools to built awesome features into your app. For more information about Firebase click here.
In this post, I write about Firebase Authentication and Firebase UI, and how easy you can implement it into your app (project is available on GitHub). So let's start!
First you have to implement Firebase into your app. To start, go to Firebase Console and click on "Create new Project".
Then enter your project name and country, where you live and click "Create Project".
After that, click on "Add Firebase to your Android app" and enter your package name and your SHA-1 key. To find out your SHA1-key, go to Android Studio, open the terminal and enter :
keytool -exportcert -list -v -alias androiddebugkey -keystore ~/.android/debug.keystore
The default password is "android". After clicking "Add app", download the .json File and add it to your Android Project in the app folder.
Okay great, now go back to Android Studio. We have to change some lines in our gradle files first.
Thats how my app level build.gradle file looks like:
Important lines you have to add in your app-gradle are lines 28-30 and lines 33.
Next lets watch in the other gradle File:
We have to make here two changes
- Adding the classpath of the Google Play Services
- Adding the maven Repository, which is required for the Firebase UI, which we are using.
Layout of the MainActivity
First lets build the User interface for our MainActivity. We just need a TextView and a Button. We do not have to care about the UI of the Login Screen, because Firebase UI have already implemented it. So we just code a TextView, which shows our Username and a Button, so we can log out.
Okay, no we have done the setup of Firebase and designed our layout for our MainActivity. So let's start with the actually implementation with authentication process.
But first, we have to go back to our Firebase Console and go to Authentication -> Sign In Method and activate Email/password and Google.
Back in Android Studio, we first declare our TextView, Button, a FirebaseAuth instance, FirebaseAuth Listener and a constant.
In onCreate(...), we then will declare our widgets and Auth objects. We see here, Firebase Auth seems to be an object of a singeton pattern.
Okay, here our magical happens. The AuthStateListener listens, if a user is already signed in. If this is the case, we set the name of our user in our UserNameTextView, else we start an Activity for Result and start with Firebase UI our Log in Screen, where the user can choose between Email/password Sign-in and Google Sign-In.
Last but not least we add an OnClickListener to our Button, which lets the User Sign out, if he clicks the Button.
Also we lets add the AutStateListener after onStart() and remove the AuthStateListener after onStop(), so it fits our Activity lifecycle.
(The full code is also available on GitHub)
Okay, that was all you have to do, to implement Firebase Authentication into your app.
With Firebase, you can implement very easy nice features into your app. It is very easy to understand, and you also do not have to write a lot of code.
If this post helped you, I would be happy if you would share it. Also follow me on Twitter, so you do not miss any of my other content.
Click here to come to one of my last posts.