1 00:00:00,370 --> 00:00:02,950 Cookies can be a bit tricky to work with when using 2 00:00:02,950 --> 00:00:05,500 only the built in PHP functions. 3 00:00:05,500 --> 00:00:07,930 Part of the reason we're using the symphony package for 4 00:00:07,930 --> 00:00:12,040 getting the request is the ease of setting the cookie on the request object. 5 00:00:13,180 --> 00:00:14,481 To create our cookie, 6 00:00:14,481 --> 00:00:18,055 we used a cookie class from the symphony package we pulled in. 7 00:00:35,561 --> 00:00:37,820 There are a few parts to the cookie. 8 00:00:37,820 --> 00:00:39,820 First the name of the cookie. 9 00:00:39,820 --> 00:00:41,916 For which we'll use access token. 10 00:00:44,659 --> 00:00:50,420 Next, the jwt. 11 00:00:50,420 --> 00:00:52,108 Followed by the expiration time. 12 00:00:56,829 --> 00:01:01,537 The path and the domain. 13 00:01:09,959 --> 00:01:14,850 The last part is important as this is what tells the cookie where it lives. 14 00:01:14,850 --> 00:01:17,360 Without it, it will live in this session and 15 00:01:17,360 --> 00:01:20,540 may not always be available when you're looking for it. 16 00:01:20,540 --> 00:01:26,020 Once we have our access token, we want to redirect the user back with a cookie. 17 00:01:26,020 --> 00:01:29,560 We're going to modify our redirect function a little bit to allow for 18 00:01:29,560 --> 00:01:31,450 a cookie to be passed in. 19 00:01:31,450 --> 00:01:32,460 Open the functions file. 20 00:01:34,830 --> 00:01:36,315 Find the redirect function. 21 00:01:41,085 --> 00:01:45,029 We're going to add a new property that defaults to an empty array. 22 00:01:48,800 --> 00:01:52,110 This is so that we can expand in the future without too much work. 23 00:01:53,390 --> 00:01:56,100 After we set a variable for the response, and 24 00:01:56,100 --> 00:02:00,304 before we send it, we need to check for cookies inside of the extras array. 25 00:02:00,304 --> 00:02:05,194 If key exists. 26 00:02:05,194 --> 00:02:11,382 Cookies in extra. 27 00:02:14,335 --> 00:02:18,575 If it does, then we'll loop over each of the cookies so that we can set multiple 28 00:02:18,575 --> 00:02:22,065 cookies on the response headers using the SET cookie function. 29 00:02:22,065 --> 00:02:26,277 Foreach extra 30 00:02:26,277 --> 00:02:32,244 cookies as cookie. 31 00:02:36,722 --> 00:02:41,237 Response-> headers-> 32 00:02:41,237 --> 00:02:46,540 setCookie and pass the cookie. 33 00:02:49,911 --> 00:02:52,528 Now let's add the redirect with a cookie. 34 00:02:58,646 --> 00:03:02,243 Redirect to the home page and pass in the cookie. 35 00:03:15,916 --> 00:03:18,753 Let's modify our home page to read the cookie just so 36 00:03:18,753 --> 00:03:20,691 that we know that we're logged in. 37 00:03:25,474 --> 00:03:28,424 We need to require our bootstrap file first. 38 00:03:37,818 --> 00:03:39,591 And then if 39 00:03:39,591 --> 00:03:49,591 (request()->cookies->has('access_token'). 40 00:03:53,060 --> 00:03:55,292 Then we're just going to echo logged in. 41 00:03:59,676 --> 00:04:03,660 Let's take a look at what we have built so far, and log in to our system. 42 00:04:13,743 --> 00:04:18,658 Great, now that you know how to redirect a user with cookies set, give it a try and 43 00:04:18,658 --> 00:04:21,841 see if you can set the registration to automatically 44 00:04:21,841 --> 00:04:23,740 log a user in when they sign up.