Welcome back to class everyone Johnny be here with slopes does come.
And in this lesson we are going to loggin users.
All right so just to recap the last lesson we put in all of the code to create a user.
We added code to change add a change request and update the display name for our user as well as create
a new collection with a user document with the user id as the document ID for each document.
Now let's go ahead and work in the log and see.
All right.
This one's This one's quite a bit easier.
OK.
I've got to do here is under logon button tab.
Well first we've got to import firebase say import firebase.
Well first let's get our e-mail and passwords from the textfield we say guard that e-mail equal or e-mail
text that text and I'm going to chain the next one so that password equal password text that text or
else we will return.
OK.
So now we have our e-mail and our password so to log in we got to do is say off which is the base for
the base class for our authentication and then we're going say off to get the instance and we're going
to say sign in.
OK.
And of course we want the with e-mail.
OK.
So the e-mail that we're sending in with is e-mail and the password is password.
We're on our completion.
So when this press return and in our response we get access to our reference or our user and our error
code and in our code all we're going to do is we're going to check for the air Rissa if left error is
equal to error.
And if there is an error we'll just debug print that out and it will say error.
Signing in and we'll string interpolate hower error here.
Else we were done.
That's it.
All we've got to do is say self that this Miss animated is true and completion is no.
So we pressed the log in and we attempt a log in and if we are unsuccessful we'll print out the error.
If we are successful then we're done here with the log and in C and we just dismiss back to the main
visi.
And here we created a and b action for our create user tab and just realizing that we actually don't
need this flow we want to do actually or what we can do we could use the action there but it's easier
to just create a direct segue by holding control and dragging from our create user button to our create
user Visi and press show.
OK so now we can just click on the Create use a button and automatically segue back here for us.
All right.
So we have our log in the C code.
So let's pretend that we have used that that we have logged in we have access to the current user through
out the app and now.
And we also get some other cool stuff for free.
The other instance persists the user state for us.
So if we close the app and come back in we're still logged in.
We don't have to handle that ourselves with like user defaults or anything like that.
And when we log out the current user head back to nil and we're ready to sign back in again.
All right so we're able to log any user.
How do we keep track of the logged in state.
And so this is where we're going to get back into our main visi.
And I know we don't have a way to actually get to this screen yet but that's going to be here in just
a second.
All right we're back here in our main This is where we're going to keep track of whether or not we're
logged in.
And for that we create an off state did to change Lisner handle and get basically this handle is responsible
for listening for changes in the logged in status it is notified when a user is resuming a signed in
session or when they sign out or when they sign in for a signed outstate or when their access token
is refreshed.
OK.
So a little bit about that refreshed token.
This occurs under the following circumstances when the token expires when the user changes password
or when the user read authenticates.
So we are going to create our handle right here under variables.
I'm going to say private var handle and this is an off state did change Lisner handle.
OK.
And that's going to be an optional and we're going to implement that in our view will appear and I actually
want this.
I'm going to cut this and add it up here under load.
Just kind of keep things together and then I'm going to grab our view will disappear from here.
Cut it and move that appear under that view will appear.
There you go.
I like that better.
OK.
So what do we want to have happen when we open the app if we are not logged in then we need to transition
and segue to the log and see.
However if we are logged in then we just proceed as normal.
We will set our listener and everything will be the same as it was.
So here we're going to do it.
So here we're going to add our listeners so we're going to say handle is equal to off dot off dot and
state to change the listener.
And we got to push and Hendler here.
We have access to the off instance and the user.
And so here in the code what we can do is we can listen and we can do a check on the user and caesareans
if user is nil.
So if the user has no that means that we are not logged in.
OK else if we are logged in there and we're just going to set the listener and the app will perform
as it has in the past.
All right.
So but if we are not logged in I need the self here self if we are not logged in.
Then we need to instantiate the log in VC and presented.
OK.
So the way to do that is we're just going to reference it from the storyboard and then present it.
So when I say left storyboard will you buy this Daury board and angry to instantiate it and say name
the name of it is just main.
The bundle is now and then we're going to instantiate the log in VCs who want to say that laga and VC
equal storyboard dot the stain sheet view controller with identifier.
So here we need an identifier and I don't think we actually set that in storyboards so I'm going to
come over to mean that storyboard looked at our log in VC come up to our attributes and Specter actually
are things we think yeah.
So here understory board ID we are going to say log in VC and get a copy that come back in the main
You got swift and set the identifier here as log in visi.
And lastly it's as simple as things self dot present view controller and the view controller that we
want to present is Lague and visi animated is true and completion is now.
All right so now when the controller when we launch the app we do a check if the users know we are not
logged in then we instantiate and present the log in to sea.
Otherwise we just set the listener and proceed as normal.
So let's test this up.
I'm going to press play here.
So I forgot to change this here so now that we are doing a check for a log and used before we set the
listener.
Now it's not guaranteed that we are setting the listener and in the set listener function.
This is where we actually instantiate our thoughts to a listener which up at the top.
Here we set as a as implicitly on the wrapped value.
So here and view will disappear.
We need to do and if not check so I say if thoughts a listener is not equal to nil then we will remove
it.
That we try that again.
All right.
So here we go.
So we are not logged in.
So it immediately instantiated and presented this view controller very nice.
And so we can go to create user mode we should be able to open up something went wrong.
And I think it was because we deleted that IB action but did not remove it in the story board.
So I'm back up to story board here under it create user we need to see here if I right click on our
controller.
Yep and then we have this morning right here.
That was bad Johnny.
So when you delete from your IB our actions or outlet's from code you need to also remove them in storyboard.
So we're just going to click on that exit there and that should fix it.
So when I run that again I'm not logged in.
We present the log in to the sea.
We don't have a user created yet so we need to go to create user.
And there we go.
All right so let's test this up.
Going to put in e-mail I'm going to say J.
J.
Dot com password 1 2 3 4 5 6 and the user name is going to be Johnny B.
Goode.
I am going to press create user and let's see if this works.
All right.
So it looks like it works.
Let's say we have created a user no errors down here.
Let's check out our fire store authentication here.
So under users we should now see a user.
There we go Jeju dot com.
Very nice.
And in our database we should also have a new collection of users with a single document with a date
created and user name.
All right so that is pretty cool.
All right so now that we have created a user let's log that user in here.
So under e-mail we're going to enter that one that we just created J.J dot com and password is 1 2 3
4 5 6.
And I want to press logon and if it works then we should dismiss to our main Visi and then we set our
listener.
And so everything is now working perfectly.
Right.
Awesome.
So now we can log in.
How about signing out.
Just go ahead and do that now.
So that's going to be a pretty quick let's jump into the story board and here in our navigation bar
here in the main Visi we we're going to do a search for a bar button and I'm just going to drag in a
Barberton item over here and we're not going to do anything too fancy here.
I'm just going to change the title to log out and that'll be that.
And then we're going to download one of our assistant editor for the main VC by holding option and clicking
on Main Visi and we're going to create an action for the for the sign out here.
It's going it's like the barber Nighttime control and drag and changes to action and the name is going
to be log out tapped it connect and that looks good.
So I'm going to change back to our clothes our assistant editor and come back into our I mean does swift
and let's find that out right here.
So here what we do is we are going to attempt a log out and with firebase the sign out can throw a an
error an exception.
So we need to have a do try catch block.
So when we say let firebase off equal off top off and then we're going to say do.
And here we are going to try firebase off dot.
Sign out.
And if there is a problem we're going to catch it.
I say catch let sign out error as and this error and then we'll just debug print the Ericsson's debug
print.
I'm going to say error.
Finding out.
And then we'll do our string intercalation and the sign out error.
All right.
And that was good.
Let's see if this works.
Save it.
And just want to point out that now that we are signing out our state change listener is going to detect
that and it's going to run this logic again and it's going to say if user is nil which it will be now
that we signed out then it's going to relaunch the log and VC again get someone to run this.
And if you remember in the last lesson I mentioned that with firebase authentication we get for free.
The user state persistence.
So we don't have to manually keep track of whether a user is logged in or not which is super nice and
so because of that you'll see that we are automatically logged in right now.
All right so if I click on a log out then we should immediately launch the log in visi.
And because this was called and we ran the logic.
And yet so now we are back to our log in view.
All right so there we go.
We are able to create users were able to log in those users were able to log back out were able to access
the user anywhere in the app assuming we import firebase of course via the auth Singleton and Yesso.
Pretty great stuff.
Hope you guys are enjoying this so far.
And the next lesson we're going to add a new feature to our app being able to add comments to our posts.
I hope we're excited for that.
And I will see you in the next one.