lucia-auth / lucia

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

[Bug]: Invalid value for argument `id`: invalid digit found in string. Expected big integer String. #1453

Closed qqqllppp closed 4 months ago

qqqllppp commented 4 months ago

Package

​@lucia-auth/adapter-prisma

Describe the bug

I am new to lucia, I am trying to sign up a new user but I keep getting an error on the "session" part.

Invalid `prisma.session.create()` invocation:

{
  data: {
    id: "amppmrl8jgs773wp3kwh74oa54ms757ndensumtg",
        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    userId: "945127043228729345",
    expiresAt: new Date("2024-02-21T07:26:43.905Z")
  }
}

Invalid value for argument `id`: invalid digit found in string. Expected big integer String.

I am using Prisma with Cockroach DB and TRPC with NextJS.

"lucia": "^3.0.1",
"@lucia-auth/adapter-prisma": "^4.0.0",
"@prisma/client": "^5.6.0",
"prisma": "^5.6.0",

My schema.prisma looks like:

generator client {
  provider = "prisma-client-js"
}

datasource db {
  provider = "cockroachdb"
  url      = env("DATABASE_URL")
}

model User {
  id             BigInt    @id @default(autoincrement())
  email          String    @unique @db.String(320) // https://www.google.com/search?channel=fs&client=ubuntu-sn&q=max+email+string+length
  hashedPassword String
  createdAt      DateTime  @default(now())
  session        Session[]
}

model Session {
  id        BigInt   @id @default(autoincrement())
  userId    BigInt
  expiresAt DateTime
  user      User     @relation(references: [id], fields: [userId], onDelete: Cascade)
}

The execution code looks like that:

const hashedPassword = await new Argon2id().hash(
  input.passwordObj.password,
);
// const userId = generateId(15);

const data = await ctx.db.user.create({
  data: {
    email: input.email,
    hashedPassword: hashedPassword,
  },
});

const session = await lucia.createSession(data.id.toString(), {});
ctx.res
  .appendHeader(
    "Set-Cookie",
    lucia.createSessionCookie(session.id).serialize(),
  )
  .status(200)
  .end();

The error occurs on this line:

const session = await lucia.createSession(data.id.toString(), {});

Whenever I sign up a new user, the User table is populated but the Session table remains empty due to the error.

Here is an example of data in User table:

id: 945127043228729345
email: test123@gmail.com
hashedPassword: $argon2id$v=19$m=19456,t=2,p=1$y4rA2Q9BpcO87ScwUN+KrQ$jYfZ27Bze7dJqnV/yrYYZTPeafCXLjW5q3pdmJmUh84
createdAt: 2024-02-21T07:24:43.116
pilcrowOnPaper commented 4 months ago

This is expected. The ID for both user and session must be a string. Also see #1366

qqqllppp commented 4 months ago

Hi @pilcrowOnPaper thanks for the response, I have now change my prisma.schema & execution code to the below (change to comply with string type), and I am getting error on User table now (so both User and Session table is empty).

schema.prisma

model User {
  id             String    @id
  email          String    @unique @db.String(320) // https://www.google.com/search?channel=fs&client=ubuntu-sn&q=max+email+string+length
  hashedPassword String
  createdAt      DateTime  @default(now())
  session        Session[]
}

model Session {
  id        String   @id
  userId    String
  expiresAt DateTime
  user      User     @relation(fields: [userId], references: [id], onDelete: Cascade)
}

execution code

import { generateId } from "lucia";
...

const hashedPassword = await new Argon2id().hash(
  input.passwordObj.password,
);
const userId = generateId(15);

const data = await ctx.db.user.create({
  data: {
    id: userId,
    email: input.email,
    hashedPassword: hashedPassword,
  },
});

const session = await lucia.createSession(userId, {});
ctx.res
  .appendHeader(
    "Set-Cookie",
    lucia.createSessionCookie(session.id).serialize(),
  )
  .status(200)
  .end();

Error:

Invalid `prisma.user.create()` invocation:

{
  data: {
    id: "r6qmicf115qh8zk",
        ~~~~~~~~~~~~~~~~~
    email: "test123@gmail.com",
    hashedPassword: "$argon2id$v=19$m=19456,t=2,p=1$qXcidGl7xstmRnft1p0/qw$kwXT3rpPjvZzwbnZorQyC4eqmBtmjGdLZiSLW1zCheM"
  }
}

Invalid value for argument `id`: invalid digit found in string. Expected big integer String.
❌ tRPC failed on auth.signUp: PrismaClientValidationError: 
Invalid `prisma.user.create()` invocation:

{
  data: {
    id: "r6qmicf115qh8zk",
        ~~~~~~~~~~~~~~~~~
    email: "test123@gmail.com",
    hashedPassword: "$argon2id$v=19$m=19456,t=2,p=1$qXcidGl7xstmRnft1p0/qw$kwXT3rpPjvZzwbnZorQyC4eqmBtmjGdLZiSLW1zCheM"
  }
}

Invalid value for argument `id`: invalid digit found in string. Expected big integer String.

Note: I am using const userId = generateId(15); for id now.

pilcrowOnPaper commented 4 months ago

Did you actually push the changes?

qqqllppp commented 4 months ago

I did push the db changes, but I had to restart my dev app as well for the changes to apply xD Thanks a bunch!

ValuONE commented 4 months ago

If the application is still running prisma can´t update the node modules with the new schema.prisma ... that was probably the reason for the issue.