It may seem surprising, but when a page component includes a function called getServerSideProps
, the properties within this function are expected to be passed as props to the page component itself. An example of this can be seen below:
export async function getServerSideProps(context) {
return {
props: { foo:'Hello!' },
}
}
export default function PageComponent(props) {
return <div>{ props.foo /* = "Hello!" */ }</div>;
}
The unexpected aspect is that these generated props
are actually passed to the _app.tsx
(or _app.jsx
) component as pageProps
.
In essence, the structure of the getServerSideProps
should follow a pattern similar to the one illustrated below:
import { unstable_getServerSession } from "next-auth/next"
import { authOptions } from "../api/auth/[...nextauth]"
export async function getServerSideProps({ req, res }) {
const session = await unstable_getServerSession(req, res, authOptions)
return {
props: {
session
}
};
}