I have an auth.js file that authenticates users by checking a PostgreSQL database:
const providers = [
Credentials({
credentials: {
email: {},
password: {},
},
authorize: async (credentials) => {
const res = await query('SELECT * FROM users WHERE email=$1', [credentials.email]);
if (res.rows.length > 0) {
const user = res.rows[0];
const passwordsMatch = await bcrypt.compare(credentials.password, user.password);
if (passwordsMatch) return user;
}
return null;
},
}),
];
export const { handlers, signIn, signOut, auth } = NextAuth({
providers,
});
But I cannot use this authentication in middleware or in other client components.
If I use it in middleware, I get an error: The edge runtime does not support Node.js ‘crypto’ module after adding middleware.js.
If I use it in client components, I get this error:
./node_modules/pg-connection-string/index.js:76:1
Module not found: Can’t resolve ‘fs’
How can I fix this?