Replies: 5 comments 6 replies
-
I have also tried to get this to work. As far as I can understand, a middleware function in Next.js is not like middleware in Express or other frameworks. It seems to be more of a preprocess tool that can only use standard Web APIs, i.e. it can work on headers, cookies, do a redirect, or rewrite. But it still is only beta so I guess some stuff might still change :-) |
Beta Was this translation helpful? Give feedback.
-
Maybe you could do a rewrite to a route with query parameters? So your middleware rewrites to the route it was accessing + I do think it's not meant to be used for this. You could always just create a function to handle the sessions and include it in all |
Beta Was this translation helpful? Give feedback.
-
I do hope something will come up in the future, because this is exactly what I'm looking for!! I'm using Next + Supabase, and would like to have access to user/profile info within The idea would be to move the following logic to a middleware to avoid repetition accross the pages to protect: export const getServerSideProps: GetServerSideProps = async ({ req }) => {
const { user } = await supabase.auth.api.getUserByCookie(req);
if (!user) {
return { props: {}, redirect: { destination: '/signin', permanent: false } };
}
const { data: profiles, error } = await supabase.from("profiles").select().eq("user_id", user.id);
if (error) {
console.error(error);
return {
props: { code: 'err_get_profile', error },
redirect: { destination: '/error', permanent: false }
};
}
if (profiles === null) {
return {
props: { code: 'profile_is_null' },
redirect: { destination: '/error', permanent: false }
};
} else if (profiles.length !== 1) {
return {
props: { code: 'too_many_profiles' },
redirect: { destination: '/error', permanent: false }
};
}
return {
props: {
user,
profile: profiles[0],
}
};
} |
Beta Was this translation helpful? Give feedback.
-
@binajmen
After that, you can access it in any page via getInitialProps or
In case of any question, feel free to ask me.... |
Beta Was this translation helpful? Give feedback.
-
This is a big problem that I hope next.js can resolve in coming updates. The way I achieve this was using the NextResponse.rewrite() method. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
My use case is this:
In every
getServerSideProps
I need to call a specific (session) API and pass the result to the browser. When I heard of middlewares I immediately though I could just do that in those instead, and thus avoid the duplication. So I thought I could just call my api in the outermost "_middleware" function, and pass the result to each subsequentgetServerSideProps
function.The problem:
Having looked into middlewares, I can't figure out a way to pass the result in the middleware to subsequent
getServerSideProps
calls. Presumably I could just do something like:But this does not work as I hoped.
Is there another way to achieve this? Or am I misunderstanding the intention with middlewares?
Thanks!
Beta Was this translation helpful? Give feedback.
All reactions