Over the past couple of releases, Next.js has made some impressive additions which have transformed the way I develop with it. One of my favourite ...
For further actions, you may consider blocking this person and/or reporting abuse
Love the simplicity. However, after I am successfully authenticated, my user session is not available inside my getServerSideProps function, so a 404 is always returned even after a successful authentication and redirect. Any thoughts on why:
const user = req.session.get("user");
would work in the sessions.js file but be unavailable in the getServerSideProps function?
HI, @tsm012. How did you solve the above issue? I am experiencing the same issue.
Create a util in
utils/cookies.js
Implement lookup logic
Attempt to access the session value
At this point, the session is empty e.g
{ }
In my case, different
cookieNames
property.because i just copied and paste code above.
Returning a 404 for an unauthenticated user - surely this would be a 401/403. In your demo, SSR would just show a 404 which doesn't make any sense.
redirecting/or using res.end within getServerSideProps will result in the error: 'ERR_HTTP_HEADERS_SENT' so don't see how that works either.
It's quite common to return a 404 on protected endpoints for an unauthed user - it makes it harder to "profile" an application from the outside. Github do this, for instance. Feel free to implement differently though, for instance redirecting to the sign in screen - it's just a demo afterall...
I'm using
res.statusCode
,res.send
insidegetServerSidePros
without issue; are you basing your assumption on a statically compiled next app?Thanks for the reply Chris.
There is an RFC for this issue. Take a look at the codesandbox demo and you will see
Maybe this is handled somehow in next-iron-session
Thanks Samuel - that is very bizarre! I'm using this exact code successfully in a project at the moment, but you're right - it is an issue in the codesandbox demo... I'll do some digging.
It seems to be an RFC at the moment and would be a great solution to be able to set headers - which could allow a redirect in
getServerSidePros
which would be great.Currently, I have to use
getInitialProps
in a HOC and check if we SSR/client and do something like:Is it possible to use
withIronSession
in_app.jsx
and pass thesession:true|false
topageProps
to wrap around each and every possible page in an easier way? That being said, it'd be inside thefunction App({ Component, pageProps }){...
. Please tell me "YES", I need this answer and how :')Very great, simple and useful example, but am not able to understand that how we could write a decorator/HoC that wraps getServerSideProps and performs the session validation in a reusable way, can anyone here give an example of that related to this example here.
This is great, thanks Chris! And thanks for the introduction to
next-iron-session
Yes succinct and too the points ;) more words could be cutdown at what is it explanation blocks. :)