I keep getting this error.
Tried for a few hours now and had no luck, I got a feeling that it’s the placement of the _app.tsx that’s messing the project up.
[next-auth]: `useSession` must be wrapped in a <SessionProvider />
This is how my file tree looks like
Navbar.tsx
"use client"
import React from 'react'
import Link from 'next/link'
import { NavLinks } from '@/constants'
import { signIn, signOut, useSession } from 'next-auth/react'
const Navbar = () => {
const { data: session, status } = useSession()
return (
<nav className="flex items-center justify-between p-6 lg:px-8" aria-label="Global">
<div className="hidden lg:flex lg:flex-1 lg:justify-end">
{status === 'authenticated' ? (
<a href="#" onClick={() => signOut()} className="text-sm font-semibold leading-6 text-gray-900">Sign out {session.user?.email}<span aria-hidden="true">→</span></a>
) : (
<a href="#" onClick={() => signIn('spotify')} className="text-sm font-semibold leading-6 text-gray-900">Log in <span aria-hidden="true">→</span></a>
)}
</div>
</nav>
)
}
export default Navbar
_app.tsx
import { SessionProvider } from "next-auth/react"
import type { AppProps } from "next/app"
import type { Session } from "next-auth"
import "./globals.css"
export default function App({ Component, pageProps: { session, ...pageProps }}:
AppProps<{ session: Session }>) {
return (
<SessionProvider session={session} refetchInterval={5 * 60}>
<Component {...pageProps} />
</SessionProvider>
)
}
Anyone out there who might know what I’m missing here?
2
Answers
Based on the error message and the provided code, it seems like you are using the Next.js
next-auth/react
package to handle authentication, and you are encountering the erroruseSession must be wrapped in a <SessionProvider />
.The error occurs because the
useSession
hook provided bynext-auth/react
requires aSessionProvider
higher up in the component tree to function properly. In your case, it seems you have correctly implemented theSessionProvider
in your_app.tsx
file. However, there might be an issue with how you import and use theSessionProvider
fromnext-auth/react
.Let’s go through a few troubleshooting steps:
Ensure that you have installed the necessary packages by running:
_app.tsx
:In your
_app.tsx
file, make sure you are importingSessionProvider
correctly fromnext-auth/react
. It should look like this:Check the
App
component in_app.tsx
:Verify that the
App
component is receiving thesession
prop correctly. In the provided code, it looks fine, but make sure there are no typos or mistakes.Check for syntax errors:
Ensure that there are no syntax errors in your code. The code you provided seems fine, but always double-check for any syntax mistakes.
Verify the project structure:
Make sure the
Navbar.tsx
and_app.tsx
files are located in the correct directories. They should be placed in thepages
directory at the root of your project.If you have checked all the above points and the issue persists, you can also try the following steps:
If the problem still persists, you might want to consider checking the Next.js and NextAuth.js documentation, as well as any GitHub issues related to the
next-auth
package. It’s possible that there might be a bug or breaking change that affects your setup.If you’re still unable to resolve the issue, consider providing more information, such as the versions of the
next-auth
andnext-auth/react
packages, any additional code related to authentication, or any relevant error logs.The reason you are getting this error is that you are using
app
directory which uses App Router a mechanism that allows you to use Server components (This was introduced with Next 13).Inside the
app
directory_app.tsx
will not be called.There are two ways to mitigate this issue
pages
directory and everything should be goodnext-auth
supports Next 13 and implement it accordinglyHope this helps!