Everyone welcome back Anjani be here with soap's dot com.
And in this lesson we are going to continue working on google log in with firebase and the last lesson
we got to the point where we actually logging in with Google and now we need to work on integrating
that with firebase.
So the way to start working with that is we need to go back to our firebase console and here under authentication
we are going to go to our seinen method and here under Google when you click on it and we are going
to say enable and let's give this a public facing names want to save firebase social aren't going to
save save.
So now Google is enabled.
And so next thing we need to do is jump back into X code and here in our view control it us with file.
Like I said we're going to have a method where we pass into a firebase method the authentication credential
for that platform's sign it it's wrong to call this phunk firebase log in and we're going to pass into
it a credential credential type off credential.
And then we're going to say off dot off dot sign in with a credential and we're going to call this credential
in completion we are going to give back a user and the optional error.
And so we will say if that error is equal to error then we will just print that AUSMIN see Debug.
Print and we'll just print out every dot localized description and then we'll return out of this function
and that's all we need for now.
But where do we call this function from.
So once in our app delegate that we are signed in with Google down here we are going to say we are going
to get our authentication credential from the user right here.
So we're going to say Well first we're going to get a reference to our view controllers and say guard
alert controller people and Id sign in that shared instance that I delegate as the controller and this
view controller is actually our view control it out with file here.
So here we set our UI delegate equal to self's Are you a delegate.
Is this view controller.
And so here in app delegate We are just grabbing reference to the delegate and casting it as a controller
and then we are saying well first weve got to do else return and then we need to get our authentications
when we say God for that authentication equal user datt authentication else return and then we need
to get our credential so we can say let credential equal Google or provider that credential with the
ID token which is the authentication that we just got that ID token and the access token is authentication
access token.
All right so now that we have our credential now we call controller dot firebase logon and we pass into
it the credential that we just got from the Google provider.
So now once we click on the Google sign in button and we are successful we are going to get a reference
to our view controller get the authentication which gives us the ID token and the access token which
gives us our credential and then we pass that credential into our firebase luggin function which then
takes that credential and uses it to sign into firebase.
And if there is not a user yet we will create a user.
All right so its going to check this out.
So when I run it in I'm going to have the Safari open here as well with our users.
And once we log in hopefully we get a user populated here and so when we click on seinen And let's see
if let's make sure we don't have any errors in X code.
Doesn't look like it.
So if I refresh your
oh sure enough.
Here we go.
We have a new user with our identifier.
Johnny Depp's that column and it shows the provider which is Google and the user id so that is pretty
cool.
All right so we are logged in.
How do we log out and sort of click on this button doesn't do anything right now.
All right so we got our Iby action for the log out tap.
So how we log out.
Is First we're going to get a reference to our fire base office and we say let firebase off equal off
that off and then to log out using firebase.
And that requires a do try catch block when to say do and then we're going to try a catch that sign
out error as in this error and we going to try to firebase off that dot sign out and here if we catch
an error we are going to just print that.
I'm going to say debug.
Print.
And we're going to say error.
Signing out.
And then we'll print out the sign out error.
And then firebase has a nice little handler that is a state change listener that can keep track of changes
in the author at authentications state.
So Harinder view did.
Let's go ahead and add that to say you did appear and we're going to add a state changelessness I will
say off that dot add state to changelessness.
Right.
And we got a listener here and we can say off and user.
And so this is going to be called anytime that someone logs out someone logs in refresh tokens you know
all that kind of stuff.
Any time that the state changes.
This is called.
So we're going to say if the user is equal to nil meaning no one is logged in then we are going to get
our user info label that we have here.
I'm going to say user label that text is equal to say no user logged in else.
If someone is logged in then we're going to just print out that user.
We're just going to print out the user id for the person that is logged in and of itself here.
Since we're in said closure and here I would say again self-taught user info labeled that text is equal
to say welcome user and then print out the user ID and if something's gone wrong and that doesn't exist
we'll just do an empty string.
All right.
So let's go ahead and test this out.
Right.
So we were already logged in firebase takes care of that for us once we have logged in.
And you remain logged in until you log out.
And so it says Welcome user and the ID.
So let's try this log out button and click and we have logged out and now says no user logged in.
However if I click on this again then nothing happens because we only logged out of the firebase instance
these are separate things.
Firebase and the Google are separate.
So we also need to be able to log out of the social platform.
So we're going to create a new function to handle that because we are going to need to be able to log
out of Facebook Google and Twitter.
So we're going to create a function here that will check on the users the firebase users information
and see which one that is currently logged in under which is the provider.
So if we look in our database again here it says provider here is Google.
And if we had Facebook one then the provider would be Facebook and Twitter one private provider would
be Twitter.
So we need to know which is the provider so that we can know how to which which social platform to log
out of.
So we're going to say fonk log out social And first we're going to get information for the users who
are going to say Gurd that user is equal to off dot dot.
Current User.
Else return.
All right we've got our user.
So what we need to look at is the provider data.
And that is in an dictionary of data.
So we need to say for info in user that provider data.
And now we're going to do a switch statement on the provider Heidi.
So when I say switch info that provider ID and we're going to have three cases that we take a look at
the Twitter Facebook and Google.
So case of Google or provider then we are going to log out.
But right now I'm just going to print say print Google and get the next case is going to be Twitter
off provider.
We want to hear off provider ID and we want the same for Google off provider.
I d There we go.
And here we're going to print
Twitter.
And then lastly we have a case for Facebook off provider ID and for that we will also print out just
Facebook and then needs to be exhaustless will have a default
which we will just break.
All right very good.
So now we are handling our three cases Google off Twitter off and Facebook off.
So these are this is whether or not we have logged into firebase using each of these.
So if we have logged in with Google provider what we need to do is we need to say Gee Id signed and
shared instance that sign out and then appear where we log out before we sign out with firebase who
will say log out.
Social Okay.
And depending on whichever social platform we have logged in with it will automatically log us out of
it through the use of this function here.
So is run this and test it out.
All right I'm going to log out we have logged out.
Both should have logged out both the firebase and Google so here it is Google which means that we got
into our site out block right here.
And so now if I click on seinen we should again yes go through the Google sign and process just like
Jollibee again.
And now we are back to being logged in we display our user info.
So that is working good.
I click log out then we are back to no use logged in and we can log back in again.
All right very good.
So congratulations you are now logging in with Google.
But you may be wondering do I have to use this ID sign in button.
And the answer is No you can use any button that you want and just to show you this quote and create
a button real quick.
So here I'm going to throw in a new button and put it right here going to constrain it.
Pin it 20 to the left 8 from the button and above it and 20 from the right change the height to 40 and
add four constraints we're going to change the text to log in Google and we're going to change text
color to white and change the background color to this or red color which if you want to use it it is
hecks color a four three three five.
And I say go ahead and create an action for this real quick so I can hold the option and click on our
controller to bring up the assistant editor and right here under the Google sign in Taft.
I'm going to control and drag and creating new IAB action and then call this one custom Google tapped
I'm going to connect close our assistant editor go into our view controller and hear all we need to
do is the same thing that we did for our GED sign in Boston Id sign in our shared instance that sign.
So if we were on this now we see our two buttons our Google sign in button as well as just a regular
old custom button and you can customize this button any any way how you want but if we click on it then
it should do the same thing.
So we go continue and we can log in with our Google account and boom.
All right so now we are logged in again.
All right.
So if you want to use the built in ID sign in but you can or you can create your own custom button.
All right so there you have it.
You are officially logging in with Google and then also using firebase to be able to have firebase users
using that Google logon.
All right so congratulations that is how you do it.
And in the next lesson we will start working on Facebook logon.
All right.
So I'll see you then.