next.js 我有一些问题得到下一个认证证书的返回数据,有人知道如何解决它?

omvjsjqw  于 2023-06-22  发布在  其他
关注(0)|答案(1)|浏览(70)

我正试着和下一个认证和云雀一起做一个证书认证。但是我不能得到凭证的返回值,所以有人知道我哪里出错了吗?
这是我的nextAuth文件

// [...nextauth.js]
import NextAuth from "next-auth";
import CredentialsProvider from "next-auth/providers/credentials";

export const authOptions = {
  secret: process.env.SECRET,
  session: {
    strategy: "jwt",
  },
  providers: [
    CredentialsProvider({
      async authorize(credentials, req) {
        const { code } = credentials;
       const response = await fetch(
          `http://localhost:3000/api/handleLarkLogin`,
          {
            method: "POST",
            body: JSON.stringify({
              code: code,
            }),
          }
        );

        const result = await response.json();

        if (result.ok && response) {
          return result;
        }
        return null;
      },
    }),
  ],
  pages: {
    signIn: "/auth/token",
  },
  callbacks: {
    async session(session, token) {
      session.user.code = token.code;
      return session;
    },
  },
};

export default NextAuth(authOptions);

这是我的登录网站

// auth/token/index.js

  const handleSubmit = async (e) => {
    try {
      
      const signInResponse = await signIn("credentials", {
        redirect: false,
        code: data,
      });

      if (signInResponse.ok) {
        console.log(signInResponse);
      } else console.log(signInResponse);
    } catch (error) {
      console.error("error", error);
    }
  };

这是我的handleLarkLogin网站

export default async function handler(req, res) {
  const code = await req.body;
  const codex = JSON.parse(code).code;
  const Url =
    "https://open.larksuite.com/open-apis/auth/v3/tenant_access_token/internal";

  const rest = await fetch(Url, {
    method: "POST",
    headers: {
      "Content-Type": "application/json; charset=utf-8",
    },
    body: JSON.stringify({
      app_id: "my code",
      app_secret: "my code",
    }),
  });

  const repo = await rest.json();

  //=============================== get tenant access token =================================
  const Urli = "https://open.larksuite.com/open-apis/authen/v1/access_token";
  const resp = await fetch(Urli, {
    method: "POST",
    headers: {
      "Content-Type": "application/json; charset=utf-8",
      Authorization: `${repo.tenant_access_token}`,
    },
    body: JSON.stringify({
      grant_type: "authorization_code",
      code: `${codex}`,
      app_id: "my code",
      app_secret: "my code",
    }),
  });

  const result = await resp.json();
  res.status(200).json(result);
}

如何获取凭据的返回值提前感谢您的任何指导。

vptzau2j

vptzau2j1#

问题在session回调中的这一行:

session.user.code = token.code;

这里token.codeundefined,所以session.user.code将是undefined
要解决这个问题,你必须在callbacks对象中的session回调之前添加jwt回调:

async jwt = ({user, token}) {
  // here "user" is the object returned by the "authorize" function of the "CredentialsProvider" 
  // so it is in fact "result"
  if (user) {
    token.code = ...; // add the property "code" to "token" maybe user.code
    // you can add other stuff to your token
  }
  return token; // return the updated token
};

现在,当session回调运行时,token.code应该包含预期的数据

相关问题