将更多数据传递给 Next-auth 中的会话

Pass more data to session in Next-auth

我们正在我们的网站上进行 OTP 验证。因此,为了获得授权,访问者在输入中输入他的 phone 号码,我们向他发送一个 OPT 号码,他再次输入发送的 opt,如果匹配,我们向他发送他的帐户凭据(令牌,用户 ID)如果存在或者我们创建新的并且我们想使用 next-auth 在会话中保存该凭据。

这是我到目前为止的进展:

export default NextAuth({
  providers: [
    CredentialsProvider({
      credentials: {
        phoneNumber: { label: 'PhoneNumber', type: 'text' },
        code: { label: 'Code', type: 'text' },
        type: { label: 'Type', type: 'text' },
      },
      async authorize(credentials, req) {
        const user_needs = await requests.auth.signInEnterOtp(
          credentials.phoneNumber,
          credentials.code,
          credentials.type
        )
        return user_needs.ok ? true : null
      },
    }),
  ],
  callbacks: {
    async session({ session, user, token }) {
      return session
    },
  },
  secret: process.env.JWT_SECRET,
})

我需要在会话中保存 user_needs,但我如何将它通过 authorize 传递到 session

我尝试在 authorize 中返回 user_need 但它没有传递给 session 回调。

最后我是这样想出来的:

async authorize(credentials, req) {
 const res = fetchUserInfo(credentials.opt)
 if(res.ok) return {user: res.data}
 return null
},
callbacks: {
 async jwt({ token, user }) {
  return { ...token, ...user }
 },
  async session({ session, user, token }) {
  return token
 },
},