2

我将 Next.js 与next-authand结合使用next-i18next

我使用useSession()next-auth/client在我的 _app.js 中使用 Provider)来检查用户是否经过身份验证。一旦通过身份验证,导航会非常快速,并且当前会话会在页面呈现之间保持。直到我切换语言环境。切换后,loading状态(来自useSession())切换到true

状态保持真实,loading直到我关注另一个浏览器选项卡并返回。

我希望会话也将保留在语言环境之间,就像它保留在页面导航之间一样......

我不知道为什么会发生这种情况,以及这种行为是否正常。

要切换语言环境,我使用:

<Link href={router.asPath} locale="en">EN</Link>
4

1 回答 1

0

我有同样的问题,尝试将 getSession 添加到 getServerSideProps。它对我有用。

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

    export async function getServerSideProps(context) {
      const session = await getSession(context)
      const authorized = !!session?.jwt
      return {
        props: { authorized },
      }
    }
于 2021-12-10T23:38:28.190 回答