#Sessions

1 messages · Page 1 of 1 (latest)

ashen mason
#

In nextauth using app directory, how do i get session on server page

trail inletBOT
#

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:


Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: