使用Google Cloud Secret Manager fet auth0_client_secret并与nextjs-auth0一起使用
我有一个下一个.js应用程序,其中使用auth0 next.js sdk设置身份验证。
当前,部署时,正在将auth0_client_secret
设置为环境变量。
我想使用Google Cloud Secret Manager在运行时获取auth0_client_secret
,并使用initauth0
方法设置它。
但是我不知道当我需要准备好调用方法initauth0({clientsecret ...})
的秘密时,我如何等待秘密管理器的响应使用auth0.handleauth()
。
这是我的尝试:/pages/api/auth/[... auth] .ts
import { initAuth0 } from "@auth0/nextjs-auth0";
const asyncHandleAuth = async () => {
const clientSecret = await getSecret("AUTH0_CLIENT_SECRET");
const auth0 = initAuth0({
clientSecret // The rest of the config is set with environment variables
});
return auth0.handleAuth();
};
export default asyncHandleAuth();
I have a Next.js application where authentication is set up with the Auth0 Next.js SDK.
Currently the AUTH0_CLIENT_SECRET
is being set as an environment variable when deploying.
I would like to use Google Cloud Secret Manager to get the AUTH0_CLIENT_SECRET
during runtime and set it using the initAuth0
method.
I'm following this example: https://github.com/auth0/nextjs-auth0/blob/main/EXAMPLES.md#create-your-own-instance-of-the-sdk
But I can't figure out how I can await the response from secret manager when I need to have the secret ready for calling the method initAuth0({clientSecret...})
and I need that in place to setup the auth end points with auth0.handleAuth()
.
This is my attempt: /pages/api/auth/[...auth].ts
import { initAuth0 } from "@auth0/nextjs-auth0";
const asyncHandleAuth = async () => {
const clientSecret = await getSecret("AUTH0_CLIENT_SECRET");
const auth0 = initAuth0({
clientSecret // The rest of the config is set with environment variables
});
return auth0.handleAuth();
};
export default asyncHandleAuth();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
拉开头发后,我发现了这个问题。 Next.js期望导出默认函数是类型
NextApiHandler
,但我正在返回Promise< NextApiHandler>
。我通过将其包裹在另一个函数中,以获取请求和响应参数,并在返回之前使用它们来调用Handleauth。
这对我有用:
After some hair pulling I found the problem. Next.js expects the export default function to be of type
NextApiHandler
but I was returningPromise<NextApiHandler>
.I solved it by wrapping it in another function that takes the request and response arguments and use them to call handleAuth before returning it.
This worked for me:
在答案中发布的代码中:
您已经等到秘密返回直到返回秘密:您的代码将暂停在该行上,直到
getsecret
结束为止。结果,使用initauth0
函数时,秘密
应准备好。也许,根据您的评论,您的出口可能会激发问题。您正在导出
asynchandleauth
这样的功能:但是我认为应该是:
您的答案是完全合理的:实际问题是您需要提供适当的参数,请求和响应表示形式,您的处理程序功能执行实际调用。但是,请注意,提出的
导出默认
仍然有效,在您的代码中,您正在执行返回导出的 thits 的函数。可能您可以这样简化它:请注意,第一个箭头功能不需要。
In the code you posted in your answer:
you are already waiting until the secret is returned: your code will suspend on that line until
getSecret
ends. As a consequence, thesecret
should be ready when using theinitAuth0
function.Perhaps, and according to your comments, the problem could be motivated by your export. You are exporting the
asyncHandleAuth
function like this:But I think it should be instead:
Your answer makes perfect sense: the actual problem is that you need to provide the appropriate arguments, the request and response representations, to your handler function to perform the actual invocation. But please, be aware that the proposed
export default
still is valid, in your code you are executing a function that returns the thing that is being exported. Probably you could simplify it like this:Note that there is no need for the first arrow function.