skip to Main Content

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 />

enter image description here

This is how my file tree looks like

tree folder view

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">&rarr;</span></a>
                ) : (
                    <a href="#" onClick={() => signIn('spotify')} className="text-sm font-semibold leading-6 text-gray-900">Log in <span aria-hidden="true">&rarr;</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


  1. 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 error useSession must be wrapped in a <SessionProvider />.

    The error occurs because the useSession hook provided by next-auth/react requires a SessionProvider higher up in the component tree to function properly. In your case, it seems you have correctly implemented the SessionProvider in your _app.tsx file. However, there might be an issue with how you import and use the SessionProvider from next-auth/react.

    Let’s go through a few troubleshooting steps:

    1. Make sure you have installed the required dependencies:
      Ensure that you have installed the necessary packages by running:

    npm install next-auth

    1. Check the import statement in _app.tsx:
      In your _app.tsx file, make sure you are importing SessionProvider correctly from next-auth/react. It should look like this:

    import { SessionProvider } from ‘next-auth/react’

    1. Check the App component in _app.tsx:
      Verify that the App component is receiving the session prop correctly. In the provided code, it looks fine, but make sure there are no typos or mistakes.

    2. 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.

    3. Verify the project structure:
      Make sure the Navbar.tsx and _app.tsx files are located in the correct directories. They should be placed in the pages 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:

    • Restart the development server: Sometimes, hot-reloading might cause unexpected issues. Restart the development server to see if it resolves the problem.
    • Clear the browser cache: Clear your browser cache to ensure you are not seeing a cached version of your app with an older configuration.

    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 and next-auth/react packages, any additional code related to authentication, or any relevant error logs.

    Login or Signup to reply.
  2. 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

    1. Use the old pages directory and everything should be good
    2. Check whether the next-auth supports Next 13 and implement it accordingly

    Hope this helps!

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search