Files
archived-next-auth/apps/examples/nextjs/pages/server.tsx
Balázs Orbán 9f99066b19 chore: update NextAuth.js example to use App Router (#8477)
* move initiaization to App Router

* add more providers

* don't use default export

* update tsconfig

* move under api

* add more providers

* add issuer

* add issuer
2023-09-03 11:59:58 +01:00

40 lines
1.3 KiB
TypeScript

import { auth } from "auth"
import Layout from "../components/layout"
import type { GetServerSidePropsContext } from "next"
import { useSession } from "next-auth/react"
export default function ServerSidePage() {
const { data: session } = useSession()
// As this page uses Server Side Rendering, the `session` will be already
// populated on render without needing to go through a loading stage.
return (
<Layout>
<h1>Server Side Rendering</h1>
<p>
This page uses the <strong>getServerSession()</strong> method in{" "}
<strong>getServerSideProps()</strong>.
</p>
<p>
Using <strong>getServerSession()</strong> in{" "}
<strong>getServerSideProps()</strong> is the recommended approach if you
need to support Server Side Rendering with authentication.
</p>
<p>
The advantage of Server Side Rendering is this page does not require
client side JavaScript.
</p>
<p>
The disadvantage of Server Side Rendering is that this page is slower to
render.
</p>
<pre>{JSON.stringify(session, null, 2)}</pre>
</Layout>
)
}
// Export the `session` prop to use sessions with Server Side Rendering
export async function getServerSideProps(context: GetServerSidePropsContext) {
return { props: { session: await auth(context.req, context.res) } }
}