Next.js Discord

Discord Forum

How to add provider

Unanswered
Atlantic mackerel posted this in #help-forum
Open in Discord
Atlantic mackerelOP
I am trying to add next-auth

I tried this :
NextAuthProvider prop
export const NextAuthProvider = ({children}: { children: React.ReactNode }) => {
    const {data: session, status} = useSession();
    console.log(status)
    return <SessionProvider session={session}>{children}</SessionProvider>;
};


export function MainCookieProvider({
                                       children
                                   }: {
    children: React.ReactNode
}) {
    const [cookies, setCookie, removeCookie] = useCookies(["user"]);
    const [user, setUser] = useState<User>(cookies.user || null)
    const ClientProviderOverride = ClientProvider as any;
    const {data: session, status} = useSession()

    return (
        <NextAuthProvider>
            {
                status === "authenticated" ?
                    <ThemeProvider
                        attribute="class"
                        enableSystem
                        disableTransitionOnChange
                    >
                        <ClientProviderOverride>
                            {children}
                        </ClientProviderOverride>
                    </ThemeProvider> :
                    <MainLayoutProvider>
                        {children}
                    </MainLayoutProvider>
            }
        </NextAuthProvider>
    )
}

But the error said:

Error: [next-auth]: `useSession` must be wrapped in a <SessionProvider />

2 Replies

Japanese common catfish
You can find the answer in this article
https://medium.com/ascentic-technology/authentication-with-next-js-13-and-next-auth-9c69d55d6bfd

Read from "Access the session in a client side component" in the article