英文:
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 "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>
</>
}
My [...nextauth.js] just returns some dummy data:
async authorize(credentials) {
console.log("authorize script running...");
const user = { id: 22, name: 'J Smith', email: 'jsmith@example.com' }
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: 'jSmith' name: 'J Smith', email: 'jsmith@example.com' }
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;
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论