如何在NextAuth中访问session.user.id?

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

How do you access session.user.id in nextauth?

问题

我正在使用 next-auth 版本 4.19.2。

我有一个简单的组件,只显示已登录用户的信息:

import { useSession, signIn, signOut } from "next-auth/react"

export default function LoginPlaceholder() {
  const { data: session } = useSession();

  if(session) {
    return <>
      Signed in as 
        userID: {session.user.id} <br/>
        name:  {session.user.name} <br/>
        email:  {session.user.email} <br/>
      <button onClick={() => signOut()}>Sign out</button>
    </>
  }
  return <>
    Not signed in <br/>
    <button onClick={() => signIn()}>Sign in</button>
  </>
}

我的 [...nextauth.js] 只返回一些虚拟数据:

async authorize(credentials) {
  console.log("authorize script running...");

  const user = { id: 22, name: 'J Smith', email: 'jsmith@example.com' }
  return user;
}

由于某种原因,我的输出只显示了姓名和电子邮件,但没有显示用户ID。

Signed in as userID:
name: J Smith
email: jsmith@example.com

我正在开发一个项目,组织中的多人可以共享相同的电子邮件地址(不是我的主意),因此电子邮件对我来说不是唯一的标识符。我需要用户ID或用户名作为唯一标识符,但我不确定如何获取它。

我还尝试传递其他数据,但我只能让姓名和电子邮件显示出来,没有其他值。

const user = { id: 22, userName: 'jSmith', name: 'J Smith', email: 'jsmith@example.com' }

但同样,我的组件没有获取到除了姓名和电子邮件之外的任何值。

我的问题是:如何修改我的代码以显示用户名?

英文:

I am using next-auth version 4.19.2 .

I have a simple component that just displays who they're logged in as:

import { useSession, signIn, signOut } from &quot;next-auth/react&quot;

export default function LoginPlaceholder() {
  const { data: session } = useSession();

  if(session) {
    return &lt;&gt;
    
      Signed in as 
        userID: {session.user.id} &lt;br/&gt;
        name:  {session.user.name} &lt;br/&gt;
        email:  {session.user.email} &lt;br/&gt;
        
      &lt;button onClick={() =&gt; signOut()}&gt;Sign out&lt;/button&gt;
    &lt;/&gt;
  }
  return &lt;&gt;
    Not signed in &lt;br/&gt;
    &lt;button onClick={() =&gt; signIn()}&gt;Sign in&lt;/button&gt;
  &lt;/&gt;
}

My [...nextauth.js] just returns some dummy data:

      async authorize(credentials) {

        console.log(&quot;authorize script running...&quot;);

        const user = { id: 22, name: &#39;J Smith&#39;, email: &#39;jsmith@example.com&#39; }
        return user;
      }

For some reason, my output only displays the name and email, but no id.

>Signed in as userID:<br/>
>name: J Smith<br/>
>email: jsmith@example.com

I work on a project, where multiple people in an organization could share the same email address (not my idea), so email is not a unique identifier for me. I need the either the ID or the username, as a unique identifier, but I was unsure how to get that.

I also tried passing other data, but the only thing I can get to show up is name and email.

const user = { id: 22, userName: &#39;jSmith&#39; name: &#39;J Smith&#39;, email: &#39;jsmith@example.com&#39; }

But again, my components are not getting any values for anything other than name and email.

My question is: How do I modify my code to display the username?

答案1

得分: 1

你应该使用回调函数,大致如下所示:

    callbacks: {
        async jwt({token, user}) {
      
           if (user?.id) {
               token.id = user.id
           }
           if (user?.userName) {
               token.userName = user.userName;
           }
           return token
        },
        async session({session, token}) {
            session.id = token.id;
            session.userName = token.userName;
            return session;
        }
      }
英文:

You should use callbacks, something along these lines:

    callbacks: {
        async jwt({token, user}) {
      
           if (user?.id) {
               token.id = user.id
           }
           if (user?.userName) {
               token.userName = user.userName;
           }
           return token
        },
        async session({session, token}) {
            session.id = token.id;
            session.userName = token.userName;
            return session;
        }
      }

huangapple
  • 本文由 发表于 2023年2月16日 04:30:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/75465165.html
匿名

发表评论

匿名网友

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

确定