我正试着和下一个认证和云雀一起做一个证书认证。但是我不能得到凭证的返回值,所以有人知道我哪里出错了吗?
这是我的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);
}
如何获取凭据的返回值提前感谢您的任何指导。
1条答案
按热度按时间vptzau2j1#
问题在
session
回调中的这一行:这里
token.code
是undefined
,所以session.user.code
将是undefined
。要解决这个问题,你必须在
callbacks
对象中的session
回调之前添加jwt
回调:现在,当
session
回调运行时,token.code
应该包含预期的数据