Replies: 4 comments 5 replies
-
Since routing is file-system based and each page is handled separately in the nextjs hybrid approach, I'd imagine the API may be better suited to live at the per-page level, similar to That being said, once #9081 lands, there may be some precedence/overlap for parts of this feature and global pattern you're describing. I'm curious how the maintainers view the "guards" idea in the greater scheme. |
Beta Was this translation helpful? Give feedback.
-
It seems that those questions, in a form or another, are recurring. You may be interested in:
Specifically, community is inventing all sorts of anti-pattern or (hopefully) valid patterns to circumvent the lack of a core feature that would allow you to check auth before accessing a route. |
Beta Was this translation helpful? Give feedback.
-
Your proposed solution is exactly what I am looking for. Has anyone found an elegant solution? |
Beta Was this translation helpful? Give feedback.
-
The new special files in Next 13 provide an opportunity here for implementing this cleanly, as well as enabling API routing and middleware within the new app dir. One could imagine a // app/api/foo/middleware.ts
import { NextResponse } from 'next/middleware'
export default function handler() {
return NextResponse.status(200)
} The rule could be that if the function returns a response, stop everything and just use that as the response. Otherwise, treat the function as a middleware and keep on rendering the page: import { NextRequest } from 'next/middleware'
export default function handler() {
if (NextRequest.headers('Authorization')) {
NextRequest.setCookie('token', NextRequest.headers('Authorization'))
}
} The benefits here would be:
|
Beta Was this translation helpful? Give feedback.
-
Feature request
Goals
In Next.js, any user can navigate anywhere in the application anytime. That's not always the right thing to do.
We need an abstraction to handle these use cases in a proper way.
Describe the solution you'd like
The router should have the capabilities to define such
guards
. In a guard, the current router information and the target path must be provided. Therefore we need access to the application state (_app props?).There are different types of guards:
guard
will fire on entering and leaving transitioncanLoadGuard
will fire only when the user navigates to a different page.canLeaveGuard
will fire only when the user leaves the current page.Describe alternatives you've considered
The current alternatives are:
Use a
HOC
for every page to act as a guard. While it works it is far from great:Look at the HOC below. We return
null
in case of an unauthenticated user. This will result in huge flickering because the page is not hydrated.Example
As an example, let watch Zeit.co dashboard. The issue was solved gracefully but it demonstrates the issue very well.
Following Next.js examples illustrate the need for such an abstraction:
Hard page refresh in fetch implementation. This will destroy all SPA benefits.
https://github.com/zeit/next.js/blob/46ecb00112b1d667c23399038874148f6eb80f49/examples/auth0/lib/user.js#L57
HOC with localStorage hack
https://github.com/zeit/next.js/blob/46ecb00112b1d667c23399038874148f6eb80f49/examples/with-cookie-auth-fauna/utils/auth.js#L16
https://github.com/zeit/next.js/blob/46ecb00112b1d667c23399038874148f6eb80f49/examples/with-cookie-auth/utils/auth.js#L34
Conditional rendering, same drawbacks like the HOC and not easy to adopt on many pages.
https://github.com/zeit/next.js/blob/46ecb00112b1d667c23399038874148f6eb80f49/examples/with-firebase-authentication/pages/index.js#L37
Problems to solve
Additional context
Related
Thanks!
Beta Was this translation helpful? Give feedback.
All reactions