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 iOS app in Swift.
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 iOS app.
In Xcode, start with an existing app
or create a new Single View App.
And if iOS isn't in your jam, then check out
the "Getting Started" videos for Android or 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 added email and password authentication.
If you haven't added this yet, click Add a Provider and Select
email/password.
There are lots of other providers to explore,
but with today I'm just sticking with this one.
On this 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 iOS tab, I see "To get started with iOS,
you'll need to register it 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 the 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.
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 frameworks, like Angular.
There are specific for game SDKs for Unity and C++,
and so much more.
In fact, 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
about any of these SDKs, check out
the documentation and the Firebase YouTube channel,
both linked below.
Now, 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.
I admit that it's challenging to pin down
the relationship between Google Cloud and Firebase.
So my oversimplified explanation is
that Firebase is the 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 quickstart 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 select Do This Later.
Some Firebase products depend on analytics, like A/B testing.
So if you decide to incorporate other features,
you probably want to enable this.
Again, it's up to you.
When you click on the Project settings,
notice the settings are identical to
the quickstart Cloud 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 iOS app,
select Add App, iOS.
This gives you a window with a series of steps to follow.
Keep in mind that the bundle ID you choose needs to match
the bundle ID of your iOS app.
The app nickname is totally up to you.
I won't provide an App Store ID, since I don't have one yet.
But if you do have one, feel free to add it here
before creating the app.
I'm now prompted to download a GoogleService-Info.plist file.
This file informs my iOS app about my Firebase app,
so I can interface with Firebase products.
Once downloaded, add this to your iOS project.
Click Next in the console, and it gives the next step.
Add the desired pods.
So I will be using CocoaPods to add the required dependencies,
but you can add the required code without CocoaPods.
Check out the guide linked below to find out
how to install the SDKs without CocoaPods.
OK.
Initialize a new pod file and then open it.
Add the Firebase/Auth dependency and then Save the pod file.
Close Xcode to prevent strange behaviors that can rarely
happen when running CocoaPods while the project is open.
Then run Pod Install.
Open the created xcworkspace.
All right.
In the project, in AppDelegate, Import Firebase.
This smart import will only import the dependencies
you need, so you don't really have to import Firebase/Auth
specifically, for example.
In the didFinishLaunchingWithOptions
function, initialize the Firebase app
using FirebaseApp.configure.
This initializes the Firebase app using the settings provided
by the GoogleService-Info.plist file.
OK.
You can now use the Firebase/Auth SDK
to sign up, clock in, log out, and get
user info about an Identity Platform user.
Identity Platform gives you access to many different login
providers, including federated identity providers like Google,
Facebook, and Twitter.
You can enable several login methods for an app,
allowing your users to authenticate
the way that they prefer.
Some login methods may require additional steps,
which are listed in the console when you enable that login
method.
See the documentation below for information
on how to implement other login providers.
For now, let's get back to implementing email and password
login.
Back in the Cloud Console, create a fake user
with a test email and a test password.
Of course, you can also create a new user from the client,
but this is a good way to get us up and running really quickly.
See how to create a user in your iOS
app following the link below.
Back in your iOS app in Xcode, go to the viewDidLoad
for your ViewController.
Call Auth.auth.signIn, and pass the email and password you just
created in the console.
This function passes a completion handler
with two parameters--
an optional AuthDataResult and an optional Error.
AuthDataResult is the result of sign-in requests containing
both the user and some additional user info.
First, I check that AuthDataResult has a value.
And if it doesn't, log the error.
Otherwise, I can do whatever I need to do with the user.
Right now, I'll just log that user's UID.
Now, of course, 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 SDKs.
The related documentation is linked below.
But you also have the option to use the FirebaseAuthUI module.
This open-source UI was created by auth specialists at Google,
includes all of the views you would
need to create an account, log in, log out, and reset
password, which you can incorporate
in just a few lines of code.
And you can override the ViewController classes
to change the appearance of your login.
Whether you make your own UI or use Firebase UI,
you will very likely need a way to know the auth status
before programming any app actions.
For example, you're going to want
to check if there's a user logged in
before writing to the database.
That's where Firebase Auth Listener comes into play.
Let's look at how to implement it.
I create a function I'm calling listenForAuthChanges.
In this function, I call Auth.auth.addSta
teChangeListener.
This function passes a closure with an optional user
and optional error.
Handle the error if one is found,
then do what you need with this user,
like assign the value of a class's user
to the logged in user.
For the purpose of this example, I'll just log the user email.
Then I'm actually going to log out,
so you can see the StateChangeListener closure
be triggered again.
I put the code to log out in the separate function here called
logOut.
I run the app, and the console logs
show that the user is locked in and then logged out.
Congratulations.
You just added Identity Platform to an iOS app.
Ready to see what else Identity Platform can do?
Check out the Firebase Auth iOS guides, Auth quickstart code,
and the Firebase YouTube channel.
And be sure to subscribe to the Google Cloud Platform channel,
so you can be the first to know about new videos in this
"Who's There" series.
Thanks for watching.
And I'll see you on the next episode.
[MUSIC PLAYING]