How to Implement Middleware in Remix? #1432
-
Its a bit sad if i need to add logic on each page to handle a page redirect if user is auth, or have proper permission. I came from laravel background , so im bit confuse on implementation on React / Node JS. Hope someone can share a bit of snippet or packages that can handle middleware for remix :) thanks |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments 33 replies
-
Instead of middleware, you can call a function directly inside the loader, this will also be more explicit. If you want to early return a response from those "middlewares" Remix let you throw the response object. For example, if you wanted to check the user has a certain role you could create this function: async function verifyUserRole(request: Request, expectedRole: string) {
let user = await getAuthenticatedUser(request); // somehow get the user
if (user.role === expectedRole) return user;
throw json({ message: "Forbidden" }, { status: 403 });
} And in any loader call it this way: let loader: LoaderFunction = async ({ request }) => {
let user = await verifyUserRole(request, "admin");
// code here will only run if user is an admin
// and you'll also get the user object at the same time
}; Another example could be to require HTTPS function requireHTTPS(request: Request) {
let url = new URL(request.url);
if (url.protocol === "https:") return;
url.protocol = "https:";
throw redirect(url.toString());
}
let loader: LoaderFunction = async ({ request }) => {
await requireHTTPS(request);
// run your loader (or action) code here
}; |
Beta Was this translation helpful? Give feedback.
-
Well, this makes kind of a pain not to be able to centralize maintenance of middlewares. There's a reason why they're so popular. |
Beta Was this translation helpful? Give feedback.
-
Consider that I am using the Remix App NodeJs Express Server. As we know that Express has numerous proven, stable middleware functions. After creating an instance of express, web apps typically have these middlewares in sequence:
How can I achieve this? Kindly guide. If Remix is giving me an option to deploy a Remix app on a server of my choice like AWS, NodeJS, Cloudfare Workers, etc, it should also allow me to use each and every bit of functionality that my chosen server offers. In my case, the incoming request should be allowed to go through a series of middleware. Pl. help me, this thing is really important before I decide to use Remix. |
Beta Was this translation helpful? Give feedback.
-
Question, maybe stupid, can't you add a global middleware in the root.tsx file inside the top-level loader or action? |
Beta Was this translation helpful? Give feedback.
-
For an express app, wouldn't you just add your middleware in the server.js file where all the other middleware is already being set up (such as morgan). I haven't actually tried this yet, but it seemed like the obvious place. |
Beta Was this translation helpful? Give feedback.
Instead of middleware, you can call a function directly inside the loader, this will also be more explicit. If you want to early return a response from those "middlewares" Remix let you throw the response object.
For example, if you wanted to check the user has a certain role you could create this function:
And in any loader call it this way: