Repository navigation
"secret" static pages, that are only accesible via an internal route #18832
Replies: 3 comments 3 replies
|
What you're asking for is not possible. Static files have to be public in order for the website to be accessible to your visitors. Static pages are also static files (HTML, css and js) that are created at build time. You shouldn't be trying to hide static files but data instead. E.g in a profile page you want to generate a static page that shows a loading state, and once an user enters that page, do a request to your API (and authenticate the request with a cookie) so it returns the private data of the user and replaces the loading state with his data. Note how the page is still static and public (a loading spinner is not a security issue). TL;DR: static pages have to be public or otherwise a browser can't fetch them. You should be securing your data, not your static files. |
|
Hi, recently I came up with the same issue. My problem is that I have some secret pages that only authenticated users are allowed to access. Iβm storing the session for security reasons as a cookie as http only. The pages itself are all static sites and I would let them static for performance. After thinking about the problem I came up with the following solution:
Depending of the features that should be available by this approach the following scenarios could be possible:
In the end this approach is just an optional middleware for the standard next server that will be compatible with static site optimisation. |
|
With Next.js 12, protected static sites are now possible out of the box with middlewares. |
Uh oh!
There was an error while loading. Please reload this page.
Hi everyone!
PROBLEM TO SOLVE
I would like to have a static page which checks for authentication before being shown to the client.
MY CURRENT KNOWLEDGE -> NEED FOR A SOLUTION
As much as I know, I could do authentication check in a SSR page, which would then in the server side do the authentication check with an external server. And afterwards the page would be built at the server side and sent to the client.
But I cannot do an authentication check in a static page, because the static page would be shown to the client at the first step, without any previous interaction to the user, like a check of a cookie for authentication purposes.
PROPOSED SOLUTION
It would be nice to combine the two powers of SSR and Static Page, by having a combination of boths. I try to show it in the following pictures.
The user types a URL, then in the server there is first an Auth check, and if it succeeds then the user is redirected to the Static Page.
PROBLEMS TO SOLVE IN THE PROPOSED SOLUTION
1- User trying to access directly to the "secret" URL
If the user types the "secret" URL of the page, then NextJs should not show that page to the client, but redirect to a predefined page. That means that the NextJs shall know about the "secret" static page.
After redirecting to the predefined page, the normal situation would happen, where there is an Auth check and afterwards the redirection to the Static Page.
DISCUSSION
Does it make sense what I have been thinking?
Is it possible to solve the proposed problem with the current state of NextJs?
Many thanks for your comments
Diego
All reactions