Next.js Discord

Discord Forum

Mongodb error Middleware NextAuth v5

Answered
Rahul the Developer posted this in #help-forum
Open in Discord
export const authConfig = {
providers: [
Credentials({
credentials: {
username: { type: "text", required: true },
password: { type: "password", required: true },
},
async authorize(credentials) {
const { username, password } = credentials as {
username: string;
password: string;
};

if (!username !password) return null;

const existingUser = await findUserByUsername(username);
if (!existingUser existingUser.password !== password) return null;

return {
...existingUser,
id: existingUser._id.toString(),
name: existingUser.username,
};
// return null;
},
}),
],
} satisfies NextAuthConfig;

import { authConfig } from "./app/api/auth/[...nextauth]/auth.config";

const { auth } = NextAuth(authConfig);

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

const isApiAuthRoute = nextUrl.pathname.startsWith(apiAuthPrefix);
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) {
return Response.redirect(new URL(DEFAULT_REDIRECT, nextUrl));
}

return null;
});

Please help me with this code I know middleware only works in "edge" runtime and when I import authConfig file this gives me edge runtime does not support Node.js 'stream' module.

Can someone help me with this I want to use mongodb here
Answered by Ray
https://github.com/vercel/next-learn/blob/main/dashboard/final-example/middleware.ts
take a look in this example, they only have callbacks in their auth.config.ts
View full answer

3 Replies

@Rahul the Developer export const authConfig = { providers: [ Credentials({ credentials: { username: { type: "text", required: true }, password: { type: "password", required: true }, }, async authorize(credentials) { const { username, password } = credentials as { username: string; password: string; }; if (!username !password) return null; const existingUser = await findUserByUsername(username); if (!existingUser existingUser.password !== password) return null; return { ...existingUser, id: existingUser._id.toString(), name: existingUser.username, }; // return null; }, }), ], } satisfies NextAuthConfig; import { authConfig } from "./app/api/auth/[...nextauth]/auth.config"; const { auth } = NextAuth(authConfig); export default auth((req) => { const { nextUrl } = req; const isLoggedIn = !!req.auth; const isApiAuthRoute = nextUrl.pathname.startsWith(apiAuthPrefix); 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) { return Response.redirect(new URL(DEFAULT_REDIRECT, nextUrl)); } return null; }); Please help me with this code I know middleware only works in "edge" runtime and when I import authConfig file this gives me edge runtime does not support Node.js 'stream' module. Can someone help me with this I want to use mongodb here
https://github.com/vercel/next-learn/blob/main/dashboard/final-example/middleware.ts
take a look in this example, they only have callbacks in their auth.config.ts
Answer
@Rahul the Developer Thanks for giving me a solution
you are welcome