1

I've seem similar problems and tried some suggestions, even deleted my whole database and created it again, but nothing seems to work.

So Im trying to log in into my application via the Google Provider in Next Auth using the Prisma Adapter and Planet Scale, but somehow I always get this message: "Try signing in with a different account."

This is my schema.prisma:

//As provided by Planet Scale:
generator client {
  provider = "prisma-client-js"
}

datasource db {
  provider = "mysql"
  url = env("DATABASE_URL")
  relationMode = "prisma"
}

But this is the User model with some custom keys:

// Necessary for Next auth
model Account {
    id                String  @id @default(cuid())
    userId            String
    type              String
    provider          String
    providerAccountId String
    refresh_token     String? // @db.Text
    access_token      String? // @db.Text
    expires_at        Int?
    token_type        String?
    scope             String?
    id_token          String? // @db.Text
    session_state     String?
    user              User    @relation(fields: [userId], references: [id], onDelete: Cascade)

    @@unique([provider, providerAccountId])
    @@index([userId])
}

model Session {
    id           String   @id @default(cuid())
    sessionToken String   @unique
    userId       String
    expires      DateTime
    user         User     @relation(fields: [userId], references: [id], onDelete: Cascade)

    @@index([userId])
}

model User {
    id            String    @id @default(cuid())
    name          String?
    email         String?   @unique
    emailVerified DateTime?
    image         String?
    accounts      Account[]
    sessions      Session[]
//this is custom:
    userPreference   UserPreference?
    role             String? @default("client")
    Timeline      TimelineEntry[]
}

model VerificationToken {
    identifier String
    token      String   @unique
    expires    DateTime

    @@unique([identifier, token])
}

And this is [...nextauth].ts:

import NextAuth, { type NextAuthOptions } from "next-auth";
import GoogleProvider from "next-auth/providers/google";
// Prisma adapter for NextAuth, optional and can be removed
import { PrismaAdapter } from "@next-auth/prisma-adapter";

import { env } from "../../../env/server.mjs";
import { prisma } from "../../../server/db";

export const authOptions: NextAuthOptions = {
  // Include user.id on session
  callbacks: {
    async jwt({ token, user }) {
      if (user?.role) {
        token.role = user.role;
      }
      return token;
    },
    async session({ session, user }) {
      if (session.user) {
        session.user.id = user.id;
        session.user.role = user.role;
      }
      return session;
    },
  },
  // Configure one or more authentication providers
  adapter: PrismaAdapter(prisma),
  providers: [
    GoogleProvider({
      clientId: env.GOOGLE_CLIENT_ID,
      clientSecret: env.GOOGLE_CLIENT_SECRET,
    }),
    // ...add more providers here
  ],
  secret: env.NEXTAUTH_SECRET,
};

export default NextAuth(authOptions);

When trying to login in dev environment I also get the error:

Invalid `p.account.create()` invocation in
<adress here which I deleted>

  16 },
  17 updateUser: ({ id, ...data }) => p.user.update({ where: { id }, data }),
  18 deleteUser: (id) => p.user.delete({ where: { id } }),
→ 19 linkAccount: (data) => p.account.create(
The provided value for the column is too long for the column's type. Column: for
[next-auth][error][adapter_error_linkAccount] 
https://next-auth.js.org/errors#adapter_error_linkaccount
Invalid `p.account.create()` invocation in

Note that I dont get anything in the error for the Column, I wish I new what column it is talking about.

If you know whats going on pls help, and thanks.

Jonas
  • 121,568
  • 97
  • 310
  • 388
Daniel Guedes
  • 387
  • 1
  • 4
  • 14
  • 1
    Is there a reason you have commented `@db.Text` attribute in Account model? This GitHub Issue https://github.com/nextauthjs/next-auth/issues/4734 mentions that adding @db.Text should fix the issue. – Nurul Sundarani Mar 28 '23 at 18:26
  • Oh my, this made me look stupid, but that's great, it worked! I didnt know what this db.Text were for and I didnt find it on prisma docs very clearly either, but now I understand it forces a type different from the default in the database, and in this case MySql uses varchar(191), which is too short for one of those keys, Im guessing the id_token. This should be more clear in the error message thou, I found so many people with very similar issues and no clear solutions. Thank you very much! – Daniel Guedes Mar 29 '23 at 02:53

1 Answers1

2

As said by Nurul Sundarani in the comments, I just had to uncomment those @db.Text. It turns out MySql use the String in the schema as a VARCHAR(191) and one or more of those fields is longer than 191, so @db.Text forces it to use a TEXT type in the database that can be longer strings.

Daniel Guedes
  • 387
  • 1
  • 4
  • 14