Loading
Current section: Redirect Cookie 4 exercises
solution

Managing Redirects and Cookies

Loading solution

Transcript

00:00 So let's go to our callback. Woo, we're almost done. So we'll bring in these utilities, and then we'll just go top to bottom and deal with every one of these cases piece by piece. So we're gonna be using this object quite a bit, a set cookie header, so we'll just create an object for that. Then let's get the redirect value

00:19 or the redirect to value from get redirect cookie value, which comes out of the request. And then right here where we're redirecting with toast, that is a response. We need to add some headers. So headers and destroy a redirect to.

00:36 So we want to get rid of that out of our business. We don't want that. And then down, we go down a little bit further, we can add that here as well. Pretty much everywhere we have redirect with toast, we're gonna add this destroy redirect to header.

00:55 And right here as well, we've got headers and destroy redirect to. And then make session already supports a redirect to because we have a settings profile connections right there. So we're gonna send a redirect to, to that. And that takes care of that. And then we'll go down in there later

01:15 to take care of doing the destroy redirect to header. Okay, so continuing down here, if we have a redirect to defined, then we want to go there. Otherwise we can take them to the connections. So redirect to, otherwise we go to connections. And then of course we have our headers right here.

01:34 So we want to destroy that. We'll do that in the make session. So we don't need to worry about that right here. Okay, and then this is the little bit of a tricky part. So we've got our onboarding redirect right here. And we could just stick that like this and then add a query string,

01:55 but that just never, you want to make sure you're encoding things properly. And on top of that, if there is no redirect to, then I don't want a dangling question mark at the end of my URLs, that's ugly. And we care about user experience more than that. So what we're gonna do is turn this into an array and we'll join with a question mark,

02:12 but then we'll filter out Boolean or falsy stuff. And so what that allows us to do is we say redirect to, if that exists, then we'll do a new URL search params, redirect to, otherwise we'll do null. And that will get filtered out. And this is complaining because I forgot a comma.

02:32 There we go. Okay, and with that onboarding redirect, now we can say to string. And that will include the redirect to when they go to onboarding. So when they finish with that, then they can go where they're supposed to go. Okay, great. And so then for this, we need to destroy our redirect to. So we're gonna combine headers, combine headers.

02:56 That's that utility we've used a couple of times. And then we'll have our destroy redirect to. And that will take care of that. And then finally, in our make session. So this is what happens when the user logs in and everything. In here, we wanna combine response inits so that we pass the destroy redirect to headers.

03:14 So take this response init, combine response inits and pass that. And then we'll also pass headers destroy redirect to. So let's test this out, shall we? This is the moment we've all been waiting for. When I click on log in with GitHub, we should have one request that sets the redirect to cookie

03:33 and then another request that unsets it. So we go to log in with GitHub, we end up on the right place. That's exactly what we were looking for. And here's the post request that was made. This has the set header for our redirect to, for our cookie. And then if we come down here to this get request, and there's a set cookie right there,

03:51 redirect to max age minus one. So it gets booted. And we are in an awesome spot. So let's just review and then we can go home. Okay, so right up here at the top, we created our destroy redirect to, that will be our headers object for a bunch of places.

04:10 We get our redirect cookie value out of the cookie. And so then we can use that. We have our destroy redirect to header for pretty much everywhere we call redirect with toast. Anytime we send back a response, we're gonna destroy that redirect to so that it's not dangling around in our cookies. And then the more complicated

04:30 or one slightly more complicated bit was this make session, where we already were sending the user to the connections page. If we have a redirect to, we want to send them to that first. And then down here, as part of the onboarding, onboarding has a redirect to support. So we wanna add that redirect to query string. And so that's what we're doing there.

04:49 And then we destroy redirect to. And then for the more common case, when the user is just logging in, then we make a session. And here we combine the response nets to destroy the redirect to. And that is the whole kit and caboodle. Well done.