Hey everybody what's going on this is Caleb with Debb slopes dot com.
And in this video we're going to set our log in Visi to pop up when we need it to.
We're going to also set it up so that we can actually authenticate users using firebase send them up
and start adding users to our user base.
Very very cool.
Let's go ahead and pull up our X code project.
Nice.
Looks good.
This is where we left off last time.
And you know what I just realized.
Let's look at this function here.
It's giving us warnings saying user was defined but never used.
I don't know that we really need to use this because if we get a user that's great if we get an error
that's where this matters.
So I'm actually going to change how this works.
I just noticed that if error is not equal to nil Let's go ahead and call log and complete and return
and let's get rid of this guard lead we don't need that.
Otherwise if there is no error we'll call log and complete.
That's a much better way to do it.
Let's go ahead and update it so that it looks like that.
Very nice.
And now we're going to go into main storyboard.
We're going to move this back to our Tabar controller here so that we can actually instantiate this
from our app.
Delegate the app delegate is going to allow us to actually present this view controller as soon as the
authorization status changes.
If the current user suddenly becomes nil meaning we've logged out it's going to present that automatically.
Very very cool.
Before we do that I want you just to remember this controller has a storyboard ID of all the VC that's
going to be important in just a little bit.
So let's go ahead and let's set this up.
Go into your app delegate.
And what we're going to do is right beneath where we configure our firebase app we're going to go ahead
and we're going to just check to see if the current user is nil.
So go ahead and type if off off the current user is nil.
OK this is what we're going to do if there is no user logged in.
We're going to go ahead and present our log in visi.
So to begin first we need to create a constant that's going to hold our storyboards So go ahead and
type let storyboard equals UI storyboard and if you give it some parentheses there you can give it a
name right which the name is main storyboard and you can see that right there.
And the bundle that it comes from is bundle.
And we're going to use the main bundle.
OK that's how you access the main storyboard.
Very easy.
Now we're going to actually go ahead and go beneath this and let's create an instance of Auth. AVC.
So Type let off Visi and that's going to be equal to storyboard Dom instantiate view controller with
identifier.
This actually returns us a view controller and we can get the identifier by simply typing off visi.
Right that's the ID we gave it in the storyboard.
Very cool.
Now in order to actually get this to present up on the screen when user is nil we're going to modify
the window and we're going to set the window to be.
We're going to call a function on it called Make key and visible and I'll show you what that is in just
a second.
First go ahead and type window make key and visible.
And as soon as that's done you can look to see what it is.
You can see it's a function but let's go ahead and let's see where it is called.
Now it's a convenience function and it says most apps call this to show the main window and also make
it key.
Right.
That means it's our our most important view controller it's the key view controller or the key window
I mean.
Now what we can access through the window is the route view controller K the route view controller is
really important and it is basically going to allow us to call a view controller on top of the route
view controller k we can present it on top meaning whatever view controller is currently shown we can
present this on top from the delegate.
So go ahead and type present view controller to present of course is Auth. VC and the animation is true.
We want it to pop up nicely and we don't care what it does when it's complete.
OK.
Very simple but what this will do is it's going to check if the current user is Nele it's going to go
ahead and instantiate off visi.
It will make the window key and visible meaning it's visible and it's the key window meaning it's first
and foremost and it can do a lot of cool stuff.
Window root view controller we are accessing the view controller which is the currently presented one.
We're going to present Auth. VC on top of that.
So whether it is the tab controller whether it's the profile page the front page if we are suddenly
logged out it's going to present the authentication Visi which is really cool.
I think we should go build and run to try it.
I'm going to make sure I moved the initial view controller to the tab bar.
OK.
Cool.
So it's build and run.
And let's go look at our simulator to see how we did.
Let's see if it properly presents.
Look at that beautiful.
It worked.
So we're not logged in but we have set it so that when no one is logged in we can pop it up there and
it works beautifully.
That's amazing.
OK.
Very cool.
Let's go ahead and let's set up our log in the to be presented as soon as we tap on the e-mail button.
So let's open up the assistant editor here.
Let's select our view controller and we're going to get rid of this boilerplate junk and let's go ahead
and to begin we need to actually create an outlet for this button.
So go ahead and just drop it there.
Right click and drag and drop.
Then go to action.
And we're going to call sign in with e-mail button was pressed.
It's really long but it's definitely descriptive.
And you know what we're not going to use these buttons yet but right click and drag and we'll just say
for an action here Google sign in button was pressed and put another one here.
Make sure it's in action.
Facebook whoops Facebook sign button was pressed.
All right.
And we're not going to use those yet but just keep them there for now.
Now what we do need to do is when the sign in with e-mail button is pressed we need to set it so that
our in or log in VC here is presented.
So to do that all we need to do is create an instance of log NBC and then present it.
So Type let log and VC equals storyboard.
I can't type storyboard dot instantiate view controller with identifier.
Remember we just set it to be equal to the same thing as the class log in VC capital L log in VC.
Very cool.
And then we need to present it present log and see animated and nyl for the completion because we don't
care what happens when it's complete.
We just care that it is complete.
OK.
Very cool.
It's going to ask that I force on wrap this because as you can see the storyboard here is being accessed
optionally.
So we're going to force and wrap it but it's cool because we know that we have a value.
We know that we have what we need.
Very cool.
And so as you know this should be all that we need to do to present the view controller we're accessing
AVC we're getting an instance and we're presenting it using present view controller.
Very very cool.
That is pretty good.
Let's go ahead and let's head into the view controller class for log in visi.
OK let's get rid of the boiler plate nonsense there.
That looks good.
And now what we need to do is we actually need to set up what happens to these text fields.
And in order to modify them we're going to need to go ahead and create some Iby outlets to use those
so the top is email field.
OK right click and drag here to the bottom password field.
Very cool.
It looks great.
And what else do we need.
We need an action for our sign in button
sign in button was pressed may leave some space there and our cancel button or maybe close button.
Yeah close button was pressed.
Whoops that was an outlet.
My bad.
Delete that right click and delete the connection there.
Let's do that one more time.
The right way.
Change it to an action.
And we're going to type close button was pressed.
Awesome.
Create some room here.
Now the close button.
That's easy.
We just need to call dismiss.
Animated is true and completion is nil.
OK that'll just get rid of the Wii U controller.
That's wonderful.
But what we need to do now is we need to set up our registration button to actually register a user
like it's supposed to.
OK.
Now in order to actually do that we need to set up the UI textfield delegate so that we can better monitor
what is actually happening in these text fields.
So to do that I'm going to go ahead and just create an extension of log in VC and I'm going to go ahead
and conform to the UI textfield delegate and now that I've done that we can actually set the delegate
of these two textfield in view TD load.
So let's do that by simply typing email field delegate equals self and password field delegate equals
self.
Ok so now we can actually access these text fields we can call functions on them and do all kinds of
really cool stuff.
And so let's go ahead and let's just get rid of this for now.
We don't need anything in here but we do need to set up what happens when we push that sign in button.
So we're going to go ahead and set it up so that in order to sign in you have to put in an email and
you have to put in a password.
So let's go ahead and write some conditional code to check against that by typing if email field text
is not equal to nil to nil and password fields text is not equal to nil OK so if there is text in both
go ahead and we're just going to try to log in a user from our Auth. service.
So go ahead and type off service instance log in user.
We're going to always try that first.
And this is getting really ugly.
So I'm going to go ahead and open up.
Log in Visi full screen because we've connected everything from the interface builder.
So we're going to try to log any user first and if that fails then we will try to register them.
OK.
So go ahead and log in user go ahead and type email field text because if there is text in the e-mail
field we'll pass it in.
OK.
The password is from the password field of course.
And you know what it's going to complain to us that we're not unwrapping this because even though we
know we have a value here it's still optional coming from the textfield.
And so we have to unwrap it when we call this function.
And now what we're going to do is we're going to press enter on our completion handler and we're going
to call this boolean success meaning if success then good if not success then bad and the error of course
if we get an error back is just going to be log in error.
Now let's say that we go through everything is successful we're good to go.
If success meaning if we are successful to log in we're going to go ahead and just dismiss the view
controller because we don't need anything else.
We don't need to display an error or we don't need to say anything that's good to go.
But what if we have a problem.
What if there is an error else meaning if success is not returned if we get false back we're going to
go ahead and just print the error for now.
Whoops.
All right.
Oops.
No it's loggin air we said it to be log an error.
Localized description.
OK.
Pretty cool.
And I think it's going to warn us.
Yeah.
You know what.
In swift for you you're not allowed to print things that are not explicitly strings.
And so it's being coerced basically.
So what we need to do actually is just type string and then describing log an air dot to localize description
and that will get rid of that problem for us.
As you can see we're basically just forcing this to be a string and not any.
OK.
So if success we're going to dismiss Otherwise we will print the error to the console so we can see
what is the problem.
And awesome so if success we're going to dismiss the view controller and that's awesome.
Now what if we needed to register a user right.
What if there was a problem and an error happened.
We're going to go ahead and then call off service instance register a user.
OK.
Meaning if there is a success we're going to dismiss the view controller.
Otherwise we're just going to print the error and then we're going to attempt to register the user because
what if they have never logged in.
Obviously there would be an error if they've never logged in.
It will print the error and then move on to try to register this user.
So now that we're inside of a closure we need to actually call a self e-mail field text to give it our
e-mail self password field text and then user creation complete.
We're going to do the same thing.
We'll use success meaning if a user was successfully created.
And if registration error is going to be the constant We'll name the error.
So now oh it's yelling at us we need to unwrap these blah blah blah.
Very cool.
OK.
Now we're good.
So what we're going to do is if we can successfully create a user if success go ahead and we're going
to actually log in our user now so go ahead and call off service instance log in user self-caused email
field dot text unwrap it self dot password field dot text unwrap it and then of course if log in is
successful
meaning Well you know what if we just create a user and they're created.
If it's successful then we can't really get an error back from loggin.
So go ahead and just type nil because we can't get an airbag for that.
And if it's successful we're going to go ahead and
dismiss the view controller by calling it dismiss and animated is true.
Completion is nil and this is how we're going to sign any user We're going to first try to plug them
in.
If that fails we'll register them and after we've registered them successfully we will log in the user
which is really cool although looks like we need to call self here because we're in a closure.
But what if we didn't have a success what if it failed to register the user.
In that case we'll just print a string describing.
Let's go ahead and find that string describing there it is and it's going to be registration error dot
localized description.
OK so Will print that out if there is a problem.
Very very cool seinen is now possible.
And I think we should go give it a shot.
When that sign in button is pressed it should try to log in the user it'll print out everything here
to our console if it's successful it'll dismiss if it's unsuccessful it'll print the problem then try
to register the user.
If that's successful it will log in the user then dismiss them.
Actually you know what we don't even need to have this secondary dismissal here.
We just need to go ahead and call the function.
Let's go ahead and just type print
successfully registered user because we are already dismissing the view controller if log in is successfully
completed.
So if it's a success we'll go ahead and just print successfully registered user because at this point
if we register them with an email and password then we can't get an airbag for registration.
And of course connectivity issues is one thing but for the actual registration we can't get an error
back so we'll just print successfully registered user.
All right.
So let's think about what we've done here we have set it up so that we can present our log in visi.
Well we present off Visi from the app delegate then we present log in we see when we tap the email button.
OK.
And then we can actually try to sign in so I'm going to pull open firebase here so we can watch our
database reloaded.
So it's good to go.
Let's build and run this to our iPhone and let's see if it works let's try to add a user to firebase
and see what happens.
OK.
So let's actually try to watch this side by side I'll pull this open on this side.
You know what may be on this sites we can watch the console as well.
Let's make this a bit smaller so we can see the console and firebase and this at the same time.
All right.
So click sign in by e-mail.
It presents beautifully.
Let's tap this.
It dismisses.
That's exactly what we want.
Now let's go ahead and let's just type Caleb slopes dot com and let's just do.
I am a wizard hoops.
I am a wizard.
That's my password.
It's not my real password but just for this app.
So when I type sign in it should first try to log any user.
If that fails it should print an error try to register them.
If that is successful it should log them in and then dismiss this view controller.
Let's give it a shot tap sign in K and we will actually go ahead and see if we get any errors.
The given sign and provider is disabled for this firebase project enable it in the firebase console
under the seinen method tab of the auto section.
I did not show you this yet and this is very important.
You can't actually authenticate with anything unless you enable it in firebase.
So go to firebase tap on authentication and check it out.
There is a tab called sign and method.
Everything is disabled.
We have not even given them the chance to log in.
So let's turn on email off.
Turn it on.
Save it.
And now it's enabled now the cool thing is we don't even need to build and run our app again.
We can just tap sign in and see if it works.
So let's go back to the database.
Let's tap seinen says there is no user record.
And then it tries to register them and looks like it's not dismissing our view.
Interesting.
So I wonder why that is it looks like it's successfully created the user.
They popped up in our database.
Right.
There's our unique ID.
There's the e-mail and the provider is firebase but it looks like it is not dismissing.
That's interesting.
And you know what I think that is simply because when we log in the user it's not calling the function
up here.
It's just we're calling it twice.
So let's actually set it so that we can actually dismiss the View Controller.
That was my bad.
I was not thinking clearly there.
So now when we actually try to log in and create a user it should dismiss I built and run ran.
Built in RAM built and run.
OK now it's not showing up and we need to think why is that.
Why is it not popping up and why is it showing our Tabar controller.
That's because we're authenticated.
That's the coolest thing.
So look if we go into our app delegate We said if there is a current user I mean if there is no current
user we should present this.
But right now we're logged in.
So it's not presenting it that is the coolest thing.
Now we'll set up later how to log out so that it pops up again.
But for now in order to get rid of our selves we're going to just go ahead and delete the user's domain
just like that.
And when I go back in you'll see that it's oh it's still logged in because we need to actually delete
the app.
And now that our users are no longer in firebase if we delete it and then run it again it won't be logged
in because we have basically removed the app.
We've removed all of the things that gets saved.
And look it's still logged in.
So it looks like firebase is actually holding on to this user unless we explicitly sign them out.
So we will handle that in a video to come.
But for now we are successfully logging in.
We're successfully adding users to our firebase database and in the next video we're going to set up
a couple more of our view controllers and we're going to also set up our profile view controller and
that's where we're going to be able to log out and actually forcefully sign ourselves out of firebase
very very cool.
Awesome job guys.
And we'll see in the next video.