在NextJS中使用Auth0获取Session时出错

问题描述 投票:0回答:1

我正在使用 NextJS 的 App Router 版本,并且我正在尝试按照此身份验证系统的快速入门指南使用 Auth0 实现身份验证。

我完成了所有步骤,我的应用程序正在重定向到 Auth0 登录,并且它在用户登录后进行重定向,但是当我尝试获取会话信息时,我收到此错误:

Cannot destructure property 'user' of '(intermediate value)' as it is null.

我的页面很简单,看起来像这样

import { getSession } from '@auth0/nextjs-auth0';

export default async function ProfileServer() {
  const { user } = await getSession();

  return (
      user && (
          <div>
            <img src={user.picture} alt={user.name} />
            <h2>{user.name}</h2>
            <p>{user.email}</p>
          </div>
      )
  );
}

我无法通过快速入门的示例代码获得会话,因此我需要一些帮助:D

reactjs next.js auth0
1个回答
0
投票

您可以尝试如下:

    import { getSession } from '@auth0/nextjs-auth0';

    export default async function ProfileServer() {
      const sessionInfo = await getSession();
    
      return (
        sessionInfo?.user && (
          <div>
            <img src={sessionInfo?.user.picture} alt={sessionInfo?.user.name} />
            <h2>{sessionInfo?.user.name}</h2>
            <p>{sessionInfo?.user.email}</p>
          </div>
        )
      );
    }
© www.soinside.com 2019 - 2024. All rights reserved.