iTranslated by AI
Beware of __NEXT_DATA__ when using NextAuth.js with getServerSideProps or getStaticProps
This is a reminder to be careful when using client-side login with libraries like next-auth: if you don't properly implement authentication within getServerSideProps or getStaticProps, your data will be exposed.
While this is common sense if you are used to traditional server-side development, it can be a dangerous pitfall if you use it without knowing, so I'm documenting it here.
Example
For example, let's consider the following code.
Usually, login checks on the client side are written in _app.js.
// _app.jsx
import { signIn, useSession } from "next-auth/client"
export const NeedLogin = ({children}) => {
const [session, loading] = useSession()
if (loading) {
return null
}
if (!session ) {
return <div>
Not signed in <br />
<button onClick={signIn}>Sign in</button>
</div>
}
return children
}
function MyApp({ Component, pageProps }) {
return (
<Provider options={{ clientMaxAge: 60 }} session={pageProps.session}>
<NeedLogin>
<Component {...pageProps} />
</NeedLogin>
</Provider>
)
}
export default MyApp
And let's write the page side like this:
// pages/somePage.jsx
import React from "react"
import { useSession } from "next-auth/client"
import { getSomeSecretData } from "../lib/getSomeData"
export default function Page({data}) {
const [, loading] = useSession()
if (loading) {
return null
}
return (
<div>
Home
<pre>Need Login: {JSON.stringify(data)}</pre>
</div>
)
}
export async function getServerSideProps() {
const data = getSomeSecretData() // For example, assume this is data you only want to show after logging in
return {
props: { data },
}
}
Then, before and after logging in, it should be displayed like this:
| Before Login | After Login |
|---|---|
![]() |
![]() |
And they lived happily ever after? No, not quite.
Let's take a look at the inspector on the page before logging in.

<script id="__NEXT_DATA__" type="application/json">{"props":{"pageProps":{"data":["This","Data","Need","Login"]},"__N_SSP":true},"page":"/","query":{},"buildId":"development","isFallback":false,"gssp":true}</script>
As you can see, the value from getServerSideProps is displayed within __NEXT_DATA__.
How to handle it
When using getServerSideProps, perform a check using getSession.
import { useSession,getSession } from "next-auth/client"
export async function getServerSideProps(context) {
const session = await getSession(context) // Don't forget the await
if (!session) {
return { props: {} }
}
const data = getSomeSecretData()
return {
props: { data },
}
}
By doing this, it won't be displayed in __NEXT_DATA__ either. [1]
[1]: Note that if you forget await, session becomes a Promise and will pass the if(!session) check.

<script id="__NEXT_DATA__" type="application/json">{"props":{"pageProps":{},"__N_SSP":true},"page":"/","query":{},"buildId":"development","isFallback":false,"gssp":true}</script>
Also, in the case of getStaticProps, you cannot use this method because you cannot obtain the request context. Since getStaticProps originally doesn't include use cases like requiring a login, if you absolutely need to use getStaticProps, it's better to fetch the data via an API or similar.
In the case of an API, you can use getSession in the same way as described above.


Discussion