如何从函数返回next-auth authOptions

0yg35tkg  于 2023-10-18  发布在  其他
关注(0)|答案(1)|浏览(93)

我必须从AWS Secrets Manager获取值并将它们加载到next-auth authOptions配置中。我有这样的实现:

export const buildAuthOptions = async () => {
  const secrets: AuthSecrets = await getSecret(
    'secret_name',
  );

  return {
    providers: [
      CognitoProvider({
        clientId: secrets.cognitoClientId,
        clientSecret: secrets.cognitoClientSecret,
        issuer: secrets.cognitoDomain,
      }),
    ],
    secret: secrets.JWTSecret,
  };
};

export const authOptions: NextAuthOptions = buildAuthOptions();

const handler: NextAuthOptions = NextAuth(authOptions);

export { handler as GET, handler as POST };

我从控制台得到的错误是

- error TypeError: options.providers is not iterable

从一个函数返回authOptions的最佳实践是什么?

bvjveswy

bvjveswy1#

here解释如下:你应该从接头人那里得到秘密。

import type { NextApiRequest, NextApiResponse } from "next"
import NextAuth from "next-auth"

export default async function auth(req: NextApiRequest, res: NextApiResponse) {
// Fetch your configs
const options = await buildAuthOptions()
  // Do whatever you want here, before the request is passed down to `NextAuth`
  return await NextAuth(req, res, options)
}

相关问题