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
版权声明:本文标题:next.js - NextAuth authorize method not allowing usage of MongoDB queries in NextJs - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744731578a2622077.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论