我们有自己的OAuth 2.0实现,我正在使用next-auth在我的Next.js应用程序中连接和获取jwt。
输出:目前,我可以登录。但是,我在客户端获得了null作为令牌。
我已经实施了以下步骤:
next-auth提供程序组件.。
function MyApp({ Component, pageProps }) {
return (
<Provider
options={{
clientMaxAge: 0,
keepAlive: 0,
}}
session={pageProps.session}>
<Component {...pageProps} />
</Provider>
);
}文件:pages/api/auth/[...nextauth].js:
export default NextAuth({
providers: [
{
id: "quot-test",
name: "quot-test",
type: "oauth",
version: "2.0",
scope: "openid email",
state: true,
protection: "state",
params: { grant_type: "authorization_code" },
idToken: true,
authorizationUrl:
"http://localhost:9000/oauth2/authorize?response_type=code",
accessTokenUrl: "http://localhost:9000/oauth2/token",
requestTokenUrl: "http://localhost:9000/oauth2/jwks",
clientId: process.env.QUOT_ID,
clientSecret: process.env.QUOT_SECRET,
},
],
secret: process.env.SECRET,
debug: true,
session: {
jwt: true,
maxAge: 60 * 5,
},
jwt: {
secret: process.env.SECRET,
encryption: false,
},
callbacks: {
async signin(user, account, profile) {
console.log("user", user, account, profile);
return true;
},
async jwt(token, user, account, profile, isNewUser) {
console.log(token);
console.log(user);
console.log(account);
console.log(profile);
console.log(isNewUser);
if (account.accessToken) {
token.accessToken = account.accessToken;
}
return Promise.resolve(token);
},
async session(session, token) {
console.log(session);
console.log(token);
return session;
},
},
});这是第一个问题:不触发回调,终端上没有打印console.log。我上面的配置出现了什么问题吗?
signIn/ signOut,在主页上注册了auth服务。import { signIn, signOut, useSession, getSession } from "next-auth/client";
export default function Home(){
...
const [session, loading] = useSession();
console.log(session); // prints null after signin
return(
<button onClick={() =>signIn(null, { callbackUrl: "http://localhost:3000/happy" })}>
Sign
</button>
...这就是下一个问题所在:尽管signin函数中的callbackUrl指向页面‘/高兴’,但我仍然被带到主页!
NEXTAUTH_URL=http://localhost:3000/
QUOTECH_ID=quot-test
QUOTECH_SECRET=[secret-from-authenticator-goes-here]
SECRET=[random-string-goes-here]我错过了什么?为什么一旦我被客户端应用程序重定向,我就会得到null。在上面的[...nextauth].js文件中,有些选项被忽略了吗?
发布于 2021-08-05 15:46:14
配置文件值在自定义提供程序上是必需的,如果您更新了它们,那么它们就会正常工作。
{
...,
profileUrl: string,
profile: function,
...
}在文档中,您可以看到这些区域是必需的字段。
profileUrl应该是获取配置文件信息的配置文件链接,而配置文件是您操作此配置文件url返回的函数,以适应下一个配置文件模式或您的自定义配置文件模式。
榜样;
{
id: "quot-test",
name: "quot-test",
type: "oauth",
version: "2.0",
scope: "openid email",
state: true,
protection: "state",
params: { grant_type: "authorization_code" },
idToken: true,
authorizationUrl: "http://localhost:9000/oauth2/authorize?response_type=code",
accessTokenUrl: "http://localhost:9000/oauth2/token",
requestTokenUrl: "http://localhost:9000/oauth2/jwks",
profileUrl: "[ProfileURL]",
profile(profile, tokens) {
return {
id: profile.id,
name: profile.name,
email: profile.email
};
},
clientId: process.env.QUOT_ID,
clientSecret: process.env.QUOT_SECRET,
}https://stackoverflow.com/questions/68225050
复制相似问题