All right. 1
So, now that you've successfully incorporated Firebase into your project, it's time to actually use it 2
and register some users. 3
So this is currently how our app looks. 4
And, essentially, what we're going to be doing is from this registration screen, we're going to be getting 5
the user to type in their email and their password. And once they've done that, we're going to--when they 6
press the register button, we're going to route this request through Firebase. And, essentially, we're asking 7
Firebase to keep a note of this user's email and to encrypt their password. 8
Now, once they're done and this was successful, then we're going to take them straight to the chat screen 9
where they're going to be able to see the messages that's associated with their account. 10
So let's go ahead and try this out. Now, in order to figure out how to do this, 11
we're going to use the Firebase Getting Started Guide for iOS. 12
And if you don't have that link, then you can simply just go to firebase.google.com/docs/ios 13
and here if you scroll down, you'll see a section called Authentication. 14
We're going to select iOS as our platform. 15
Now, we're going to be using password authentication which ask the user for an email and a password 16
and they're going to need their email address and password later on to be able to log in again. 17
Firstly, we've already done this part where we've added Firebase to our project. 18
The next thing we have to do, though, is we have to enable email and password sign in our Firebase console. 19
So let's head over to our Firebase console and go into our project Flash Chat iOS13. 20
And here we're going to click on the Authentication tab and click on sign in method, 21
and then we're going to edit this email and password method of signing. 22
So we're gonna go ahead and enable it and click save. So, now that this is enabled, we can go back to the 23
documentation and we can proceed to the next step. 24
So, first, we need to import Firebase in our ApplicationDelegate and you should have already done this 25
in the last session. 26
So just check that you've got Firebase imported and you've got the FirebaseApp already configured and 27
connected to the Firebase servers. 28
So, now that we've done steps one and two, the next step is to go ahead and add this line of code where we 29
use the authentication module from five days to create a new user using an email and a password. 30
So notice how these two inputs, these two arguments, we have to supply. And in our case, we want to do this 31
inside our RegisterViewController in the moment when they press on the register button which is this 32
one right here. 33
So they're going to type in their email, 34
they're going to type in their password, and then they're going to press on the register button and we're 35
going to trigger this line of code. 36
So let's go ahead and copy this line of code and paste it inside our IBAction and delete this comment 37
right here. 38
Now, notice how as soon as we do that, we get some errors. 39
Firstly, it tells us that it doesn't know about "Auth." 40
So Auth, of course, comes from the Firebase module, 41
so just as what we did in the app delegate, we have to import the Firebase module here, so it knows about 42
this authentication module which contains a createUser with email and password method. 43
Now, the next two errors are easy because it tells us that it doesn't know what email is and it doesn't 44
know what password is. 45
So in this case, the email that we're going to use to register the user is going to be based on whatever 46
they typed into the email text field. 47
So in here we're going to create a constant code email 48
and we're going to set it to equal emaiTtextField.text and the password is, of course, going to be from 49
the passwordTextField.text. 50
Now, notice how even as I'm typing this text property that it's telling me that the text displayed by 51
the text field is not a string but an optional String denoted by that question mark. 52
So I can't simply just leave it as this because I'm gonna get the error telling me that the optional 53
type String which refers to this one must be unwrapped to a value of type normal String in order for 54
it to be used as an input to the email parameter 55
and the same thing with password. 56
So we can use what we learned about optional binding in order to turn the emailTextField.text 57
into a real string. 58
So to do that, we add the word "if" in front of the "let" and I'm going to chain these two things together. 59
Let email = emailTextField and let password = passwordTextField.text by adding 60
a comma here. 61
What this does is it says if emailTextField.text is not nil and it can be turned into a string 62
stored inside this property called email, and the same thing with password. And only if both of these 63
things do not fail, then do we continue on to the next stage which is to actually create our user. 64
Now, you'll notice that inside here, 65
email is now a normal string and so is password. This way. 66
we've done optional chaining on two optionals and they both have to not be nil for this "if" statement 67
to be carried out. And in our case, that makes sense. 68
We can't have a nil email. 69
We can't have a nil password if we want to create our user. 70
So, now inside this closure, we get access to two things. 71
One is the authentication result which you'll see from the documentation that this is the user's account 72
data after they've been successfully registered. 73
The other thing we get back is an error if there is one. And this error is an optional as well. 74
So, say, if I tried to just simply print this error, you can see that it's actually an optional error type. 75
So what we're going to do instead is we're going to, again, invoke our trustee if "let" to optionally bind 76
a new constant, which I'll call "e" to the error. 77
So this is an optional and we're going to turn it into a nonoptional if indeed there was an error, 78
so if error was not nil. And in this case, we're simply going to print the error. 79
Now, if you wanted to build out this app for deployment instead of, say, for learning, then you might want 80
to think about the ways that you can relay this error back to the user. 81
Say, for example, creating a pop-up or a heads up display like we did previously. But in my case, I'm just 82
going to print it for me as a developer to know if there were any errors that were triggered. 83
Now, the next thing is I'm going to add an "else" in here because if there were no errors or if error is 84
equal to nil, then this block is going to get skipped and we're going to fall into here. 85
In that case, then I've successfully registered my user without any errors. 86
So what I want to do is I want to navigate to the ChatViewController. So how do we do that? 87
Well, if you remember in our Main.storyboard, we've already set up a segue between the Register View 88
Controller and the Chat View Controller. And the segue should have an identifier called 89
RegisterToChat with a capital "R." capital "T," and capital "C." 90
So I'm going to copy that and I'm going to use it to trigger a segue inside this "else" block. 91
So I'm going to call the method performSegue which is down here withIdentifier and the identifier 92
is, of course, that string that I copy just now which takes the user from the register screen to the chat 93
screen, and the sender is gonna be "self" because this is the origin of that segue. 94
It's attached from the Register View Controller to the Chat View Controller. 95
And, of course, because we are inside a closure as denoted by this "in" and this syntax here, then we, 96
of course, have to add the word "self" in front of any methods that we're calling on the current class. 97
So let's go ahead and add that and we should now be error-free and ready to test our app. 98
All right. 99
So I'm gonna go over to the Register screen and I'm going to type in an email and password to register 100
with. 101
So, usually, with Firebase, they have certain rules just like you would find on any other service that 102
says a valid email must have some letters or numbers, and then an "at symbol," and then some more letters 103
and numbers, and then a .com or .co.uk, or something like that. 104
So the simplest way to make sure that this is valid, I tend to test using something like 105
1@2.com. 106
And then the password, 107
I'm just going to use 1, 2, 3, 4, 5, 6. 108
Now, Firebase also has some rules on passwords. 109
One of these rules is that the password must be at least six characters long. 110
And notice how the password field is being obscured because in our Main.storyboard when we added that 111
Password TextField in the attributes, we selected Secure Text Entry and you should do that for the password 112
fields that you create in your apps, so that other people when they're looking over your user's shoulders, 113
they're not going to be able to see your user's password. 114
So, now that we've done this, let's go ahead and click Register. And now because the registration process 115
was successful, we got taken to the Chat View Controller. 116
This one right here. And we didn't get any errors printed in here. 117
Now, you might say. "But wait a minute, I do see an error. I see something in my console that's about the 118
iCloud Keychain. 119
Well, this is actually a warning from the iOS operating system and it's because we're testing our app 120
using a secure entry field for passwords, but we're doing it on a simulator where we don't have the user's 121
iCloud Keychain details. 122
This is not an error from Firebase Authentication. And this is trying to tell you that you can use a 123
iCloud Keychain to suggest the user a automatically created strong password. 124
But in our case, we're not too bothered about this AutoFilling process. 125
So I'm just gonna ignore this warning message and it's not going to affect your app in any way. 126
And up here, I've got some more printouts which actually come from my WelcomeViewController. 127
So I'm gonna disable all of these print statements and delete them because we're now done with that 128
part of the section. 129
Now, you might have kept the CLTypingLabel, so you won't get these print statements. But in my case, 130
I'm just going to simply delete them. 131
So now that we've bypassed this error message which means we've successfully registered our user, 132
we can also head back over to our Firebase console. And if you select your project and go to the Authentication 133
tab, you'll see that under the Users tab, you've now got a brand-new user. 134
And as convention with secure authentication, you should only be able to see your user's emails. 135
You should not be able to see a text version of their password. 136
And this is because if you store a user's password as plain text in any sort of database, it makes it 137
really prone to hacking. And you don't want what happened to Ashley Madison to happen to you. 138
So in this case, we're actually offloading this burden of securing the user's password to Google who 139
are pretty much expert at stuff like this. 140
So that means all of your user's passwords are going to be securely encrypted and stored safely. 141
And you don't have to think about cryptography or encryption or hashing or any of that stuff, it's all sorted 142
for you. 143
So now that we've successfully registered our first user using the Firebase Authentication module, let's 144
go ahead and see what are some potential problems that can happen in this process. 145
Let's say that if I decided to register my user, and remember how I told you previously that you can't 146
actually have a password that's less than six characters long. 147
So if I just put in 1, 2, 3, 4, and I click register, you can see that in the console, I get an 148
error. 149
And in the app, I don't get to proceed forwards into the chat screen. 150
So what's this error all about? 151
Well, it says that the password must be six characters long or more. 152
So if you wanted to, you can actually happen to the property called localizedDescription. 153
And this is a description of the error that is localized to the language that the user has selected 154
for their iPhone. 155
So if they're based in Spain, then this should be in Spanish. 156
And if the phone is set to French, then it should display the description of the error in their own language. 157
And this is really useful because you can use this localized description which doesn't contain all of 158
this funky stuff about error codes or, or, or anything like that. 159
Instead, it has something that the user can understand such as the password must be six characters long 160
or more, and that gives them really good feedback as to why it is that the app is not working and taking 161
them through to the chat screen. 162
Now, this module is long enough as it is. But if you wanted to, you could actually put this string into 163
a label, say, somewhere up here or somewhere down here, wherever you like, or even a pop up if you wanted to 164
to give the user some feedback as to why the app is not letting them continue. 165
So, now that we've managed to register our very first user: 1@2.com, 166
the next step is to log in this user, right? In the next lesson, 167
we're going to be tackling the login route of our app, and we're going to be using the Firebase Documentation 168
to try and get our login working for this user and their password. 169
We're going to go through it together. 170
So all of that and more, I'll see you there.