admin管理员组

文章数量:1389863

Intro

Hello, I am using the following files for my NextAuth configuration. When I use a server action inside the authorize method I get the edge error. I tought that the authorize method does not run on edge, but it seems like because of my middleware I get that.

Is there any posibility of using middleware like I did while using a server action inside the authorize method?

middleware.ts -> routes permissions auth.config.ts -> being able to use it in middleware (because it runs on edge) auth.ts -> for session configuration

Code

auth.config.ts

export default {
  providers: [
    Credentials({
      async authorize(credentials) {
        const validatedFields = zLoginSchema.safeParse(credentials);
        if (validatedFields.success) {
          const { email, password } = validatedFields.data;

          const existingUser = await getUserByEmail(email);
          if (!existingUser || !existingUser.password) return null;

          const passwordsMatch = await bcryptpare(password, existingUser.password);

          if (passwordsMatch) return existingUser;
        }

        return null;
      },
    }),
  ],
} satisfies NextAuthConfig;

middleware.ts

export default auth((req: NextAuthRequest) => {
  const { nextUrl } = req;
  const isLoggedIn = !!req.auth;

  const isApiAuthRoute = nextUrl.pathname.startsWith(apiAuthPrefix);
  const isPublicRoute = publicRoutes.includes(nextUrl.pathname);
  const isAuthRoute = authRoutes.includes(nextUrl.pathname);

  if (isApiAuthRoute) {
    return null;
  }

  if (isAuthRoute) {
    if (isLoggedIn) {
      return Response.redirect(new URL(DEFAULT_LOGIN_REDIRECT, nextUrl));
    }
    return null;
  }

  if (!isLoggedIn && !isPublicRoute) {
    return Response.redirect(new URL('/login', nextUrl));
  }

  return null;
});

Error

⨯ Error: The edge runtime does not support Node.js 'dns' module.
Learn More: 
     at promisify2 (internal:promisify:18:24)
     at [project]/node_modules/mongodb/lib/utils.js [middleware-edge] (ecmascript) (../src/utils.ts:1308:37)
     at [project]/node_modules/mongodb/lib/timeout.js [middleware-edge] (ecmascript) (../src/timeout.ts:6:40)
     at [project]/node_modules/mongodb/lib/operations/execute_operation.js [middleware-edge] (ecmascript) (../src/operations/execute_operation.ts:27:44)
     at [project]/node_modules/mongodb/lib/admin.js [middleware-edge] (ecmascript) (../src/admin.ts:4:0)
     at [project]/node_modules/mongodb/lib/index.js [middleware-edge] (ecmascript) (../src/index.ts:1:32)
     at [project]/src/lib/db.ts [middleware-edge] (ecmascript) (src/lib/db.ts:1:0)
     at [project]/src/actions/user.ts [middleware-edge] (ecmascript) (src/actions/user.ts:4:0)
     at [project]/src/lib/auth.config.ts [middleware-edge] (ecmascript) (src/lib/auth.config.ts:6:0)
     at [project]/src/middleware.ts [middleware-edge] (ecmascript) (src/middleware.ts:2:0)
     at <anonymous> (edge-wrapper.js:2:6)
 1 | import { MongoClient } from 'mongodb';

I tried using MongoDB Adapter (I do not use mongoose). It works if I make an api route instead of making a server action, but this defeats the purpose exposes an api route for getting the user which I do not want.

本文标签: nextjsNextAuth authorize method not allowing usage of MongoDB queries in NextJsStack Overflow