So in the last lesson, we learned about using the Firebase Authentication Module to register our very 1
first user, 2
this 1@2.com person. 3
Now, we're going to look at the login routes. 4
So in this case, it's this screen right here and we're going to be doing pretty much the same thing 5
as what we did in the register screen. 6
We're going to take the email and password that they've typed in, and in the moment, when they press on 7
the login button, we're going to take their details through to Firebase. And we're going to be using 8
a login method to check that email and password against what we've got stored in our fFrebase Authentication 9
database. 10
And if that was successful, then we're again going to take them straight to the Chart View Controller. 11
So this is the case where after the user registers for the first time, the next time, they use our service 12
they're always going to be accessing it through the login route. 13
So let's go ahead and tackle this. 14
If we head back to the documentation and we go to authentication, iOS password authentication, we can 15
skip all the parts that we've already done and head over to the sign in user with an email address 16
and password section. 17
We've already done step one and step two. 18
We now have to move on to step three. 19
So the method is called signInWithEmail and password. 20
And, again, it takes two arguments or inputs. 21
One is the e-mail that they've registered with and another is the password that they've registered with. 22
And once this closure completes, we end up with an authentication result and an error 23
if there was one. 24
So the user's account data will live in this authResult object that we get back and we can use it inside 25
this closure block or we can check to see if there were any errors. 26
Again, I'm gonna go ahead and copy this code, making sure that you're copying the Swift code and not the 27
Objective-C code which will look very strange anyways. And heading back to our app, 28
we're going to go to the LoginViewController.swift and we're going to put that code inside the 29
loginPressed IBAction. 30
So that, of course, relates to this particular button when it gets pressed. 31
And what we want to happen is to trigger this sign in with email and password method. 32
So, again, we need to tell this file that we want to use the Firebase module, so that we can use the Auth object 33
and we also have to supply an email and password. And I want to quickly pause here and pose a 34
challenge to you. 35
See if you can add the code to retrieve the tags that the user typed into the email and the password fields, 36
then you should only call the sign and method if neither the email nor the password text is equal 37
to nil. 38
I'll give you a few seconds to pause the video before I show you the solution. 39
Ready? 40
Here's the solution. 41
I'm going to do the same thing that we did before to use optional binding to get out the email text 42
and password text. 43
So I'm going to write "if let email = emailTextField.text, and then add a comma, 44
and let password = passwordTextField.text, and then we're going to wrap all of this authentication 45
code inside a set of curly brackets like so. 46
And now we're ready to tackle this block. 47
And that's the solution to the challenge. 48
The code we wrote is very similar to what we did in the RegisterViewController. 49
Now, you might have noticed that this particular signIn method actually looks a little bit different 50
from the create user method that we got from Firebase earlier on. 51
And the extra bits are this part, the "weak self," and also this "guard" statement, and this is just a little 52
bit of code that Firebase has actually left in here which is actually slightly outdated. The latest syntax 53
would look something like this. 54
And what it does is it avoids a retained cycle in the cases where we're actually referring to the view 55
controller within our closure, 56
and we're going to be destroying this view controller before this closure completes. 57
So if you're interested in getting more details on this and why it is that we don't actually need this 58
part or this part in our code, then I've linked to a number of resources for you to check out. And I have 59
to warn you it's quite complex and the rabbit hole goes quite deep, 60
but I found some resources that explain this topic quite well. 61
So if you're interested in the nitty-gritty details, then be sure to check it out. 62
All right. 63
So coming back to our Authentication signIn method, we want to do the same thing that we did before 64
which is to check if there was an error. 65
So if let e = error, then in this case, I'm going to simply just print the error, 66
but, of course, we could also handle this error by presenting it to the user, in which case we would have 67
to take a look at the reference for "FIRAuthErrors" to see all of the different error codes, create a 68
switch statement that checks for which one we're gettingm and then turn all of those error codes into 69
a message that's useful for the user, and then present it in some sort of alerts. 70
Now in my case, I'm just going to print it out for the developer to see. 71
But if we know that the error is equal to nil, then we can go ahead and add an "else" statement to proceed 72
with the login process. 73
So in this case, what we're gonna do is we're going to keep the login screen in our navigation stack. 74
So at this point, they've got the welcome screen at the bottom, 75
the login screen next, and then on top of that, we're going to present the next screen which is the 76
ChatViewController. And I want to pose this to you as another challenge. See if you can write the code that 77
navigates the ChatViewController when signIn has been successful. I'll give you a few seconds to pose 78
a video before I show you the solution. 79
Ready? 80
Here's the solution. 81
We're going to navigate to the ChatViewController in the same way as we did in the RegisterViewController 82
which is to perform a segue with a particular identifier. 83
And in our case, that segue is this one which we named LoginToChat. 84
So I'm just going to copy it from here to minimize the possibility of typos. And then down here in this 85
"else" statement, I'm going to say performSegue withIdentifier and sender is "self." 86
This is the starting point of our segue. 87
And the final thing we have to do is to add the "self" in front of our performSegue method call, and 88
that's the solution to the challenge. 89
We're now ready to run our app and test it out, so instead of going to the register page, I'm going to 90
go into the login page. And here I'm going to use the credentials that I used previously to register 91
which is a user with email 1@2.com and their password was 92
1, 2, 3, 4, 5, 6. 93
Because six is the minimum number of characters I have to supply Firebase with in order for it to accept 94
it as a password. 95
So, now my email is going to be 1@2.com and my password: 1, 2, 3, 4, 5, 6, 96
and then I'm going to click Log In. 97
And as you can see, the login process was successful. 98
I didn't get any error messages and it performed to that segue taking me to the Chat View Controller right 99
here. 100
So that's how we log in our users. In the next lesson, 101
we're going to be covering the logout route so that we'll be able to log in users, register users, and 102
also log them out back to the welcome screen. 103
So for all of that and more, I'll see you on the next lesson.