Sn0w
Sn0w

Reputation: 17

Invalid `})=>p.user.update()` invocation in PrismaAdapter (AuthJs)

I've setup an app with NextAuth. You can only signing in by magic link with Resend. The email is sent but when I open the link in this email, an error is thrown. I've been looking for a solution for many hours but I don't understand why it doesn't work. I suppose it's come from the PrismaAdapter but maybe I've done something wrong.

This is my config:

System:
    OS: macOS 15.2
    CPU: (10) arm64 Apple M2 Pro
    Memory: 82.28 MB / 16.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 20.12.2 - ~/.nvm/versions/node/v20.12.2/bin/node
    Yarn: 1.22.19 - /opt/homebrew/bin/yarn
    npm: 10.5.0 - ~/.nvm/versions/node/v20.12.2/bin/npm
    pnpm: 8.10.4 - /opt/homebrew/bin/pnpm
  Browsers:
    Safari: 18.2
  npmPackages:
    @auth/prisma-adapter: ^2.7.4 => 2.7.4 
    next: 15.1.7 => 15.1.7 
    next-auth: 5.0.0-beta.25 => 5.0.0-beta.25 
    react: ^19.0.0 => 19.0.0

The error:

[auth][debug]: adapter_updateUser {
  "args": [
    {
      "id": "67c1e9f7e7a1ec4df402a6bc",
      "emailVerified": "2025-03-01T08:40:06.642Z"
    }
  ]
}
[auth][error] AdapterError: Read more at https://errors.authjs.dev#adaptererror
[auth][cause]: PrismaClientKnownRequestError: 
Invalid `})=>p.user.update()` invocation in
/Users/snow/Projects/dev-toolkit/.next/server/chunks/node_modules__pnpm_463d7d._.js:481:47

  478     });
  479     return account?.user ?? null;
  480 },
→ 481 updateUser: ({ id, ...data })=>p.user.update(
Raw query failed. Code: `unknown`. Message: `Kind: Command failed: Error code 14 (TypeMismatch): BSON field 'update.updates.u' is the wrong type 'array', expected type 'object', labels: {}`
    at Un.handleRequestError (/Users/snow/Projects/dev-toolkit/node_modules/.pnpm/@[email protected][email protected][email protected]/node_modules/@prisma/client/runtime/library.js:121:7447)
    at Un.handleAndLogRequestError (/Users/snow/Projects/dev-toolkit/node_modules/.pnpm/@[email protected][email protected][email protected]/node_modules/@prisma/client/runtime/library.js:121:6771)
    at Un.request (/Users/snow/Projects/dev-toolkit/node_modules/.pnpm/@[email protected][email protected][email protected]/node_modules/@prisma/client/runtime/library.js:121:6478)
    at async l (/Users/snow/Projects/dev-toolkit/node_modules/.pnpm/@[email protected][email protected][email protected]/node_modules/@prisma/client/runtime/library.js:130:9644)
    at async acc.<computed> (/Users/snow/Projects/dev-toolkit/.next/server/chunks/c1962_@auth_core_b84366._.js:1364:24)
    at async handleLoginOrRegister (/Users/snow/Projects/dev-toolkit/.next/server/chunks/c1962_@auth_core_b84366._.js:2678:20)
    at async Module.callback (/Users/snow/Projects/dev-toolkit/.next/server/chunks/c1962_@auth_core_b84366._.js:3934:64)
    at async AuthInternal (/Users/snow/Projects/dev-toolkit/.next/server/chunks/c1962_@auth_core_b84366._.js:4859:24)
    at async Auth (/Users/snow/Projects/dev-toolkit/.next/server/chunks/c1962_@auth_core_b84366._.js:5120:34)
    at async AppRouteRouteModule.do (/Users/snow/Projects/dev-toolkit/node_modules/.pnpm/[email protected][email protected][email protected]/node_modules/next/dist/compiled/next-server/app-route.runtime.dev.js:10:32847)
    at async AppRouteRouteModule.handle (/Users/snow/Projects/dev-toolkit/node_modules/.pnpm/[email protected][email protected][email protected]/node_modules/next/dist/compiled/next-server/app-route.runtime.dev.js:10:39868)
    at async doRender (/Users/snow/Projects/dev-toolkit/node_modules/.pnpm/[email protected][email protected][email protected]/node_modules/next/dist/server/base-server.js:1452:42)
    at async responseGenerator (/Users/snow/Projects/dev-toolkit/node_modules/.pnpm/[email protected][email protected][email protected]/node_modules/next/dist/server/base-server.js:1822:28)
    at async DevServer.renderToResponseWithComponentsImpl (/Users/snow/Projects/dev-toolkit/node_modules/.pnpm/[email protected][email protected][email protected]/node_modules/next/dist/server/base-server.js:1832:28)
    at async DevServer.renderPageComponent (/Users/snow/Projects/dev-toolkit/node_modules/.pnpm/[email protected][email protected][email protected]/node_modules/next/dist/server/base-server.js:2259:24)
    at async DevServer.renderToResponseImpl (/Users/snow/Projects/dev-toolkit/node_modules/.pnpm/[email protected][email protected][email protected]/node_modules/next/dist/server/base-server.js:2297:32)
    at async DevServer.pipeImpl (/Users/snow/Projects/dev-toolkit/node_modules/.pnpm/[email protected][email protected][email protected]/node_modules/next/dist/server/base-server.js:959:25)
    at async NextNodeServer.handleCatchallRenderRequest (/Users/snow/Projects/dev-toolkit/node_modules/.pnpm/[email protected][email protected][email protected]/node_modules/next/dist/server/next-server.js:281:17)
    at async DevServer.handleRequestImpl (/Users/snow/Projects/dev-toolkit/node_modules/.pnpm/[email protected][email protected][email protected]/node_modules/next/dist/server/base-server.js:853:17)
    at async /Users/snow/Projects/dev-toolkit/node_modules/.pnpm/[email protected][email protected][email protected]/node_modules/next/dist/server/dev/next-dev-server.js:371:20
    at async Span.traceAsyncFn (/Users/snow/Projects/dev-toolkit/node_modules/.pnpm/[email protected][email protected][email protected]/node_modules/next/dist/trace/trace.js:153:20)
    at async DevServer.handleRequest (/Users/snow/Projects/dev-toolkit/node_modules/.pnpm/[email protected][email protected][email protected]/node_modules/next/dist/server/dev/next-dev-server.js:368:24)
    at async invokeRender (/Users/snow/Projects/dev-toolkit/node_modules/.pnpm/[email protected][email protected][email protected]/node_modules/next/dist/server/lib/router-server.js:230:21)
    at async handleRequest (/Users/snow/Projects/dev-toolkit/node_modules/.pnpm/[email protected][email protected][email protected]/node_modules/next/dist/server/lib/router-server.js:408:24)
    at async requestHandlerImpl (/Users/snow/Projects/dev-toolkit/node_modules/.pnpm/[email protected][email protected][email protected]/node_modules/next/dist/server/lib/router-server.js:432:13)
    at async Server.requestListener (/Users/snow/Projects/dev-toolkit/node_modules/.pnpm/[email protected][email protected][email protected]/node_modules/next/dist/server/lib/start-server.js:146:13)
[auth][details]: {}

The Prisma schema:

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

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

model User {
  id            String    @id @default(auto()) @map("_id") @db.ObjectId
  name          String?
  email         String?   @unique
  emailVerified DateTime?
  image         String?
  customer_id   String?
  accounts      Account[]
  sessions      Session[]

  createdAt DateTime @default(now())
  updatedAt DateTime @updatedAt
}

model Account {
  id                String  @id @default(auto()) @map("_id") @db.ObjectId
  userId            String  @db.ObjectId
  type              String
  provider          String
  providerAccountId String
  refresh_token     String? @db.String
  access_token      String? @db.String
  expires_at        Int?
  token_type        String?
  scope             String?
  id_token          String? @db.String
  session_state     String?

  createdAt DateTime @default(now())
  updatedAt DateTime @updatedAt

  user User @relation(fields: [userId], references: [id], onDelete: Cascade)

  @@unique([provider, providerAccountId])
}

model Session {
  id           String   @id @default(auto()) @map("_id") @db.ObjectId
  sessionToken String   @unique
  userId       String   @db.ObjectId
  expires      DateTime
  user         User     @relation(fields: [userId], references: [id], onDelete: Cascade)

  createdAt DateTime @default(now())
  updatedAt DateTime @updatedAt
}

model VerificationToken {
  id         String   @id @default(auto()) @map("_id") @db.ObjectId
  identifier String
  token      String
  expires    DateTime

  @@unique([identifier, token])
}

My SignIn button following the documentation:

"use client";

import {
  Dialog,
  DialogContent,
  DialogFooter,
  DialogHeader,
  DialogTitle,
  DialogTrigger,
} from "@/components/ui/dialog";
import { Input } from "@/components/ui/input";
import { ErrorWithMessage, reportError } from "@/utils/errors";
import { zodResolver } from "@hookform/resolvers/zod";
import { Loader2, Send } from "lucide-react";
import { signIn, signOut, useSession } from "next-auth/react";
import { useState } from "react";
import { useForm } from "react-hook-form";
import { z } from "zod";
import { Button } from "./ui/button";
import {
  Form,
  FormControl,
  FormDescription,
  FormField,
  FormItem,
  FormLabel,
  FormMessage,
} from "./ui/form";

const FormSchema = z.object({
  email: z.string().email({ message: "Please provide a valid email." }),
});

export function SignInButton() {
  const { data: session } = useSession();
  const [isSignOutLoading, setIsSignOutLoading] = useState(false);
  const [isLoading, setIsLoading] = useState(false);
  const [isOpen, setIsOpen] = useState(false);
  const form = useForm<z.infer<typeof FormSchema>>({
    resolver: zodResolver(FormSchema),
    defaultValues: {
      email: "",
    },
  });

  async function onSubmit(data: z.infer<typeof FormSchema>) {
    setIsLoading(true);

    try {
      await signIn("resend", data);
      form.reset();
      setIsOpen(false);
    } catch (error: ErrorWithMessage | unknown) {
      reportError(error);
    } finally {
      setIsLoading(false);
    }
  }

  const handleSignOut = async () => {
    setIsSignOutLoading(true);

    try {
      await signOut();
    } catch (error: ErrorWithMessage | unknown) {
      reportError(error);
    } finally {
      setIsSignOutLoading(false);
    }
  };

  if (session) {
    return (
      <Button
        variant="outline"
        onClick={handleSignOut}
        disabled={isSignOutLoading}
      >
        {isSignOutLoading && <Loader2 className="size-3 animate-spin" />}
        Sign out
      </Button>
    );
  }

  return (
    <Dialog open={isOpen} onOpenChange={setIsOpen}>
      <DialogTrigger asChild>
        <Button variant="outline">Sign in</Button>
      </DialogTrigger>
      <DialogContent>
        <Form {...form}>
          <form onSubmit={form.handleSubmit(onSubmit)}>
            <DialogHeader className="mb-4">
              <DialogTitle>Sign in with a simple link</DialogTitle>
            </DialogHeader>

            <FormField
              control={form.control}
              name="email"
              render={({ field }) => (
                <FormItem>
                  <FormLabel>Email</FormLabel>
                  <FormControl>
                    <Input placeholder="Email" {...field} />
                  </FormControl>
                  <FormDescription>
                    We&apos;ll send you a sign in link to this email
                  </FormDescription>
                  <FormMessage />
                </FormItem>
              )}
            />

            <DialogFooter className="mt-4">
              <Button variant="ghost">Close</Button>
              <Button type="submit" disabled={isLoading}>
                {isLoading && <Loader2 className="size-3 animate-spin" />}
                Send sign in link <Send />
              </Button>
            </DialogFooter>
          </form>
        </Form>
      </DialogContent>
    </Dialog>
  );
}

Any help will be appreciated!

Thx.

Upvotes: 0

Views: 13

Answers (0)

Related Questions