Current section: Redirect Cookie 4 exercises

Handling Authentication Redirects and Cookies

Loading solution


00:00 Let's go to our AuthProviderRoute. That's the route that's going to handle this submission. We'll grab these utilities, we're going to need those. Then we're going to wrap these two in a try-catch, so that we can augment the response that's thrown. We're going to just re-throw the error, whatever it is.

00:18 But if that error is an instance of a response object, then we're going to be able to do a couple of extra things. So let's get the form data from await request form data. And then we're going to get a raw redirect

00:36 to from the form data dot get redirect to. And then we'll say our redirect to is type of raw redirect to. Whoops. If that is a string, then that's what we're going to do. Otherwise, we'll fall back to the get refer route.

00:56 So whoever is sending or making this request over here, wherever they were before, that's the referrer. And that's where we're going to set the redirect to, as just our fallback. So they can go right back to where they left off. And then we can generate our redirect cookie header from those utilities.

01:14 And if that exists, then we'll append a set cookie header for that redirect cookie header. And that will save it into the cookie. So with all that done, if we did it correct, then we should get a redirect to cookie header or cookie in our cookie.

01:31 So if I hit Login with GitHub, then that actually did work. We didn't actually go through the whole process. But we can take a look at our application. And we'll see the redirect to is right there. Now, once we've finished the whole process and we end up redirecting to the correct place, we don't want that cookie anymore. So the next step is going to not only redirect

01:51 to the correct place, but also get rid of this cookie as well. That is it. Well done.