1 00:00:00,710 --> 00:00:04,330 In the last section we were able to test our entire authentication flow. 2 00:00:04,470 --> 00:00:09,270 But there's still one tiny thing that we're missing to really call it complete and that is the ability 3 00:00:09,270 --> 00:00:12,420 to sign out or log out of our application. 4 00:00:12,690 --> 00:00:16,470 So we're going to add a tiny bit of code that will enable a user to log out. 5 00:00:16,590 --> 00:00:18,410 And this really is rather straightforward. 6 00:00:18,420 --> 00:00:24,300 So let's just get to it and make it happen inside of my all throughout jazz file. 7 00:00:24,360 --> 00:00:30,240 I will find the two existing Google authentication routes that we've already set up underneath each 8 00:00:30,240 --> 00:00:30,900 of those. 9 00:00:30,900 --> 00:00:33,190 And to add one additional handler. 10 00:00:33,570 --> 00:00:40,770 We're going to say that whenever a user who is authenticated makes a request to the route slash API 11 00:00:40,860 --> 00:00:44,910 slash log out we will log the user out of our application. 12 00:00:45,330 --> 00:00:49,420 So just like any route handler we put together first argument is the route. 13 00:00:49,440 --> 00:00:55,470 The second argument is the function that we want to execute whenever a user accesses this route. 14 00:00:55,470 --> 00:00:58,770 It has the arguments req and Retz. 15 00:00:58,980 --> 00:01:01,550 They're short for request and response. 16 00:01:02,220 --> 00:01:02,490 OK. 17 00:01:02,520 --> 00:01:08,550 So just like we spoke about in the last section we had said that passport automatically attaches this 18 00:01:08,550 --> 00:01:12,270 wreck and you or this user property to the wreck object. 19 00:01:12,330 --> 00:01:18,330 Passport also attaches a couple of other functions to the request object as well that we can use to 20 00:01:18,330 --> 00:01:21,370 manipulate the user's authentication status. 21 00:01:21,600 --> 00:01:28,260 And for us the one that we care about the most is something called rec dot log out. 22 00:01:28,260 --> 00:01:30,560 And I bet you can guess what that function does. 23 00:01:30,900 --> 00:01:37,440 So don't log out is a function that is attached automatically to the request object by passport when 24 00:01:37,440 --> 00:01:39,080 we call log out right here. 25 00:01:39,120 --> 00:01:45,120 It takes the cookie that contains our users ID and it kills the ID that's in there and says OK you're 26 00:01:45,120 --> 00:01:47,650 logged out you no longer have any IDs. 27 00:01:47,730 --> 00:01:49,770 Any idea what user you are. 28 00:01:49,770 --> 00:01:52,570 And so it just kills the cookie that's in there and says That's it. 29 00:01:52,590 --> 00:01:54,080 You're not that user anymore. 30 00:01:56,040 --> 00:02:02,010 Just to make sure that we send back some acknowledgement to the user saying OK you are no longer signed 31 00:02:02,010 --> 00:02:09,410 in anymore we will send back red dots send and we'll just do recked user like so. 32 00:02:09,610 --> 00:02:13,950 And so this is just going to prove to whoever is making this request that they are no longer signed 33 00:02:13,950 --> 00:02:14,670 in. 34 00:02:14,670 --> 00:02:20,640 So whenever someone makes this request to log out they should always get back something that says essentially 35 00:02:20,670 --> 00:02:26,150 undefined or no content or just basically nothing at all. 36 00:02:26,190 --> 00:02:30,750 So let's test this out inside the browser now and we'll be able to go through our entire authentication 37 00:02:30,750 --> 00:02:31,890 flow. 38 00:02:31,910 --> 00:02:38,420 So back inside the browser I'm going to kill the existing tabs I have to make a new one. 39 00:02:38,940 --> 00:02:43,260 Now I'm currently considered to be logged into my application and so I'm just going to go ahead and 40 00:02:43,260 --> 00:02:51,990 immediately attempt to log out by accessing localhost Kolin 5000 slash API slash log out. 41 00:02:52,170 --> 00:02:55,280 Now when I do so I should see a completely empty screen. 42 00:02:55,380 --> 00:03:01,890 And that means that we have gotten a response from our application but that response was empty because 43 00:03:01,890 --> 00:03:04,260 we no longer have a record user. 44 00:03:04,440 --> 00:03:09,780 The instant we log out we answer we call log out right here recked user is destroyed by passport because 45 00:03:09,780 --> 00:03:12,320 we are no longer signed in. 46 00:03:12,330 --> 00:03:18,780 So now if I tried going to API slash current user and remember we just to find that in the last section 47 00:03:19,860 --> 00:03:25,920 I will also see an empty screen because rec user no longer exists. 48 00:03:26,040 --> 00:03:32,340 Of course I can always sign back into the application by going to a local host call 5000 Auth. slash 49 00:03:32,340 --> 00:03:35,930 Google that kicks me into the flow. 50 00:03:35,940 --> 00:03:40,360 We still see this little warning message right here which we will resolve in just a little bit. 51 00:03:40,500 --> 00:03:48,030 And now I can again check to see who I am so I'll do API slash current user there's my user model again. 52 00:03:48,030 --> 00:03:54,760 And of course I can log out again and then get my current user again and still empty. 53 00:03:55,110 --> 00:03:55,410 OK. 54 00:03:55,440 --> 00:03:57,610 So that's pretty much laaga not that. 55 00:03:57,720 --> 00:03:59,180 So let's continue in the next section. 56 00:03:59,220 --> 00:04:04,860 And we're going to take care of a couple of odds and ends and we will also do that optional discussion 57 00:04:04,890 --> 00:04:09,420 that I've been talking about nonstop at this point around some of this like cookie stuff over here as 58 00:04:09,420 --> 00:04:09,850 well. 59 00:04:10,080 --> 00:04:11,830 So I'll see you in just a second.