admin管理员组文章数量:1289543
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.
本文标签:
版权声明:本文标题:typescript - How do I properly manage pool connection when using nextauth session strategy with nodemailer? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741476386a2380909.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论