Authentication and getStaticProps in Next.js #11916
Replies: 5 comments 1 reply
-
You could try creating a wrapper for There's an Next steps might be to write a babel plugin that always wraps the |
Beta Was this translation helpful? Give feedback.
-
maybe this can work for you: #11334 (reply in thread) |
Beta Was this translation helpful? Give feedback.
-
I wrote down a detailed guide on what we do for vercel.com/dashboard here: #10724 |
Beta Was this translation helpful? Give feedback.
-
I'm having the same issue. Authentication and next.js is weirdly difficult |
Beta Was this translation helpful? Give feedback.
-
@jkjustjoshing maybe this can help you I am doing something like this export async function getServerSideProps({ req, res, query }) {
const contentTypeDefinition = getContentTypeDefinition(query.type)
// This content does not exist in db
if (!contentTypeDefinition) {
res.writeHead(302, { Location: '/404' })
res.end()
return
}
// Connect to DB (this could be a middleware)
await connect()
try {
// Run permission check
await runMiddleware(req, res, hasPermissionsForContent(query.type))
} catch (e) {
// User can not access
res.writeHead(302, { Location: '/404' })
res.end()
return
} |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I'm forcing authentication in my
getInitialProps
function in_app.js
. If auth token doesn't exist, I stop rendering the page and do a 302 redirect to the login page.I'm trying out
getStaticProps
, and this prevents my_app.js
getInitialProps
function from running.Besides running a custom Express server wrapping my Next.js app, is there a way to take advantage of
getStaticProps
but also prevent access for unauthenticated users?Beta Was this translation helpful? Give feedback.
All reactions