#Sessions
1 messages · Page 1 of 1 (latest)
Hi @ashen mason. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
In the app directory of Next.js, you can use the getServerSession function to retrieve session data on the server-side. This function is part of the next-auth library.
Here is an example of how you can use it:
import { getServerSession } from 'next-auth/react'
export async function getServerSideProps(context) {
const session = await getServerSession(context.req)
return {
props: { session },
}
}
In this example, getServerSession is used inside the getServerSideProps function to retrieve the session data from the request (context.req). The session data is then passed as a prop to the page component.
Please note that this function is only available on the server-side. If you need to access the session data on the client-side, you should use the useSession hook from next-auth/react.
For more information, you can refer to the NextAuth.js documentation (https://next-auth.js.org/configuration/nextjs#getserversession).
Sources:
- Github Issue (closed): Problem in server side execution in page dir > Comments (https://github.com/vercel/next.js/issues/56871)