Reputation: 13
The problem: I keep getting error messages saying that my neonserverless pool connection has unexpectedly terminated.
uncaughtException: Error: Connection terminated unexpectedly at fn.eval (webpack-internal:///(middleware)/./node_modules/.pnpm/@neondatabase+serverless@0
Where I suspect the problem lies: not creating the connection object in the right way / place
The various confounding issues: as I'm using nodemailer, which is incompatible with the Edge runtime used by Next middleware, I've distributed various parts of the auth configuration between auth.ts, middleware.ts and authConfig.ts. Middleware defaults to jwt unless I explicitly state that I need to use the session strategy (I'm using magic email login), but then that also requires that I explicitly state in middleware what adapter I'm using, which then also means I need to refer to the pool const. I tried setting up a pool const in a separate databaseConfig.ts file, but still got the connection termination issues. So what's working, but what I assume is not the right approach, is to create pool connection objects in both auth.ts and middleware.ts. Users can log in and out okay, the cookies work, the database is getting accessed accordingly, but I keep getting the messages saying: "connection terminated unexpectedly".
I remember reading that the pool connection should not be created outside of any of the relevant functions (see here and comment in auth.ts code), so I've set them up as shown. But since I'm still getting the error and since repetition of code = bad, I can't help thinking there must be another way to dot all the i's without making two separate pool connection objects.
auth.ts file:
import { Pool } from "@neondatabase/serverless"
//Nextauth docs say *don't* declare pool variable here
export const {handlers, signIn, signOut, auth} = NextAuth(() => {
//declare pool variable here inside function
const pool = new Pool({
host: process.env.BLAH,
user: process.env.BLAH,
password: process.env.BLAH,
database: process.env.BLAH,
max: 20,
idleTimeoutMillis: 30000,
connectionString: process.env.BLAH,
connectionTimeoutMillis: 2000,
})
return {...authConfig,
adapter: PostgresAdapter(pool),
callbacks: {
session({ session, user }) {
return session
}
},
providers: [
Nodemailer({
server: {
host: process.env.BLAH,
port: Number(process.env.BLAH),
auth: {
user: process.env.BLAH,
pass: process.env.BLAH,
},
},
from: process.env.EMAIL_FROM,
}),
]}
// other options...
})
authConfig.ts
export default {
providers: [],
session: { strategy: "database" },
// other options...
} satisfies NextAuthConfig
middleware.ts
export const {auth: middleware} = NextAuth(() => {
const pool = new Pool({
host: process.env.BLAH,
user: process.env.BLAH,
password: process.env.BLAH,
database: process.env.BLAH,
max: 20,
idleTimeoutMillis: 30000,
connectionString: process.env.BLAH,
connectionTimeoutMillis: 2000,
})
return {...authConfig,
adapter: PostgresAdapter(pool),
callbacks: {
session({ session, user }) {
return session
}
},
}
})
So that's where I am with it. Be grateful for any insights, thanks.
Upvotes: 0
Views: 14