Error: [next-auth]: `useSession` 必须包裹在 <SessionProvider /> 内部。

huangapple go评论72阅读模式
英文:

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

问题

I am getting the below error when I am loading my ProfilePage. Page loads fine in the browser, but I get this error in the terminal.

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

All the children are wrapped using SessionProvider.

Below is my layout.jsx file.

import Provider from './components/Provider'
import Nav from './components/Nav'

import './globals.css'
import { Inter } from 'next/font/google'

const inter = Inter({ subsets: ['latin'] })

const RootLayout = ({ children }) => {
  return (
    <html lang="en">
      <body className={inter.className}>
        <Provider>
          <div className="main">
            <div className="gradient" />
          </div>

          <main className="app">
            <Nav />
            {children}
          </main>
        </Provider>
      </body>
    </html>
  )
}

export default RootLayout

Below is my Provider.jsx component.

'use client'

import { SessionProvider } from 'next-auth/react'

const Provider = ({ children }) => {
  return <SessionProvider>{children}</SessionProvider>
}

export default Provider

Below is my page.jsx for the ProfilePage.

'use client'

import { useSession } from 'next-auth/react'

const ProfilePage = () => {
  const { data: session } = useSession({
    required: true,
  })

  return (
    <section>
      <h1 className="head_text text-center">Profile</h1>
      <p>You are logged in as, {session?.user?.email}</p>
    </section>
  )
}

export default ProfilePage

Any help is appreciated.

Thank you.

英文:

I am getting the below error when I am loading my ProfilePage. Page loads fine in the browser, but I get this error in the terminal.

Error: [next-auth]: `useSession` must be wrapped in a &lt;SessionProvider /&gt;

All the children are wrapped using SessionProvider.

Below is my layout.jsx file.

import Provider from &#39;./components/Provider&#39;
import Nav from &#39;./components/Nav&#39;

import &#39;./globals.css&#39;
import { Inter } from &#39;next/font/google&#39;

const inter = Inter({ subsets: [&#39;latin&#39;] })

const RootLayout = ({ children }) =&gt; {
  return (
    &lt;html lang=&quot;en&quot;&gt;
      &lt;body className={inter.className}&gt;
        &lt;Provider&gt;
          &lt;div className=&quot;main&quot;&gt;
            &lt;div className=&quot;gradient&quot; /&gt;
          &lt;/div&gt;

          &lt;main className=&quot;app&quot;&gt;
            &lt;Nav /&gt;
            {children}
          &lt;/main&gt;
        &lt;/Provider&gt;
      &lt;/body&gt;
    &lt;/html&gt;
  )
}

export default RootLayout

Below is my Provider.jsx component.

&#39;use client&#39;

import { SessionProvider } from &#39;next-auth/react&#39;

const Provider = ({ children }) =&gt; {
  return &lt;SessionProvider&gt;{children}&lt;/SessionProvider&gt;
}

export default Provider

Below is my page.jsx for the ProfilePage.

&#39;use client&#39;

import { useSession } from &#39;next-auth/react&#39;

const ProfilePage = () =&gt; {
  const { data: session } = useSession({
    required: true,
  })

  return (
    &lt;section&gt;
      &lt;h1 className=&quot;head_text text-center&quot;&gt;Profile&lt;/h1&gt;
      &lt;p&gt;You are logged in as, {session?.user?.email}&lt;/p&gt;
    &lt;/section&gt;
  )
}

export default ProfilePage

Any help is appreciated.

Thank you.

答案1

得分: 4

我通过清除 .next 文件夹缓存成功解决了这个问题。

英文:

I was able to fix the issue by clearing .next folder cache.

huangapple
  • 本文由 发表于 2023年5月14日 20:23:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/76247463.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定