JEN PERSON: Welcome to "Who's [? There?", ?]
A show about Cloud Identity.
[MUSIC PLAYING]
In the last episode of "Who's [? There?", ?]
I talked about why Identity Platform is awesome,
and how to enable it in your GCP project.
Today I'm showing you how to integrate Identity Platform
into your Android app in Java.
If you haven't enabled Identity Platform yet,
I recommend you check out the last episode of "Who's There?"
before proceeding.
You also need an Android app.
In Android Studio, start with an existing app,
or create a new one.
And if Android doesn't happen to be your jam,
check out the Getting Started videos for iOS and web,
both linked below.
All right, with all the prerequisites out of the way,
let's get started.
In the Cloud console for my project,
I selected the Identity Platform tab.
I previously had an email and password authentication,
so if you haven't yet, click Add a Provider
and select Email Password.
There are lots of other providers to explore,
but today, I'm just going to stick with this one.
On the Providers page, you see a link
in the upper-right for Application Setup Details.
This pops up a window with brief getting started information.
When I click the Android tab, I see
to get started with Android, you'll
need to register and add Firebase configuration
to your app.
Learn More.
I click Get Started with Firebase, which takes me
to an entirely new console.
Some of the information is the same as the Cloud Console,
like project name and ID, but then
there are all these new tabs down
the side that say Develop, Quality, Analytics, and Grow--
and a big pop-up in the middle asking about data sharing.
If you've used Firebase before, this probably looks familiar,
but it's entirely possible you've never even heard
of Firebase, let alone used it.
So let's talk about what's going on here.
Firebase is a suite of products to help you build your app
and grow your user base.
It includes SDKs for Objective-C and Swift
for iOS, Java and Kotlin for Android, JavaScript for web,
as well as several web frameworks like Angular.
There are game-specific SDKs for Unity in C++, and so much more.
I can make a whole series of videos just on Firebase,
but fortunately, the Firebase team has already done that.
If you're interested in finding out more,
check out the documentation in the Firebase YouTube channel,
both linked below.
So in many ways, Firebase is the perfect complement
to Google Cloud.
Very broadly speaking, Google Cloud products
focus on server-side solutions like App Engine,
Compute Engine, Cloud Functions, Kubernetes Engine,
and Cloud Run.
And Firebase products focus on client-side solutions
like authentication, cloud messaging,
cloud storage for Firebase, and so on.
And I admit that it's challenging
to pin down the relationship between Google Cloud
and Firebase.
So for my oversimplified explanation
is that Firebase is a way to access some Google Cloud
Platform products from a client application.
Specifically, for Identity Platform,
this explanation works well.
The Firebase Auth SDKs enable you
to access Identity Platform functionality
from your client app.
With that in mind, let's head back to that Firebase Console.
The Firebase Console automatically
opened to our project.
Notice this UI indicates that Quick Start is already
a Firebase project.
When adding Identity Platform to a Google Cloud project,
a Firebase project is automatically created.
First, let's deal with this pop-up.
It's up to you whether you want to check off
to use the default settings for Google Analytics Data,
or just to select Do This Later.
Some Firebase products depend on analytics, like A/B testing,
so if you decide to incorporate other features,
you'll probably want to enable this.
Again, it's entirely up to you.
When you click on the Project Settings,
notice that the settings are identical to the Cloud Quick
Start project.
Firebase has basically added some more details
and functionality to the Cloud project,
similar to when you enable an API
or add software from the marketplace.
Since you want to add Identity Platform to an Android app,
select Add App, Android.
This gives you a window with a series of steps to follow.
Now, keep in mind when you pick the Android package name,
it must match the package name of your Android app.
The app nickname is totally up to you.
You can leave it blank if you prefer.
I don't provide the SHA-1 certificate,
since I'm not using any of the products for which it
is required.
But if you plan on implementing Google Sign In or Phone Number
Sign [? In and ?] [? Auth, ?] feel free to add the SHA-1 here
before creating [? an app. ?] You can also add it later.
Now you are prompted to download a Google Services JSON file.
This file informs your Android app about the Firebase app
so you can interface with Firebase products.
Once downloaded, add this to your Android project.
Click Next in the console, and it gives you the next step.
Add the gradle dependencies required to use Firebase.
In addition to the gradle files required
to build a Firebase app, you also
need specific gradle file to use Firebase Auth.
Add the following project level and app level dependencies.
You can see them here.
In your app in Android Studio, declare
an instance of Firebase Auth.
OK, now you can use the Firebase Auth
SDK to sign up, log in, log out, and get
user info about an Identity Platform user.
Identity Platform gives you access
to many different log in providers,
including federated identity providers like Google,
Facebook, and Twitter.
You can enable several log in methods for an app,
allowing your users to authenticate
the way that they prefer.
Some log in methods may require additional steps,
which are listed in the console when
you enable that log in method.
See the documentation below for information
on how to implement other log in providers.
For now, let's get back to implementing email and password
log in.
In the Cloud Console, create a fake user
with a test email and a test password.
Of course, you can create a new user from the client,
but this is a good way to get your app up
and running quickly.
See how to create a user in your Android
app following the link below.
In onStart, call a function called logIn.
This function calls the Firebase Auth method sign
in with email and password, passing
the email and password you created in the Cloud Console.
When the sign in function completes,
the onComplete listener is called,
passing the Auth result task.
If the task is successful, then you are signed in.
I've added a log [? and toast ?] to reflect this.
If the sign in fails, I display this as well--
just for our reference right now.
So it's clear that my example thus far is not a complete auth
solution.
You need a way for users to create accounts, handle
password resets, and log out.
Each of these functions needs some UI development as well.
If you prefer, you can write all this functionality yourself
using the Firebase guides as reference.
But you also have the option to use the Firebase Auth UI
module.
This open source UI was created by Auth specialists at Google.
It includes all the views you would
need to implement account creation,
log in, log out, and password reset functionality.
You can incorporate Auth UI in just a few lines of code.
Personally, whenever I start a new project,
I use Firebase UI so I can focus on the key features of my app,
while still protecting data through authentication.
But whether you make your own UI or use Firebase UI,
you will very likely need to know the auth status
before performing any app actions.
For example, you want to check if there is a user logged in
before writing to a database.
That's where getting the current user comes into play.
Let's look at how to implement that functionality.
To get the currently signed-in user,
call getCurrentUser on your Firebase Auth instance.
You may have noticed that this function was already called
in the onSuccess listener.
If the appearance of your UI depends upon
whether there is a current user, you can call getCurrentUser
in onStart.
I put the code to log out in this separate function called
logOut.
I'm going to call this function at the end
of the onSuccess listener if the log in was successful.
Of course, it's unusual to log in only to log out immediately,
but this is purely for the sake of example,
so you can see the log out function run.
OK, I build and run the app, and the console
logs show that the user has logged in, and then logged out.
Congratulations.
You just added Identity Platform to an Android app.
Ready to see what else Identity Platform can do?
Check out the Firebase Auth Android guides, Auth Quick
Start Code, and Firebase YouTube channel,
and subscribe to the Google Cloud Platform channel
so you can be the first to know about new videos in the
"Who's There?" series.
Thanks for watching, and I'll see you on a future episode.