Credential Login Authorize Function Runs on Edge, somehow #9949
Replies: 1 comment 1 reply
-
Alright, found a workaround using the data api: |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I have a problem with the implementation of the middleware, I presume. In the beginning, I used Firestore with test rules. However, as I didn't know that the server shouldn't use the client SDKs, I tried to change it to admin SDKs and have even tried MongoDB. The problem is that I get the error below:
Error: The edge runtime does not support Node.js 'stream' module.
Learn More: https://nextjs.org/docs/messages/node-module-in-edge-runtime
at (webpack-internal:///(middleware)/./node_modules/next/dist/esm/server/web/globals.js:33)
at Object.get (webpack-internal:///(middleware)/./node_modules/next/dist/esm/server/web/globals.js:33:19)
at eval (webpack-internal:///(middleware)/./node_modules/mongodb/lib/cursor/abstract_cursor.js:617:45)
at (middleware)/./node_modules/mongodb/lib/cursor/abstract_cursor.js (file:///Users/arman/Documents/Code/istut/istut-v2/.next/server/src/middleware.js:1152:1)
at webpack_require (file:///Users/arman/Documents/Code/istut/istut-v2/.next/server/edge-runtime-webpack.js:37:33)
at fn (file:///Users/arman/Documents/Code/istut/istut-v2/.next/server/edge-runtime-webpack.js:280:21)
at eval (webpack-internal:///(middleware)/./node_modules/mongodb/lib/cursor/aggregation_cursor.js:9:27)
at (middleware)/./node_modules/mongodb/lib/cursor/aggregation_cursor.js (file:///Users/arman/Documents/Code/istut/istut-v2/.next/server/src/middleware.js:1163:1)
at webpack_require (file:///Users/arman/Documents/Code/istut/istut-v2/.next/server/edge-runtime-webpack.js:37:33)
at fn (file:///Users/arman/Documents/Code/istut/istut-v2/.next/server/edge-runtime-webpack.js:280:21)
at eval (webpack-internal:///(middleware)/./node_modules/mongodb/lib/collection.js:10:30) {
middleware: true
}
And when I restart the development I get a ginormous amount of errors, all of which have the exact same message except their dependency names:
Module not found: Can't resolve 'dependencyname' in '/Users/arman/Documents/Code/istut/istut-v2/node_modules/mongodb/lib'
By the way, the code inside my credential login's authorize function is causing this. This is happening due to some kind of leakage of the auth.config or auth.ts files to the middleware, I assume.
I refactored my code such that it uses MongoDB, and the app doesn't have a single problem except when I add even the most simple console.log((await client.connect()).db("users").collection("users").find()); inside the authorize function.
Relevant Code
middleware.ts
import { NextResponse } from "next/server";
import authConfig from "./auth.config";
import NextAuth from "next-auth";
const { auth } = NextAuth(authConfig);
export default auth(req => {
// If route is admin and isLoggedIn has admin on it, permit access,
});
export const config = {
matcher: ["/((?!.+\.[\w]+$|_next|$|auth/new-verification).)", "/(api|trpc)(.)"]
};
auth.ts
import NextAuth, { Account } from "next-auth";
import authConfig from "./auth.config";
import { FirestoreAdapter } from "@auth/firebase-adapter";
import { accountsCol, app, db, usersCol } from "./lib/firebaseConfig";
import { doc, getDoc, getDocs, query, where, limit } from "firebase/firestore";
import { cert } from "firebase-admin/app";
export const {
handlers: { GET, POST },
auth,
signIn,
signOut
} = NextAuth({
session: { strategy: "jwt" },
...authConfig
});
auth.config.ts
import { NextAuthConfig } from "next-auth";
import Credentials from "next-auth/providers/credentials";
import { LoginSchema } from "./schemas/login";
import client from "./lib/client";
export default {
providers: [
Credentials({
async authorize(credentials) {
const validatedFields = LoginSchema.safeParse(credentials);
} satisfies NextAuthConfig;
I would be more than willing to provide more information regarding the related files if need be.
Heaps of thanks ahead of time :)
Beta Was this translation helpful? Give feedback.
All reactions