如何使用Next Auth和KeyCloak提供商添加多租户
我一直在尝试使用 next-auth 和 keycloak 在一起,但是我使用多键登录时,我使用单个keycloak提供商时我无法更改clientsecret
和发行者
在运行时的选项,因此我尝试添加每个领域中使用不同的id
的倍数keycloak提供商,它可以使用,我可以使用React Hook选择正确的领域:
[... NextAuth] .TS
const realms = [
{
id: 'abc',
clientId: 'nextjs',
clientSecret: 'asfasdfdfasdfdasfasfddsf',
issuer: 'http://localhost:8080/realms/abc',
},
{
id: 'xyz',
clientId: 'nextjs',
clientSecret: 'ssdfsdfsdfasdfasdfasdfasfdsdf',
issuer: 'http://localhost:8080/realms/xyz',
}
];
提供商:
export default NextAuth({
providers: realms.map((realm) => KeycloakProvider({
id: realm.id,
clientId: realm.clientId,
clientSecret: realm.clientSecret,
issuer: realm.issuer
})),
});
我最大的问题是我不能在运行时包括更多的提供商/领域配置,如果我可以使用安全的终点来获取这些KeyCloak Configs ...如果有人可以帮助我,向我展示一些指南如何实现它,那么欢迎任何帮助!
我是NextJs的新手,您可以检查我的全项目code
I've been trying to use next-auth and keycloak together, but I'm falling when using multi-realms login, using a single keycloak provider I couldn't change clientSecret
and issuer
options at runtime, so I tried to add multiples keycloak providers with a different id
per realm, it works and I can use the react hook to select the right realm:
[...nextauth].ts
const realms = [
{
id: 'abc',
clientId: 'nextjs',
clientSecret: 'asfasdfdfasdfdasfasfddsf',
issuer: 'http://localhost:8080/realms/abc',
},
{
id: 'xyz',
clientId: 'nextjs',
clientSecret: 'ssdfsdfsdfasdfasdfasdfasfdsdf',
issuer: 'http://localhost:8080/realms/xyz',
}
];
providers:
export default NextAuth({
providers: realms.map((realm) => KeycloakProvider({
id: realm.id,
clientId: realm.clientId,
clientSecret: realm.clientSecret,
issuer: realm.issuer
})),
});
My biggest problem is that I can't include more providers/realms configurations at runtime, it would be nice if I could use an secure end-point to fetch those keycloak configs... so if someone could help me out, showing me some guide how to achieve it, any help is welcome!
My I'm pretty new to NextJS and you can check my full-project-code
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好的,我找到了一个似乎有效的解决方案!
在
[... NextAuth] .TSX
文件中,而不是直接导出NextAuth函数,而是像其他任何API一样使用处理程序,然后您可以动态控制提供商。例子:Ok I found a solution that seems to work!
In the
[...nextauth].tsx
file, instead of directly exporting the NextAuth function, use a handler just like any other api, and then you can control the providers dynamically. Example: