这是一个使用next-auth进行身份验证的next.js13应用程序。它当前仅使用Azure AD作为提供程序进行设置。
在开发中一切都很好,但在生产中,当我刚进入登陆页面时,我得到了一个404回来。
当我尝试打开另一个页面(例如https://mySite/dashboard)时,出现以下服务器错误:
\[next-auth\]\[error\]\[NO_SECRET\]
https://next-auth.js.org/errors#no_secret Please define a `secret` in production. MissingSecret \[MissingSecretError\]: Please define a `secret` in production.
at assertConfig (/datahub-ui/node_modules/next-auth/core/lib/assert.js:42:12)
at NextAuthHandler (/datahub-ui/node_modules/next-auth/core/index.js:70:52)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async NextAuthNextHandler (/datahub-ui/node_modules/next-auth/next/index.js:24:19)
at async auth (/datahub-ui/.next/server/pages/api/auth/\[...nextauth\].js:37:12)
at async Object.apiResolver (/datahub-ui/node_modules/next/dist/server/api-utils/node.js:363:9)
at async NextNodeServer.runApi (/datahub-ui/node_modules/next/dist/server/next-server.js:474:9)
at async Object.fn (/datahub-ui/node_modules/next/dist/server/next-server.js:736:37)
at async Router.execute (/datahub-ui/node_modules/next/dist/server/router.js:252:36)
at async NextNodeServer.run (/datahub-ui/node_modules/next/dist/server/base-server.js:383:29) {
code: 'NO_SECRET'
}
- 代码:**[...下一个时间]. ts
import NextAuth from 'next-auth'
import AzureADProvider from 'next-auth/providers/azure-ad'
import { NextApiRequest, NextApiResponse } from 'next'
export default async function auth(req: NextApiRequest, res: NextApiResponse) {
const providers = \[
AzureADProvider({
clientId: process.env.NEXT_PUBLIC_AZURE_AD_CLIENT_ID,
clientSecret: process.env.NEXT_PUBLIC_AZURE_AD_CLIENT_SECRET,
tenantId: process.env.NEXT_PUBLIC_AZURE_AD_TENANT_ID,
}),
\]
return await NextAuth(req, res, {
providers,
secret: process.env.NEXTAUTH_SECRET,
})
}
环境生产
NEXT_PUBLIC_AZURE_AD_CLIENT_ID='..................-9cea-a456aa4a38f4'
NEXT_PUBLIC_AZURE_AD_TENANT_ID='..................-adec-c9cc85f02498'
NEXT_PUBLIC_AZURE_AD_CLIENT_SECRET='.............................bR7'
NEXTAUTH_SECRET='1...........................................3cf'
NEXTAUTH_URL='https://mySite'
我已经检查了一些相关的问题(或者至少,我认为它们是相关的):
- https://github.com/nextauthjs/next-auth/issues/3245
- MissingSecret [MissingSecretError]: Please define a
secret
in production
你知道我怎样才能找到这个问题吗?
1条答案
按热度按时间jchrr9hc1#
为什么需要默认导出auth()处理程序?
根据文档,您不需要像以前那样导出自定义处理程序。
您所需要的只是在[...nextauth].tsx文件中有这样的东西。
如果你仍然遇到任何问题,请参考new NextAuth.js documentation,或者你可以阅读这篇文章,它通过步骤和代码示例解释了一切:How to implement Google login and protected routes using NextAuth
希望这能帮上忙。