1- import NextAuth , { CredentialsSignin } from "next-auth" ;
1+ import NextAuth from "next-auth" ;
22import { authConfig } from "./auth.config" ;
33import GitHub from "next-auth/providers/github" ;
44import { Pool } from "@neondatabase/serverless" ;
55import NeonAdapter from "@auth/neon-adapter" ;
66
7- class InvalidLoginError extends CredentialsSignin {
8- code = "Invalid identifier or password" ;
9- }
7+ type NeonAdapterPool = Parameters < typeof NeonAdapter > [ 0 ] ;
108
119export const { handlers, auth, signIn, signOut } = NextAuth ( ( ) => {
12- const pool = new Pool ( { connectionString : process . env . DATABASE_URL } ) ;
10+ // Neon 连接只在有数据库配置时启用;本地协作者若没有 `.env`,将回退为纯 JWT 会话,避免直接抛错阻塞开发。
11+ const databaseUrl = process . env . DATABASE_URL ;
12+ const adapter = databaseUrl
13+ ? NeonAdapter (
14+ new Pool ( {
15+ connectionString : databaseUrl ,
16+ } ) as unknown as NeonAdapterPool ,
17+ )
18+ : undefined ;
19+
20+ if ( ! databaseUrl ) {
21+ console . warn ( "[auth] DATABASE_URL missing – running without Neon adapter" ) ;
22+ }
23+
1324 return {
1425 ...authConfig ,
15- adapter : NeonAdapter ( pool ) ,
1626 providers : [
1727 GitHub ( {
1828 profile ( profile ) {
@@ -25,8 +35,17 @@ export const { handlers, auth, signIn, signOut } = NextAuth(() => {
2535 } ,
2636 } ) ,
2737 ] ,
38+ ...( adapter
39+ ? {
40+ adapter,
41+ session : {
42+ strategy : "database" as const ,
43+ } ,
44+ }
45+ : {
46+ session : {
47+ strategy : "jwt" as const ,
48+ } ,
49+ } ) ,
2850 } ;
29- session: {
30- strategy: "database" ;
31- }
3251} ) ;
0 commit comments