无法接收使用 NextAuth 的附加参数

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

Can not receive additional parameter using NextAuth

问题

我在我的NextJS 13应用程序中使用NextAuth。最初,当用户经过身份验证时,会话返回三个参数:nameemailpicture。我的目标是添加一个额外的参数progress,该参数将在用户的工作过程中进行更新。

由于我将注册用户保存在MongoDB中,我使用NextJS Api Route通过PUT请求来更新用户并添加progress参数。但问题是,尽管添加了额外的参数,会话仍然返回相同的三个参数:nameemailpicture,而没有progress

问题是如何强制会话返回额外的参数?

附言:与此同时,我不确定通过API而不是通过NextAuth本身尝试添加额外参数是否正确。

英文:

I'm using NextAuth in my NextJS 13 app. Initially, when the user is authenticated, session returns three parameters: name, email and picture. My goal is to add an additional parameter progress that will be updated during the user's work.

As I keep the registered user in MongoDB I use NextJS Api Route to update the user and add progress parameter by PUT request. But the problem is despite that additional parameter was added, the session keeps returning the same three parameters: name, email and picture without progress.

The question is how to force the session to return additional parameter?

PS. At the same time I'm not sure that I'm doing it right by trying to add additional parameter via API instead of NextAuth itself 🤔

答案1

得分: 0

要扩展 NextAuth 的会话对象,您需要修改会话回调如下:

async session({ session, user }) {
    if (session.user) {
      session.user.id = user.id;
      session.user.progress = user.progress;
    }
    return session;
}

在扩展对象后,您还需要扩展类型,如下所示:

import type { DefaultSession } from "next-auth";

declare module "next-auth" {
  interface Session {
    user?: {
      id: string;
      progress: string;
    } & DefaultSession["user"];
  }
  interface DefaultUser {
    progress: string;
  }
}

更多信息:https://next-auth.js.org/configuration/callbacks

英文:

To extend the session object for NextAuth you have to modify the session callback like so:

async session({ session, user }) {
    if (session.user) {
      session.user.id = user.id;
      session.user.progress = user.progress;
    }
    return session;
  },

After extending the object, you need to extend the types aswell.

Like so:

import type { DefaultSession } from "next-auth";

declare module "next-auth" {
  interface Session {
    user?: {
      id: string;
      progress: string;
    } & DefaultSession["user"];
  }
  interface DefaultUser {
    progress: string;
  }
}

More info: https://next-auth.js.org/configuration/callbacks

答案2

得分: 0

在我的情况下,我正在使用 NextAuth 中的 OAuth 和 JWT。我还必须使用 JWT 将新变量传递给会话回调:

callbacks: {
  async jwt({ session, user }) {
    // 检查OAuth账户是否存在,表示首次登录
    if (account) {
      // 从数据库获取附加信息的API调用
      response = ...

      token.progress = response.progress;
    }

    // 在会话回调运行之前继续返回令牌
    return token;
  },
  async session({ session, token }) {
    if (token) {
      session.error = token.error;
      session.user.progress = token.progress;
    }
    return session;
  }
}
英文:

In my situation, I am using Oauth and jwt in NextAuth. I had to also use the jwt to pass the new variables to the session callback:

callbacks: {
  async jwt({ session, user }) {
  // check if oauth account exists, indicating first sign in
    if (account) {
      // your api call for additional information from db
      response = ...

      token.progress = response.progress;
    }

    // continue to return token before the session callback runs
    return token;
  },
  async session({ session, token }) {
    if (token) {
      session.error = token.error;
      session.user.progress = token.progress;
    }
    return session;
}

huangapple
  • 本文由 发表于 2023年2月10日 10:14:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/75406322.html
匿名

发表评论

匿名网友

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

确定