All right. 1
So, now that we've managed the registration flow as well as the login flow for our users, the final 2
thing to take care of is how do they actually log out, and return back to the welcome screen. In this 3
lesson, 4
that's exactly what we're going to tackle. 5
So right at the top here of this navigation bar, we're going to be adding a logout button. 6
And when the user presses on that logout button, we're going to take them all the way back to the welcome 7
screen. 8
And in order to do this, we need to be able to add in a bar button item to this navigation bar up here. 9
Let's go ahead and do that now. 10
If you head over to the Main.storyboard and navigate to our Chat View Controller which is this one 11
right here, at the top right here, we want to create a log out button. 12
So let's open up our object library and we're going to search for something called a bar button item. 13
And as they say here, this thing is going to act pretty much the same as a button, but it's going to live 14
in our navigation bar. 15
So let's drag that and drop it on the right over here. And instead of it saying item, we're going to call 16
it Log Out, and then hit enter. 17
Now, we can work with this bar button item pretty much exactly the same way as we've been working with 18
normal buttons. 19
So I'm going to create an IBAction from this button. 20
So I'm going to hold down the control key and drag it down here just above that last curly brace 21
and I'm going to change the type to be more specific which is the UIBarButtonItem type, 22
and the name is going to be called logOutPressed, and then hit enter to connect that up. 23
So now if we go into our ChatViewController, you can see that we can now write some code to be triggered 24
when our user decides that they want to log out. 25
So heading back over to the Firebase Documentation again, you can see that at the very bottom, you've 26
got a next step section. 27
And here there is a section on how to sign out our user 28
and we can do that by using the signOut method, 29
and this signOut method happens to be one that can throw an error. 30
So we have to wrap it inside 31
a do-catch block. 32
So let's go ahead and copy this code and implement it inside our logOutPressed. 33
So I'm gonna hit paste. And now we have to add the required module which is the Firebase module as an 34
input up at the top, and so that it will know what the Authentication module is all about. 35
You can either do it the way that we saw as we copied over from Firebase or, alternatively, you can actually 36
simplify things by chaining this part together with the signOut method, 37
so replacing it right here, and then we can delete that extra line. 38
Essentially, what we've got now is a do-catch block, and the catch block will get carried out if there 39
were any problems signing out the user. 40
So, now let's try this out. 41
Notice how we're going to be going through the Log In screen 42
quite a few times. 43
So here's a quick tip. 44
When you're building an app that has a log in and registration flow, it can be quite frustrating when 45
every time you need to test it, you have to enter in the email and password. To save yourself this effort, 46
what you can do is while you're testing the app and while you're building it is to simply go into the 47
relevant screen and actually assign it a value. 48
So in this case, my text for this Email TextField is going to be 1@2.com, and then the text for 49
my password text field is going to be 1, 2, 3, 4, 5, 6. 50
So those are the details that I used to sign up to my messaging service. And now whenever I run my app 51
and I go into the Log In screen, 52
those details will be entered automatically, 53
so I can just simply press log in. 54
So now that we've logged in and we're on our Chat View Controller, let's try out our Log Out flow. 55
So when I've pressed Log Out, nothing has happened in here, but I also didn't get any error messages. 56
But let's think about what we would actually like to happen if the sign up process was successful. 57
Well, we want to take our users all the way back to the WelcomeViewController. 58
So how do we do that? 59
Well, we would add that inside the do block after trying to signOut. 60
If this process was successful, it's going to continue executing the lines of code inside this do block. 61
So it's right here that we want to navigate our user to the welcome screen. 62
But how do we do that? 63
Well, the way that our navigation stack is arranged is such that we've got this at the very bottom, 64
and then we went to the Log In screen, so this lives on top of this one, 65
and then finally stacked on the very top is our Chat View Controller. 66
So that's the one that we're seeing in the foreground right here. 67
Now, if I was to click on the back button that takes me back one level, 68
so it dismisses that Chat View Controller, destroys it, 69
and if I go back further, it takes me to the Welcome View Controller. But I don't want to go through two 70
levels of back buttons, 71
I want to go straight over here. 72
So how can I do that? 73
Well, there's something called a pop to route view controller method and that is something that we get 74
as a part of the navigation controller. So we can say navigationController.popToRootViewController, 75
and this will pop and get rid of all the view controllers on the stack except the very Root View Controller. 76
So we can select animated as true as well to get a brief animation if appropriate. And what it should 77
do is it should take us all the way back to the view controller that is at the root of the navigation 78
stack which is, of course, our Welcome View Controller. 79
So let's try that out and run our app, let's log in once more, and then log out, and it takes us all the 80
way back to the Welcome View Controller. 81
The very last thing I want to fix on this screen is I want to get rid of this Back button because while 82
it makes sense to have a Back button on the Log In and Registration screens because the user might have 83
pressed on the Register button, and then realize actually I've already registered, so I can actually log 84
in and might have press Log In, and realizing that they actually need to register. 85
So that button, I think, makes sense. 86
But once we get to the Chat View Controller, 87
I don't think it actually makes a lot of sense in terms of user experience to go from here back to the 88
Log In screen. 89
So this is something that is up to you, really, 90
and it's kind of an individual choice depending on what you think is more natural for your app. 91
But I think I'm actually going to get rid of this Back button, so that it's less confusing for users 92
so that they're able to log out straight to the welcome screen and to the screen that shows the name 93
of the app. 94
So in order to hide this Back button, I'm going to set that up inside the viewDidLoad. And inside here, 95
I'm going to tap into the navigation item and I'm going to tap into something called hidesBackButton. 96
And this is a boolean, so true or false. 97
And I'm going to set it to true so that we actually hide this particular button here the next time that 98
we run our app. And now if we log in again, you'll see that in the Chat View Controller, we no longer have 99
that button right there. 100
But what if you wanted a title for the navigation bar? 101
Well, you can do the same thing inside viewDidLoad. 102
You can set the title property to whichever string it is that you want. 103
And in my case, I just want it to say FlashChat with the lightning symbol. 104
So I'm going to paste that in here as the title. 105
And now if I run the app again, and once we get to the Chat View Controller, you'll see that title show 106
up right in the middle, so that we can orientate ourselves to know which app it is that we're actually 107
using right now. 108
So that's all for the logout route. And in the next lesson, we're going to be tackling some things to 109
get rid of all of these strings that we're using all over the place. 110
So for example, we've got a segue identifier here which is a string. 111
We've got another one right here. 112
And these are really vulnerable because if you have even the slightest typo in here and it doesn't match 113
the name inside the Main.storyboard, your app will crash and it'll be quite hard to debug what's 114
going on. 115
So in order to minimize errors in the future, I want to show you how to create a constants file in Swift. 116
So that's what we're going to be doing next, 117
and I hope to see you there.