1 00:00:00,340 --> 00:00:05,693 In our inc folder, we're going to add 2 00:00:05,693 --> 00:00:11,557 a New File named functions_auth.php. 3 00:00:11,557 --> 00:00:17,187 We'll also need to add this file to our bootstrap file. 4 00:00:17,187 --> 00:00:23,838 Duplicate that line and we'll change it to auth and then we can close it. 5 00:00:23,838 --> 00:00:27,258 In our functions_auth file, 6 00:00:27,258 --> 00:00:31,605 we're ready to create a new function. 7 00:00:31,605 --> 00:00:34,367 We'll name this isAuthenticated. 8 00:00:40,175 --> 00:00:45,360 This function will need to read our session to know if a user is logged in, 9 00:00:45,360 --> 00:00:49,525 for the session variable to work inside of this function, 10 00:00:49,525 --> 00:00:51,744 we'll need a way to access it. 11 00:00:51,744 --> 00:00:56,759 One simple approach is to call the global $session; For 12 00:00:56,759 --> 00:01:01,680 more options see the notes associated with this video. 13 00:01:02,760 --> 00:01:08,962 We can then use session->get and look for 14 00:01:08,962 --> 00:01:14,651 our' auth_logged_in' variable. 15 00:01:14,651 --> 00:01:20,225 The value of this variable should be true if the user is logged in. 16 00:01:20,225 --> 00:01:26,750 The get method allows us to specify a default value as the second parameter. 17 00:01:26,750 --> 00:01:31,427 So we'll use for our second parameter, false, 18 00:01:31,427 --> 00:01:35,428 then we can simply return those results. 19 00:01:37,571 --> 00:01:43,910 Either true, if the user is logged in, or false if the user is not logged in. 20 00:01:44,950 --> 00:01:49,297 Now, let's use that isAuthenticated function in our navigation to show 21 00:01:49,297 --> 00:01:50,498 the proper length. 22 00:01:52,758 --> 00:01:58,156 Under templates, We have a nav.php. 23 00:01:58,156 --> 00:02:03,740 We only want to add books if the user is authenticated. 24 00:02:03,740 --> 00:02:06,008 So we can add a conditional around that item. 25 00:02:11,044 --> 00:02:15,776 If isAuthenticated. 26 00:02:20,368 --> 00:02:25,012 And then 27 00:02:25,012 --> 00:02:30,990 endif;. 28 00:02:30,990 --> 00:02:37,745 Next, if the user is logged in, we don't need to show the login and register links. 29 00:02:37,745 --> 00:02:41,460 Instead, we want to show log out. 30 00:02:41,460 --> 00:02:42,712 So let's add another conditional. 31 00:02:42,712 --> 00:02:49,708 If, isAuthenticated. 32 00:02:56,046 --> 00:02:58,050 We're going to add a new item. 33 00:02:58,050 --> 00:03:00,739 So let's copy the Login. 34 00:03:00,739 --> 00:03:04,211 And then we can add an else. 35 00:03:09,137 --> 00:03:13,846 And finally, endif;. 36 00:03:18,503 --> 00:03:23,828 We'll change the login to Logout. 37 00:03:23,828 --> 00:03:31,456 And we'll link directly to procedures doLogout. 38 00:03:31,456 --> 00:03:37,028 Let's go back and refresh our browser to see the changes in our navigation. 39 00:03:37,028 --> 00:03:41,160 We can see the add book link which hasn't changed. 40 00:03:41,160 --> 00:03:46,200 But now instead of login and register we see Logout. 41 00:03:46,200 --> 00:03:51,330 But before that Logout will work we need to add that procedure.