Hi Alex here from rebeloper.com and welcome back to the channel. Now in this
episode we are going to talk about Auth States, how to login and sign out,
well basically automatically and how we can handle all of that so without
further ado let's jump right into it!
Okay, in the previous episode we have set up a lot of code and we left off in the
ViewWillAppear method inside our RootViewController. Where we have added our
ShouldOpenMailApp function. Basically this opens up the mail app so the user
can tap on and that link that we have just sent him or her. Now, if you recall
let me just show my device here. Let's build and run this we had quite an
ugly interface here because we don't have any text on the button nor the
navigation title, so we are going to fix this today and in the next episode we
are going to finally wrap it up. I know I promised you all of the wrap-up for
today's episode but hey we will have another tutorial tomorrow. What we are
going to do today is check for our AuthStateDidChangeListenerHandle. Okay,
first of all we need to go all the way up here and declare our AuthStateDidChangeListenerHandle.
That's a mouthful! Okay! So var and I'll just copy and this
because it's quite hard to add that it here and AuthStateDidChangeListenerHandle.
I will make this an optional. Okay, that is first things first and maybe I
don't need to add just declare it like so. Great! Now in the
viewWillAppear we want to listen for changes.
So: AuthStateDidChangeListenerHandle and let's
add a value here. Auth.auth( ).addStateDidChangeListener
The listener is our auth and our user so that is what we basically get back.
Now first of all we want to check if we do have our auth or if we do have our user
because those are optional or rather yet just check the user because we don't
really care about the auth. So: if let user == nil { } else { }. And if the user is
user os nil that means basically that we are not signed in. So: if let user == nil
then this means we are not signed in because we don't have a valid user.
Now let's go further: if let user = user, let email = user.email
then we do have a signed in user. Let me just
fix this so we don't need let here. The user is nil.
Let's print out here... and what do we want to do well first of all we want to
set our Service AuthState to be signed out. Let's do that!
So: Service.auth State = .signOut. Okay, and let's do the same in our
user. Let's print out and let's set our Service.authState = .sigedIn
Again this takes care of the UI logic, if we did set the state to be
signed in then we presenting and sign out we present something else. Now next
up we want to maybe set the navigation controllers title...
...to be lets say Profile because we are
sign out we don't really know the name of our user or rather yet the
email. And let's add in here self because we are in a closure. Let's do the same
for our logged in user : self.navigationController.title and this
time we are going to add in our email. We will present our email. Now one last
thing we need to do is handle the title of our button. Let's do that first
when the user is nil. So: self.signInSignOutButton.setTitle
And the should be "Sign In" because we are sign out we should be signing in. Okay and for
the state of .normal. And the same basically goes to our sign in sign out
button when we are logged in at this time we are going to set the title to
"Sign Out". Okay, and for the control state of .normal. Okay, let's build and run and
see how this looks like. And of course because we are signed in, then we need
to have... we are Signed Out we need to have a Sign in button here. And I don't
know why this profile does not show up I would need to take care of that later on.
Finally we are going to implement one last thing in this tutorial video and
that is to, well sign out but before we do that I just remember that we need to
remove the listener when the view will disappear and that is a pretty simple
just grabbed AuthStateDidChangeListenerHandle and just remove it.
Let's add that in here and because this is an optional let's guard unwrap it.
So: guard let AuthStateDidChangeListenerHandle = authStateDidChangeListenerHandle else { return } .
Okay, and just type in: Auth.auth( ).removeStateDidChangeListener
and just paste that in and there. Okay that is how we remove that listener.
Now let's go all the way down and let's implement our signOut.
First of all let's create a detailed text, so: detaildText
and that is "Signing out..." That is because the way this Hud is
presented and handled. Let's present our Hud here.
So:Hud.handle( hud: with: Hudinfo). So this will be our hud and our Hudinfo will be of
type and let's .show because we are just going to show it. And we say "Working" and
the detailText will be detailedText. Okay! Next up we want to grab our auth.
So: let auth = Auth.auth( ). There we go and now we want to sign out
but for that we need to add that in a do try-catch statement, so let's add that in there.
Now we want to try auth.signOut. Okay and if we do get an error then
we want to handle that. Okay, let's print out the error for now.
Okay... and that took care of all the errors and
warnings in Xcode, but before we leave this video as it is let's handle our Hud.
If we did sign out we want to set our Hud.handle and let's see here: hud, with: HudInfo
will be type error or rather yet .success here. Sorry about that!
.success and let's type in here "Success" detailText: "Successfully signed out".
Okay and for the error we want to show a Hud with an error the localized description.
There we go! hud, with: HudInfo, the type will be .error.
Text: "Error" and the string will be for the details localizedDescription.
Okay, I believe that is it, so let's build and run and try this out. Well basically
we cannot sign out but if we tap on sign in we can send a magic link. There we go!
We will test this out just show you my Quicktime Player. There we go.
We we will test this out later on in the next episode for now let's just know
that user is nil and we will get the whole around up in the next episode!
Hope you enjoyed this one, if you did go ahead and give it a thumbs up, subscribe to the channel
and hit that notification bell to get notified of the next and last
video where we will finally handle the incoming link from our email and as
usual I will see you in the next one!