Transcript
-
Since the navigation is used on every page that we render with our application, we're gonna need the authenticated user on almost every request that our server receives. So it would be great to go ahead and just check for
-
that authenticated user out the get go using a middleware. So let's move this auth check out of our view and into a router level middleware.
-
Because if we haven't found a route specifically for the user, then we don't care whether or not the authenticated user is there or not. So we don't care about it at the server level, but it would be nice to have this
-
automatically happen for every route that we do have defined. So we can do it at the router level so that it runs for every route that we have registered. So let's hide our text editor away, jump into our terminal, and
-
let's stop our server, clear that out. And let's create a new middleware with node ace make middleware. We'll keep with the naming convention from AdonisJS 5 and call this our silent auth middleware.
-
Hit Enter there. It's gonna ask us which stack we wanna register this middleware for. As we just discussed, we're wanting it for our router since we don't really care about whether or not we have an authenticated user at the server level,
-
where we may or may not have a route matching. So we'll create this at our router level, and there we go. So now we have a silent auth middleware registered. And since we've made this with the acli,
-
it's added it to that start kernels file for our registered route middleware. Let's boot our server back up while we're here and hide this away, jump back into our text editor.
-
And while we're here, let's switch this back to just checking if we have an auth user. With that change, let's dive into our new silent auth middleware. Here we see our brand new middleware,
-
looks exactly like the others that we've looked at so far. We have our next call right here. And since we want to check for the authenticated user and have that be prepared for our route handler,
-
we're going to want to perform that check before that next function call. So we can get rid of this console log right here, and we're provided our CTX or our HTTP context as our parameters.
-
We can do await CTX auth and call that check method here. Give that a save, and that's all that we should need to do there. Remember, we've just changed this back to just checking auth user.
-
So with our silent auth now making that check globally for all registered routes that we have inside of our application. If we dive back into our browser at this point,
-
it just refreshed itself and we still have our auth user. If we dive into our writers, we still have our auth user. Directors still have our auth user. A specific director, it's still there.
-
So it's checking it for each and every route that we have.
Checking For and Populating an Authenticated User
We'll create a silent auth middleware that will automatically check whether a request has an authenticated user attached to it, and populate that user's details if one is found.
Join the Discussion 0 comments
Be the first to comment!