In this course, we're going to create an Instagram Clone and learn how to work with cloud servers using Firebase. By the end of this course, you will be well-equipped to build your own apps!
This course is designed for anyone who wants to:
- Learn about iOS development and coding
- Move into a career as an iOS developer
- Master Swift skills
To get the most out of this course, you should have some basic knowledge of iOS.
Hi. Within this lecture, we're going to see how to create users, how to manage users using Firebase. So, we're past boring stuff, creating user interface and everything. Now we're going to learn the real deal with Firebase. In order to do that, we've going to have to go back to our dashboard and go to Authentication module. Every time we use a module, we have to initialize it just for once, okay? So, click over here to set sign in and sign up methods. And as you can see, we have various methods over here, we're going to choose the email and password. So, this is the most basic one, but this is the most used one as well. So, I'm going to say 'Enable' and then 'Save'. And that's it. That's all you have to do just to initialize the authentication module. Then we can go back to our code and start writing code to start creating users. So, in the Database and in the Storage we're going to do the same thing, initialize for the first time and just one time, but we're going to do that later on. So, in the view controller, we have our signInClicked and signUpClicked methods, functions, right? So, in this signUpClicked, we have to start with signing up because that's where we register a new user. Without having a user we cannot sign them in. First we have to sign them up. So, how do we sign up a user? Of course, we're going to use Firebase modules, Firebase classes. And in order to use that you have to come over here and say import Firebase. And in the signUpClicked, I'm going to call this Auth. So, this is the authentication class for Firebase apps and as you can see it shows up in the description as well. We're going to use this class in order to create an authentication method in Firebase. Maybe you will have to call this FirebaseAuth as well. Before we had to do it this way, we had to call Firebase and then Firebase authentication, but now we don't have to do that. They just combined it in the Firebase. But for a reason, if you cannot see Auth maybe you will have to come back and say import FirebaseAuth as well. And here for the second thing, I'm going to call this auth, the small auth, okay? So, this is the instance of the Auth class. So, this is kind of creating the object out of Auth class. And if you say createUser you will see this option, createUser with email, password and a completion block, and that's exactly what we are looking for. We're going to provide an email and a password and it will give us a completion block in return, okay? So, how are we going to get this email? We're going to say emailText.text and I can put this exclamation point in here because I can force unwrap this. And in the passwordText.text, will give us the password, right? So, we're going to give the email and password to the user. And of course, before we do that we can check to see if email and password is actually not an empty string, right? Because if the user gives us an empty string, we don't want to create anything for him. We really want an email and the password. So, I'm going to check to see if email.Text is not equal to an empty string and passwordText.text is not equal to an empty string as well. I'm going to open and close these parentheses, curly braces I mean, and I'm going to cut everything in the authentication method and pass it in here and else I'm going to display an alert message to the user saying that you have to give me some password or you have to give me some username. So, let me do that quick because you know how to do that, right? You say let alert UIAlertController, title will be Error, message will be maybe username and password missing or something, okay? So, you can write whatever you want in the message, and preferredStyle, hit 'Enter' and say .alert. And the next thing we have to create a button here. So, I'm going to say okButton. This is going to be a UIAlertAction as you might remember. So, title will be OK and style will be default or cancel, and we don't need any handler. And later on I'm going to add this to my alert saying that alert.addAction okButton. And for the final thing I'm going to say self.present. I'm going to present the alert with an animation and without any completion. So, that's what we have been doing so far in order to create alerts. And in here it will give us a result. So, this is kind of a closure and it gives back a completion block for us. We have seen that before right? We give some inputs and we get some outputs as a result in here. And this is the AuthData result and an error. So, you can call this AuthData or data and you can call the error, Error. So, of course you can choose whatever name you want in here but I'm going to go for some explicit names like authdata and error. Now, I will begin to check if error is not nil, because if the error is not nil then it means that I cannot even get an authdata or I couldn't create that user for some reason. In order to get this error, you can just say error.localized description. And in this block, actually, if error is not nil, we have to display an alert message to the user as well. And this time error message will come from the Firebase. We're not even going to bother writing any error messages in here, Firebase will take care of that for us. And in else we're going to perform a segue, Why? Because we have created the user, now we want to go to the feed view controller, right? So, I'm going to say, toFeedVC and sender will be nil. And I believe we can copy and paste this alert to the error message in here. And since we're going to have so much error messages, we can just create an error message function for this as well. And we have some kind of thing in here, error message, we have to say self.performSegue because we are in a closure. Remember in closures we have to add this self.thing. So, rather than copying and pasting this, I'm going to create another function here saying makeAlert. I'm going to cut everything in here and paste everything. So, whenever I call this makeAlert, it will call it for me. So, I'm going to take an input of title and message as string because I'm going to pass these parameters to here. I don't need any third one. So, I'm going to come over here and say "title". And in order to avoid any confusion, I'm going to call this titleInput and messageInput. So, title will be titleInput and the message will be messageInput. We have done this before, so you might remember, right? From the essentials part, the beginning of this course actually. So, in the makeAlert I'm going to say error and username and password missing here, but in the above block above us we have the error from the Firebase itself. So, I'm going to say, makeAlert and title can be Error but messageInput is coming from this error this time, okay? So, I'm going to say, "error.localizedDescription. So, this will give us a description that user can actually understand. And this will give us an error saying that you have to define a default value here, in case, this error gets nil. So, this is an optional, as you can see, I'm not going to force unwrap this in this way because the error can come nil for some reason, I don't know what but... or some empty string, I'm going to provide a default value. Even though we are checking to see if error is not nil I'm not going to take any chances because this is way too easy to implement. You just say two double question marks and Error and asks us to say self.makeAlert because we are in a closure. So far so good. This way we can actually create the users and we can see if they get created or not because we will perform a segue and of course we can go back to the Firebase and check the authentication module to see if the user is created or not. So, here if I say Sign Up it will say username and password missing, okay? As you can see this got called. And let me just write an email address in here like firstname.lastname@example.org and for password let me give 123 or 1. So, if I say sign up it will give me an error. And this time this is the Firebase error. As you can see, it says that password must be at least six characters. So, this is the rule for Firebase, it has to be six characters or more. So, we're getting the error message from the Firebase as well. So far so good. So, let me do 123456. And here we go. We are inside of our feed and we can reach to other tab bar controllers as well. So, let me go to Users tab in the authentication module. And here we go we see email@example.com. We see the user unique ID. This is kind of our UUID but it's created with Firebase, for Firebase actually assigns this value to us. And we see the creation date and signing in date as well. So, that's how we create users using Firebase. And of course, it's not enough for us because we have to learn how to sign them in as well. Let's do that in the next lecture.
Atil is an instructor at Bogazici University, where he graduated back in 2010. He is also co-founder of Academy Club, which provides training, and Pera Games, which operates in the mobile gaming industry.