image
Sign In
Start course
Difficulty
Intermediate
Duration
3h 35m
Students
56
Description

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!

Intended Audience

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

Prerequisites

To get the most out of this course, you should have some basic knowledge of iOS.

Transcript

Hi. Within this lecture we're going to see how we can sign the previously created users in and how we can log them out so that we can complete the authentication process. So in here, rather than doing just a segue, I'm going to delete here and we're going to use this Auth again in order to sign the users in. So, let me just start by typing Auth, and of course, we can check to see if email.text is not an empty string again because if they're actually empty strings we don't just want to send a request to the server, we just want to display an alert message to the user as usual. So, let's say, .passwordText.text is not an empty string. Then I'm going to do my thing and else I'm going to display a message to my user saying that this error user name and password, okay? So in here, I'm going to use the Auth class from the fire base one more time. So, I'm going to say Auth.auth, okay? So, I'm creating the instance again, and rather than saying create user, I'm going to say not login but sign in, okay? If you write sign, you will see a signOut and you will see a signIn. And we are interested in with this. So, signIn withEmail, password, and of course, a completion block. So, this is exactly what we're looking for. We're just going to provide an email and then a password. So, the email will come from emailText.text, okay? And you can just force unwrap this and for our password, passwordText.text. You can force unwrap this as well. And in the completion block, if you hit Enter, it will give you 'authdata', okay? So, 'authdata' and 'error'. So, if you say, error is not nil if error is not nil, we're going to display an alert message one more time. So, I'm going to copy and paste this self.makeAlert thing in here, it will get this error from this error this time. So, it gets reflected over here, okay? And if it's nil actually, it means that we are good to go, so we can perform a segue. So, we are doing the same thing basically. But rather than creating the user, we're just signing them in. And this is a very cool feature because it actually checks the password of the user. So, if we give the wrong password, it displays the alert message for us. So, let's test that with a lars@metallica.com. We don't have that user right now. So, rather than saying sign up if I say sign in, as you can see we get the error message, it says that there is no user. So, that's good, right? We know what we're dealing with in here. So, we get this cool air messages from Firebase, let's do it with James but I'm giving wrong password. One more time and as you can see it says that password is invalid. So, let's do James with the real password, 1, 2, 3, 4, 5, 6. And if I click on sign in here you go, I'm signed in and it took me to the feed. So, it's going good. We have created user, we manage sign in process, all thanks to the Firebase. If we tried that with a regular server without any backend support, we would have to write tons of code in order to make this happen. And thanks to the Firebase we did it with a snap of a finger, okay? So, we are now inside of our feed view controller but the thing is, if I close this app and open it one more time it will ask me to give my credentials again. So, this is not we want. We want to remember if user has checked in, signed in or not. So, every time I open the app I just don't want to give my credentials, right? Because nobody will use my app if I ask them. So, we have to come up with a way to solve this problem. And in fact, Firebase has this capability to check if a user has signed in or signed out before. So, in the viewDidLoad, in my view controller, I'm going to show you how to do that. So, we're going to use Auth, again. And if you say, Auth.auth.currentUser, okay,  this one, the current user, it gives you an optional user as you can see. And if this is nil, it means that nobody has signed in. If this is not nil, it means that somebody has signed in before. So, if you assign this to a variable and if you check to see if current user is not nil, it means that there is somebody already signed in before. So, I'm going to say, 'if current user is not nil', then it means that I have a user and I can try to do a segue from this view controller 'tofeedViewController', for example. And this is what we did before. Actually, when I run this, you will see that this won't even work. But I'm going to show you anyway because if you Google it, if you Google have to remember user in the Firebase IOS Swift, you will get this code. But in fact, this is not what you're looking for. So, let me just sign in one more time, james@metallica.com and let's say sign in, it will sign me in. But if I run this from the scratch, it should have taken me to the feed view controller anyway because we have signed in before. But even though I try again, as you can see, I cannot go to the feed view controller. So, that's not the way that we want to execute this code. We want to get this code and execute it in the scene delegate. So far, we haven't seen scene delegate yet. And this is a great opportunity for us to learn how to do stuff in the scene delegate as well. So, what I'm going to do, I'm going to cut all of this from the view controller, okay? We're not going to check this in the view controller. So, let me select everything and hit Command X, and let's go to scene delegate. So, we want to do it in here because we have a variable called window in the scene delegate, okay? Before we had this in the app delegate, now we have this in the scene delegate. So, in scene delegate, we're going to do this check. And if you have this window in app delegate for any reason, I don't know, maybe you're using an earlier version of Xcode, then you have to do this inside of app delegate. But after iOS 13, there is a scene delegate and in the scene there is a function called 'will connect to session', and that's where we can change the scene. That's where we can change the window. And by window, I mean we're going to change the route view controller. As you can see, if we change this entry point it defines which view controller is going to be shown first when we actually start this app. And we're going to take this arrow to the tab bar controller if and only if a user has signed in before, okay? We're going to do this programmatically. We're going to do this within codes. And in order to do that, I have to come to tab bar controller and in the storyboard id I have to give it an ID, like Tab Bar. And I'm going to use that tab bar in here. Okay? So, let me paste this here. And of course this will give me an error because we cannot do a segue from here, right?  Because we're not in the view controller, we're just in the scene delegate now and we're just going to check to see if current user is not nil. And in order to check this, make sure that Firebase is actually imported in the scene delegate. If it's not, you have to add it yourself, like import Firebase. And then it's going to be like this. I'm going to check if the current user is not nil and if it's not nil, I'm going to change the route view controller, I'm going to move that arrow to the tab bar controller. In order to do that, I'm going to create a board variable in which I will create my UI storyboard. So, what's the name of my storyboard? It's Main with a capital M, okay? And the bundle is nil and this is my storyboard, actually. So, I can create a view controller using that storyboard. I'm going to call this tab bar and I'm going to say board.instiatiateViewController. So , here you go, we have this option, instantiateViewController with identifier. So, we have created an identified for tab bar, right? So, if you read the description, we can represent at view controller using that instantiateViewController. And remember we have to cast this as UI tab bar controller. Because our tab bar controller doesn't have any class, it's just a UI tab bar controller. For example, if I tried to assign my settings view controller as a variable here, I would then have to cast this as settings view controller. But right now in class I don't have anything. So, I'm just going to cast this as UI tab bar controller. So, remember if you're dealing with a specific class then you have to cast this as a specific class. Then I'm going to come over here and say, window.rootViewController and this is tab bar. And this is exactly where I take the arrow and put it in front of my actually tab bar. And we are doing this only and only if the current user is not nil. So, let me open this and try and as you can see, we are in the feed view controller. We haven't got to see the signing view controller at all because we have already signed in before. Now, we have to find a way to log out so that we can test if this works or not. And in the SettingsViewController, we're just performing a segue but this is actually wrong right now because we have to sign out. We have to let Firebase know that we have signed out. So, I'm going to import Firebase in here as well and I'm going to delete this because first we need to sign out, then we 're going to perform this. So, I'm going to call Auth.auth.signOut. And as you can see it throws an error. So, what do we do when we get and chose syntax in here? We have to use do try-catch structure. If you remember, we have to say do try. What are we going to try? We're going to try Auth.auth.signOut and we're going to catch the error if there is any. And for right now, I can just do a print error, okay? And then actually, I can do perform seguating in here. And there are two ways to do that. The complex way is to go to the scene delegate and call the function that we have written before in order to check if there is any signed in user and change the view controller route ViewController one more time, but I'm not going to do that. I'm not going to bother just taking all of this, I'm putting into a function and then trying to define this as a scene delegate variable and then try to reach that function, yada, yada yada. It's an alternative but I'm not going to do that because it's going to make this a lot more complicated than it should be. Rather after trying this, I'm just going to say self.perform that self.perform segue and the identifier that I'm looking for is toViewController and the sender is nil. So, let me go to my main storyboard and by doing that, let me check the segue name, toViewController. So, now I'm certain that this will work but let's just test to see because I'm going to go into the feed view controller once I hit that because I'm already signed in, now I'm going to sign out, okay? So, log out. And now we see the sign in, but in order to make sure we have to run this one more time, then we have to see the signing view controller now. Yet, here we go. We see the signing view controller since we don't have any user signed in. Let's sign in with one of the users, okay? Let's go for james@metallica.com and the password is 1, 2, 3, 4, 5, 6. Let's sign in and let's run one more time to make sure this is working properly. Here you go. Now, this is working properly. And officially, we have completed the authentication module. Now, next thing to do for us to understand how to upload data to the database so that we can retrieve those information and show them to the user later on.

 

About the Author
Students
2086
Courses
55
Learning Paths
3

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.

Covered Topics