Created
August 22, 2022 08:21
Auth of next-auth
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// pages/index.js | |
import Login from '../components/login'; | |
import { getProviders } from "next-auth/react" | |
import { unstable_getServerSession } from "next-auth/next" | |
import { authOptions } from "./api/auth/[...nextauth]"; | |
export default function Home({ posts, Session , providers }) { | |
// check status is looding or not | |
if (status === "loading") { | |
return <h2> Looding </h2> | |
} else if (!Session) { | |
// if session is not avilable the render login page | |
return <Login providers={providers} /> | |
} else if (Session) { | |
// main component | |
return <Main /> | |
} | |
} | |
export async function getServerSideProps(context) { | |
// get serve side session | |
const session = await unstable_getServerSession(context.req, context.res, authOptions) | |
// get provider from next-auth | |
const providers = await getProviders() | |
return { | |
props: { | |
Session, | |
providers, | |
... | |
}, | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment