English 中文(简体)
我需要帮助向会议发送习俗信息。 用户物体使用下层
原标题:I need help sending custom information to session.user object using next-auth

试图在会议上增加习俗数据。 用户,但其工作。 奇怪的是,如果我发出:

const user = { id:  1 , name: "J Smith", email: "[email protected]", };

然后,我收到了我的网页上的信息。 jsx使用Session,并在 con中印刷:

const { data: session } = useSession(); console.log("in form session: ", session);

但问题是,在我试图向用户增加习俗信息时:

const user = { userId: res.data.data.login.userId, companyId: res.data.data.login.companyId, };

只有在我使用假冒、姓名和电子邮件时,它才能在没有习惯信息的情况下用空出用户物体。

这是我的全面法典:

import NextAuth from "next-auth";
import CredentialsProvider from "next-auth/providers/credentials";
import axios from "axios";

const handler = NextAuth({
  providers: [
    CredentialsProvider({
      name: "Credentials",
      credentials: {
        email: {
          label: "Email",
          type: "email",
          placeholder: "Enter your email",
        },
        password: { label: "Password", type: "password" },
      },
      async authorize(credentials, req) {
        const res = await axios({
          method: "POST",
          url: `${process.env.NEXT_PUBLIC_AUTH_URL}/api`,
          data: {
            query: `
              query Login($email: String!, $password: String!) {
                login(email: $email, password: $password) {
                  userId
                  token
                  tokenExpiration
                  companyId
                }
              }
            `,
            variables: {
              email: credentials.email,
              password: credentials.password,
            },
          },
        });
        // Validation
        if (res.data.errors) {
          throw new Error(res.data.errors[0].message);
        }
        const user = {
          userId: res.data.data.login.userId,
          companyId: res.data.data.login.companyId,
        };
        if (user) {
          return user;
        }
        // // Return null if user data could not be retrieved
        return null;
      },
    }),
  ],
  session: {
    strategy: "jwt",
  },
  pages: {
    signIn: "/login",
    signOut: "/logout",
  },
  secret: process.env.NEXTAUTH_SECRET,
});

export { handler as GET, handler as POST };

Someone knows how to fix this? Thanks

最佳回答

列出从证书提供人那里接收用户的jwt打背和将暴露在客户中所需数据的会议反馈(例如<代码>使用<>>/代码>)

I already fix it, I post my answer here, maybe its help someone in the future. I was missing the jwt and session callback and the session callback. Because next-auth by default don t expose everything for security reasons.

Adding this fix it:

callbacks: {
  async jwt({ token, user }) {
    // Persist the OAuth access_token to the token right after signin
    if (user) {
      token.accessToken = user.token;
    }
    return token;
  },
  async session({ session, token, user }) {
    // Send properties to the client, like an access_token from a provider.
    session.accessToken = token.accessToken;
    return session;
  },
},

而这种挫折将暴露客户的象征性接触。

问题回答

HI in the case of typescript this is throwing an error.

法典:

callbacks: {
async signIn({ user, account, profile, email, credentials }) {
  return true;
},
async jwt({ token, user }) {
  if (user) {
    token.role = user.role;
    token.company_id = user.company_id;
    token.company_name = user.company_name;
    token.first_name = user.first_name;
    token.last_name = user.last_name;
  }
  return token;
},
async session({ session, user, token }) {
  session.user.role = token.role;
  session.user.company_id = token.company_id;
  session.user.company_name = token.company_name;
  session.user.first_name = token.first_name;
  session.user.last_name = token.last_name;
  return session;
},
  },

错误:

Property  role  does not exist on type  User | AdapterUser .
  Property  role  does not exist on type  User .ts(2339)




相关问题
拆除月度边界(实际大型日历)

采用大型反应日历的Im, 并想要清除包罗日历的边界。 以及如何改变日记栏和日记的颜色? 如何使其发挥作用?

表中未显示的元数据

我将元件定义为如下文所示,但是在我的剪辑中,它没有显示“当地:3000/#home”。 我的所有jsx档案都有“用户”bc。

NextJS 13 Data Fetching

Is there anyway of custom baseURL for their fetch method? For example in axios: const instance = axios.create({ baseURL: https://some-domain.com/api/ , timeout: 1000, headers: { X-Custom-Header ...

热门标签