Replies: 8 comments 10 replies
-
Exactly what I am looking for! Hope this gets implemented in the near future https://stackoverflow.com/questions/67512961/next-js-redirects-redirect-if-user-has-no-token |
Beta Was this translation helpful? Give feedback.
-
Just what I need. Arguably, I would prefer to have this solved at the config level (sub-path locale redirects) but I figured one could achieve it with Can definitely see this being useful elsewhere too |
Beta Was this translation helpful? Give feedback.
-
Is this on the roadmap? |
Beta Was this translation helpful? Give feedback.
-
What are you doing, dear Vercel ? |
Beta Was this translation helpful? Give feedback.
-
#27431 (review) exciting news, it looks like this is on the team's radar. |
Beta Was this translation helpful? Give feedback.
-
In a meantime, anyone figured out a workaround for this (via next.config.js)? |
Beta Was this translation helpful? Give feedback.
-
Next 12 introduced the middleware that solved the issue of import { NextRequest, NextResponse } from 'next/server';
export function middleware(req: NextRequest) {
if (
req.nextUrl.pathname.match(/\/(account|admin|checkout|confirmation).*/) &&
!req.cookies.sessionid
) {
return NextResponse.redirect('/signin', 307);
}
} |
Beta Was this translation helpful? Give feedback.
-
Update: We've released a solution for adding conditional logic for redirects, rewrites, and headers with Middleware in Next.js 12. Since this allows you to write code, you're not longer constrained by the configuration file limitations which previously blocked doing a |
Beta Was this translation helpful? Give feedback.
-
Update: We've released a solution for adding conditional logic for redirects, rewrites, and headers with Middleware in Next.js 12. Since this allows you to write code, you're not longer constrained by the configuration file limitations which previously blocked doing a
hasNot
type key.Describe the feature you'd like to request
Next 10.2 introduced the
has
property, which is an incredible feature that enables a laundry list of feature requests (redirect authenticated users from / to /dashboard, getStaticProps on subdomains, the list goes on).Unfortunately, the number one feature on my list doesn't seem to be possible at the moment: (I caveat this by saying this might already be possible and I'm doing it wrong) I would love to be able to redirect or rewrite a route if the request does not have a cookie, header, host, or query. The most common use case for this would be to redirect a request for something like
/private/:path*
to/login?redirect=:path
but I'm sure there are plenty of other creative implementations that could be unlocked.Describe the solution you'd like
A hasNot¹ property for headers, redirects, and rewrites that acts like the hasProperty but in reverse. Given the above scenario, it would be great to be able to do something like:
¹ - This is awkward naming, but English kind of fails us here. I picked hasNot because it's a pretty clear and concise partner to has, and other frameworks have used it, even if they discarded it for something differently awkward.
Describe alternatives you've considered
My use case is pretty specifically around auth, so the alternatives I currently implement are either:
These are perfectly fine, but the DX of handling this at the config level would be amazing!
Beta Was this translation helpful? Give feedback.
All reactions