We have our own OAuth 2.0 implementation and I am using next-auth
to connect and get a jwt in my Next.js
app.
我们有自己的OAuth 2.0实现,我正在使用Next-auth连接并在我的Next.js应用程序中获取JWT。
Output:
At the moment, I am able to login. However, I get null
as token on the client-side.
输出:目前,我可以登录。但是,我在客户端得到的令牌为空。
Steps I have implemented:
- Added the
next-auth
Provider component at the top-level.
function MyApp({ Component, pageProps }) {
return (
<Provider
options={{
clientMaxAge: 0,
keepAlive: 0,
}}
session={pageProps.session}>
<Component {...pageProps} />
</Provider>
);
}
- The file:
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;
},
},
});
This is where the first issue is: none of the callbacks are triggered and no console.log is printed on the terminal. There appears to be something wrong in my config above?
这就是第一个问题所在:没有触发任何回调,也没有在终端上打印任何console.log。我上面的配置似乎有问题?
- Added
signIn/ signOut
to sign-in with auth service on the Home page.
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>
...
This is where the next issue is: even though the callbackUrl in signin function is to the page '/happy', I am still taken to home page!
这就是下一个问题所在:即使登录函数中的回调URL指向页面‘/Happy’,我仍然被带到主页!
- I have .env.local file in the root with the following:
NEXTAUTH_URL=http://localhost:3000/
QUOTECH_ID=quot-test
QUOTECH_SECRET=[secret-from-authenticator-goes-here]
SECRET=[random-string-goes-here]
What am I missing ? Why am I getting null
once I am redirected by the client-app. It appears some of the options are being ignored in the [...nextauth].js
file above?
我错过了什么?为什么一旦我被客户端应用程序重定向,我就会得到NULL。上面的[...nextauth].js文件中似乎忽略了一些选项?
更多回答
Profile values are required on custom provider, if you update them that should work.
如果您更新了应该起作用的配置文件值,则自定义提供程序上需要配置文件值。
{
...,
profileUrl: string,
profile: function,
...
}
On documentation you can see those areas are required fields.
在文档中,您可以看到这些区域是必填字段。
profileUrl should be a profile link to get profile info
and profile is a function you manipulate this profile url returns to be fit in next-auth profile modal or your custom profile modal.
ProfileUrl应该是获取配置文件信息的配置文件链接,而配置文件是您操作此配置文件URL返回的函数,以适应下一次身份验证配置文件模式或您的自定义配置文件模式。
example;
举例说明;
{
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,
}
更多回答
Hi Tuna, any working sample provider would be of the highest appreciation :-)
嗨,金枪鱼,任何工作样品供应商都将非常感谢:-)
@killjoy you can check the adapter's GitHub repo for real-world examples of custom next-auth adapters.
@KILLJOY您可以查看适配器的GitHub repo,以获取定制Next-Auth适配器的真实示例。
我是一名优秀的程序员,十分优秀!