lucia-auth / lucia

Authentication, simple and clean
https://lucia-auth.com
MIT License
9.46k stars 485 forks source link

[Bug]: Vercel postgres - Error: Connection terminated #1304

Closed arepp23 closed 10 months ago

arepp23 commented 10 months ago

Package

lucia-auth

Describe the bug

When attempting to use lucia with Next.js and vercel postgres, I'm getting the following:

Error: Connection terminated
    at cn.eval (webpack-internal:///(middleware)/./node_modules/@neondatabase/serverless/index.js:3890:40)
    at Object.onceWrapper (webpack-internal:///(middleware)/./node_modules/@neondatabase/serverless/index.js:1127:141)
    at cn.emit (webpack-internal:///(middleware)/./node_modules/@neondatabase/serverless/index.js:1099:68)
    at x.eval (webpack-internal:///(middleware)/./node_modules/@neondatabase/serverless/index.js:3737:19)
    at x.emit (webpack-internal:///(middleware)/./node_modules/@neondatabase/serverless/index.js:1098:37)
    at WebSocket.eval (webpack-internal:///(middleware)/./node_modules/@neondatabase/serverless/index.js:3050:22)
    at [nodejs.internal.kHybridDispatch] (node:internal/event_target:731:20)
    at WebSocket.dispatchEvent (node:internal/event_target:673:26)
    at fireEvent (eval at requireWithFakeGlobalScope (C:\Users\flomu\Desktop\Inventory\nextjs-dashboard\node_modules\next\dist\compiled\edge-runtime\index.js:1:657096), <anonymous>:12687:14)
    at TLSSocket.onSocketClose (eval at requireWithFakeGlobalScope (C:\Users\flomu\Desktop\Inventory\nextjs-dashboard\node_modules\next\dist\compiled\edge-runtime\index.js:1:657096), <anonymous>:12884:7)
Error: Connection terminated
    at cn.eval (webpack-internal:///(middleware)/./node_modules/@neondatabase/serverless/index.js:3890:40)
    at Object.onceWrapper (webpack-internal:///(middleware)/./node_modules/@neondatabase/serverless/index.js:1127:141)
    at cn.emit (webpack-internal:///(middleware)/./node_modules/@neondatabase/serverless/index.js:1099:68)
    at x.eval (webpack-internal:///(middleware)/./node_modules/@neondatabase/serverless/index.js:3737:19)
    at x.emit (webpack-internal:///(middleware)/./node_modules/@neondatabase/serverless/index.js:1098:37)
    at WebSocket.eval (webpack-internal:///(middleware)/./node_modules/@neondatabase/serverless/index.js:3050:22)
    at [nodejs.internal.kHybridDispatch] (node:internal/event_target:731:20)
    at WebSocket.dispatchEvent (node:internal/event_target:673:26)
    at fireEvent (eval at requireWithFakeGlobalScope (C:\Users\flomu\Desktop\Inventory\nextjs-dashboard\node_modules\next\dist\compiled\edge-runtime\index.js:1:657096), <anonymous>:12687:14)
    at TLSSocket.onSocketClose (eval at requireWithFakeGlobalScope (C:\Users\flomu\Desktop\Inventory\nextjs-dashboard\node_modules\next\dist\compiled\edge-runtime\index.js:1:657096), <anonymous>:12884:7)
 ⨯ unhandledRejection: Error: Connection terminated
    at cn.eval (webpack-internal:///(middleware)/./node_modules/@neondatabase/serverless/index.js:3890:40)
    at Object.onceWrapper (webpack-internal:///(middleware)/./node_modules/@neondatabase/serverless/index.js:1127:141)
    at cn.emit (webpack-internal:///(middleware)/./node_modules/@neondatabase/serverless/index.js:1099:68)
    at x.eval (webpack-internal:///(middleware)/./node_modules/@neondatabase/serverless/index.js:3737:19)
    at x.emit (webpack-internal:///(middleware)/./node_modules/@neondatabase/serverless/index.js:1098:37)
    at WebSocket.eval (webpack-internal:///(middleware)/./node_modules/@neondatabase/serverless/index.js:3050:22)
    at [nodejs.internal.kHybridDispatch] (node:internal/event_target:731:20)
    at WebSocket.dispatchEvent (node:internal/event_target:673:26)
    at fireEvent (eval at requireWithFakeGlobalScope (C:\Users\flomu\Desktop\Inventory\nextjs-dashboard\node_modules\next\dist\compiled\edge-runtime\index.js:1:657096), <anonymous>:12687:14)
    at TLSSocket.onSocketClose (eval at requireWithFakeGlobalScope (C:\Users\flomu\Desktop\Inventory\nextjs-dashboard\node_modules\next\dist\compiled\edge-runtime\index.js:1:657096), <anonymous>:12884:7)
 ⨯ unhandledRejection: Error: Connection terminated
    at cn.eval (webpack-internal:///(middleware)/./node_modules/@neondatabase/serverless/index.js:3890:40)
    at Object.onceWrapper (webpack-internal:///(middleware)/./node_modules/@neondatabase/serverless/index.js:1127:141)
    at cn.emit (webpack-internal:///(middleware)/./node_modules/@neondatabase/serverless/index.js:1099:68)
    at x.eval (webpack-internal:///(middleware)/./node_modules/@neondatabase/serverless/index.js:3737:19)
    at x.emit (webpack-internal:///(middleware)/./node_modules/@neondatabase/serverless/index.js:1098:37)
    at WebSocket.eval (webpack-internal:///(middleware)/./node_modules/@neondatabase/serverless/index.js:3050:22)
    at [nodejs.internal.kHybridDispatch] (node:internal/event_target:731:20)
    at WebSocket.dispatchEvent (node:internal/event_target:673:26)
    at fireEvent (eval at requireWithFakeGlobalScope (C:\Users\flomu\Desktop\Inventory\nextjs-dashboard\node_modules\next\dist\compiled\edge-runtime\index.js:1:657096), <anonymous>:12687:14)
    at TLSSocket.onSocketClose (eval at requireWithFakeGlobalScope (C:\Users\flomu\Desktop\Inventory\nextjs-dashboard\node_modules\next\dist\compiled\edge-runtime\index.js:1:657096), <anonymous>:12884:7)

Here's how I have it configured in my code, following the documentation example:

// lucia.ts
import { lucia } from "lucia";
import { pg } from "@lucia-auth/adapter-postgresql";
import { db } from "@vercel/postgres";
import { nextjs_future } from "lucia/middleware";
import "lucia/polyfill/node"; // polyfill for node 18 and below

export const auth = lucia({
    adapter: pg(db, {
        user: "auth_user",
        key: "user_key",
        session: "user_session"
    }),
    env: "DEV",
    middleware: nextjs_future(),
    sessionCookie: {
        expires: false
    },
});
// middleware.ts
export default async function middleware(req: NextRequest) {
  const url = req.nextUrl;

  try {
    const user = await auth.createUser({
      key: {
        providerId: "email",
        providerUserId: "redacted@gmail.com",
        password: "!Login123"
      },
      attributes: {
        email: "redacted@gmail.com",
                email_verified: false // `Boolean(false)` if stored as an integer
      } // expects `Lucia.DatabaseUserAttributes`
    });
    console.log(user)
  } catch (e) {
    if (e instanceof LuciaError && e.message === `AUTH_DUPLICATE_KEY_ID`) {
      // key already exists
      console.log('key already exists!')
    }
    // provided user attributes violates database rules (e.g. unique constraint)
    // or unexpected database errors
  }
...
...
...

I have created the three necessary tables in vercel postgres already.

pilcrowonpaper commented 10 months ago

Can you first verify that it’s an issue with Lucia and not how you setup your database connection?

arepp23 commented 10 months ago

@pilcrowOnPaper I have the app connected to the vercel postgres database already and can read and write data no problem.

pilcrowonpaper commented 10 months ago

Can you check whether you can query your database in the middleware?

arepp23 commented 10 months ago

Yes I am able to query another table from the middleware function

pilcrowonpaper commented 10 months ago

Are you querying using db instead of sql?

arepp23 commented 10 months ago

I'm able to query data with both db and sql from the middleware

pilcrowonpaper commented 10 months ago

Can you insert data with db and use transactions?

arepp23 commented 10 months ago

I realized I wasn't logging the actual error. It's saying [error: column "email" of relation "auth_user" does not exist]

I made the tables using the schemas shown here: https://lucia-auth.com/database-adapters/postgres/

pilcrowonpaper commented 10 months ago

You have to add the email and email_verified column to the user table