This episode is for members only

Sign up to access "Build a Starter Kit With Inertia and Fortify" right now.

Get started
Already a member? Sign in to continue
Playing
12. Redirecting to the intended location

Episodes

0%
Your progress
  • Total: 5h 21m
  • Played: 0m
  • Remaining: 5h 21m
Join or sign in to track your progress

Transcript

00:00
So redirecting to the intended location is a really important part of an app. Basically, when we go ahead and click on this dashboard, which we don't have protected with middleware at the moment,
00:09
if we're not authenticated, we want to show the login modal. Then, when we've entered our details in that login modal, we want to be redirected over to the dashboard. Now, we're going to come across an issue with some
00:21
of the middleware we already have defined in our Laravel app in a moment. We're going to change that and fix that up. But then I'm going to show you how this actually works.
00:29
And how it redirects to our intended location. And this is really important. Because later, we're going to want to change around some of the default responses
00:38
that get returned when we're working with Fortify. OK, so let's first of all go over to our dashboard controller and add in the middleware here to protect this. Of course, this needs to be an authenticated user.
00:51
And you can do this at the root level or within the controller. It doesn't matter. So we'll apply our middleware to here. And now, sure enough, when we click on dashboard,
00:59
we should not be allowed in. But what actually happens is we get a root login not defined error. Now, this isn't anything specific to Fortify.
01:08
It's just because over in our web routes, we have called this auth.login. And the default middleware within our project, if we just head over to HTTP middleware and authenticate,
01:21
expects to be redirected over to the root called login, which we don't have. So we're just going to change that over to auth.login. As simple as that, that is now going to work.
01:30
Now, because we've set up our modal functionality with this package which works with roots, what that means now is when we click dashboard, we're going to be forwarded over to auth.login,
01:40
and our modal is going to appear. So everything worked really nicely. Now, when I go ahead and sign in, you'll notice that we will be redirected over
01:50
to our dashboard. So everything is looking good. But what's going on behind the scenes here? How is this actually redirecting us over
01:57
to our intended location? Well, what we're going to do is do a little bit of source diving again. And we're going to go back over to that authenticated session
02:06
controller. And we're going to talk about these responses, which pretty much happen for every action we take within Fortify. Now, these are really clever because these are customizable.
02:17
We can, within the Laravel container, bind different responses based on certain actions. We're going to do that later. But I'm going to go over this now
02:26
so we've got a little bit of an idea of how this works. So we've already kind of looked at the login pipeline when we were diving in to try and find out that remember Boolean that we passed through.
02:37
But this is returning a login response. So what is this login response? Well, let's go ahead and just open this up. We've got a contract, which means
02:45
we can change this over within our container. So we can bind our own response to this if we want to to a class of our own. And this here, this file at the top,
02:56
is the actual login response. So this is part of the Fortify package. So we're not going to modify that directly, of course. But let's take a look.
03:03
So this, when it gets cast to a response, checks if we need JSON, which we don't because we're working with inertia. And otherwise, returns some data about two factor.
03:16
But otherwise, it redirects to the intended location. So this is pretty much what we want. Now, we're actually going to be modifying this response later when we get to flashing messages,
03:26
so when we show our host notification at the top of the page. But let's take a look at this Fortify redirects as well. So if we open up the base Fortify class in here,
03:36
so Fortify source, let's look at redirects. So this basically pulls from config and looks under our redirect section. Now, that might seem a little bit weird
03:46
because if we open up our Fortify config and we search for redirect, this doesn't exist in here. This is something that's kind of opt-in. You can add it yourself.
03:57
So for example, if you wanted to redirect to a specific location with any of the functionality that we have, you can add in that redirect there. And we might be doing that later depending on how we get on.
04:08
But I think it's useful to work out how this kind of stuff is working. So for almost every action that we take when we create a user account, when
04:16
we enable two factor authentication, all of that stuff, we always almost get returned a response in here, which we can directly modify. So later, if we needed to do something slightly
04:27
different after the user is logged in and we needed that response to behave a little bit differently, we can bind a new thing to the container. So we can bind a new class to the container
04:39
and implement our own login response. So that's basically what's happening. But now we've got that issue fixed up. And we know that this prompts the sign in.
04:49
We can go ahead and sign in. And because we've just looked at that login response, we know that this redirects to the intended location, which in this case is the dashboard.
40 episodes5 hrs 21 mins

Overview

Let’s build our own Laravel starter kit with Inertia and include all the features you’d expect. We’ll implement authentication, email verification, profile photo uploads, two factor authentication, and more.

Fortify is a frontend agnostic authentication package for Laravel, meaning most of the work is done for us — we just need to build the frontend and make requests to the endpoints that Fortify registers.

Once you’re done, you’ll have a clean slate for building your next project, with the ability to control everything in your own starter kit.

Alex Garrett-Smith
Alex Garrett-Smith
Hey, I'm the founder of Codecourse!

Episode discussion

No comments, yet. Be the first!