Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

next-auth v5 - logging in using (oauth provider) with prisma adapter & mongdb #9685

Closed
igmtink opened this issue Jan 19, 2024 · 15 comments · Fixed by #9797
Closed

next-auth v5 - logging in using (oauth provider) with prisma adapter & mongdb #9685

igmtink opened this issue Jan 19, 2024 · 15 comments · Fixed by #9797
Labels
adapters Changes related to the core code concerning database adapters bug Something isn't working core Refers to `@auth/core` priority Priority fix or enhancement

Comments

@igmtink
Copy link

igmtink commented Jan 19, 2024

Environment

System:
OS: Linux 5.15 Ubuntu 22.04.2 LTS 22.04.2 LTS (Jammy Jellyfish)
CPU: (12) x64 AMD Ryzen 5 4600G with Radeon Graphics
Memory: 275.91 MB / 3.48 GB
Container: Yes
Shell: 3.6.1 - /home/linuxbrew/.linuxbrew/bin/fish
Binaries:
Node: 21.3.0 - /home/linuxbrew/.linuxbrew/bin/node
npm: 10.2.4 - /home/linuxbrew/.linuxbrew/bin/npm
pnpm: 8.14.0 - ~/.local/share/pnpm/pnpm
bun: 1.0.23 - ~/.bun/bin/bun
npmPackages:
@auth/prisma-adapter: ^1.0.14 => 1.0.15
next: latest => 14.1.0
next-auth: beta => 5.0.0-beta.5
react: ^18 => 18.2.0

Reproduction URL

https://github.com/igmtink/igmt-auth

Describe the issue

Whenever I logging in using oauth provider which is google provider I've got this error:

image

How to reproduce

Sign in with Google

Expected behavior

Create the user in my mongodb after logging in with google provider, and successfully sign in.

@igmtink igmtink added bug Something isn't working triage Unseen or unconfirmed by a maintainer yet. Provide extra information in the meantime. labels Jan 19, 2024
@bocarw121
Copy link

Hey,

I was going through your repo and it seems like there is an issue with the createUser method inside of the PrismaAdapter.

What I did to solve it is spread the PrismaAdapter(db) inside of the NextAuth adapter method and override the createUser method and pass in an objectId that the schema is expecting.

inside of your auth.ts do this

install the bson-objectid and import it

import ObjectId from 'bson-objectid'

update your adapter to look like this

adapter: {
    ...PrismaAdapter(db),
    async createUser(user) {
      return await db.user.create({
        data: {
          ...user,
          id: ObjectId().toHexString()
        }
      })
    }
  }

That should fix the issue. Hope this helps!

@igmtink
Copy link
Author

igmtink commented Jan 19, 2024

Hey,

I was going through your repo and it seems like there is an issue with the createUser method inside of the PrismaAdapter.

What I did to solve it is spread the PrismaAdapter(db) inside of the NextAuth adapter method and override the createUser method and pass in an objectId that the schema is expecting.

inside of your auth.ts do this

install the bson-objectid and import it

import ObjectId from 'bson-objectid'

update your adapter to look like this

adapter: {
    ...PrismaAdapter(db),
    async createUser(user) {
      return await db.user.create({
        data: {
          ...user,
          id: ObjectId().toHexString()
        }
      })
    }
  }

That should fix the issue. Hope this helps!

Thank you so much sir ☺️

@bocarw121
Copy link

Hey,
I was going through your repo and it seems like there is an issue with the createUser method inside of the PrismaAdapter.
What I did to solve it is spread the PrismaAdapter(db) inside of the NextAuth adapter method and override the createUser method and pass in an objectId that the schema is expecting.
inside of your auth.ts do this
install the bson-objectid and import it
import ObjectId from 'bson-objectid'
update your adapter to look like this

adapter: {
    ...PrismaAdapter(db),
    async createUser(user) {
      return await db.user.create({
        data: {
          ...user,
          id: ObjectId().toHexString()
        }
      })
    }
  }

That should fix the issue. Hope this helps!

Thank you so much sir ☺️

My Pleasure!

@igmtink
Copy link
Author

igmtink commented Jan 20, 2024

Hey,

I was going through your repo and it seems like there is an issue with the createUser method inside of the PrismaAdapter.

What I did to solve it is spread the PrismaAdapter(db) inside of the NextAuth adapter method and override the createUser method and pass in an objectId that the schema is expecting.

inside of your auth.ts do this

install the bson-objectid and import it

import ObjectId from 'bson-objectid'

update your adapter to look like this

adapter: {
    ...PrismaAdapter(db),
    async createUser(user) {
      return await db.user.create({
        data: {
          ...user,
          id: ObjectId().toHexString()
        }
      })
    }
  }

That should fix the issue. Hope this helps!

Good day sir, I have another problem with deploying in vercel

image

@bocarw121
Copy link

@igmtink

Hey, you can do this for now

adapter: {
    ...PrismaAdapter(db),
    createUser(user) {
      return db.user.create({
        data: {
          ...user,
          id: ObjectId().toHexString()
        }
      }) as any
    }

You actually don't have to add async to the create user as it returns the promise.

Just to let you know your not exporting update from auth.ts and it doesn't seem to be available as an export from the NextAuth function.

You should install npm install [email protected] and add update under the signOut function.

Also keep running npm run build to make sure there aren't any other errors before pushing to Vercel.

Let me know if you have other questions.

Take care.

@igmtink
Copy link
Author

igmtink commented Jan 21, 2024

@igmtink

Hey, you can do this for now

adapter: {
    ...PrismaAdapter(db),
    createUser(user) {
      return db.user.create({
        data: {
          ...user,
          id: ObjectId().toHexString()
        }
      }) as any
    }

You actually don't have to add async to the create user as it returns the promise.

Just to let you know your not exporting update from auth.ts and it doesn't seem to be available as an export from the NextAuth function.

You should install npm install [email protected] and add update under the signOut function.

Also keep running npm run build to make sure there aren't any other errors before pushing to Vercel.

Let me know if you have other questions.

Take care.

Still have this type error sir

image

@igmtink
Copy link
Author

igmtink commented Jan 21, 2024

I moved the "as any" in the end of bracket of "data: {} as any" then the error is gone

@bocarw121
Copy link

That's strange the type error disappeared on my side after adding as any to the end of the createUser method that had the type error. Great to hear its good now 😄. Are you able to deploy it?

@igmtink
Copy link
Author

igmtink commented Jan 21, 2024

That's strange the type error disappeared on my side after adding as any to the end of the createUser method that had the type error. Great to hear its good now 😄. Are you able to deploy it?

Yes thank you so much 😊

@bocarw121
Copy link

Awesome, my pleasure!

@meruiden
Copy link

Woah, this fix helps me too, thanks! It's because they try to store the id from the oauth provider, just opened #9699

@bocarw121
Copy link

@meruiden Great to hear it helped and thanks for opening the issue.

@ahmedivy
Copy link

also solves my issue ❤️

@balazsorban44 balazsorban44 added adapters Changes related to the core code concerning database adapters core Refers to `@auth/core` priority Priority fix or enhancement and removed triage Unseen or unconfirmed by a maintainer yet. Provide extra information in the meantime. labels Jan 25, 2024
@thearmanahmed
Copy link

import NextAuth from "next-auth";
import { Adapter } from "@auth/core/adapters";

declare module "next-auth" {
interface User {
/** The user's postal address. */
role: "ADMIN" | "USER";
}
}

import authConfig from "./auth.config";
import { db } from "./lib/db";
import { getUserById } from "./data/user";
import { PrismaAdapter } from "@auth/prisma-adapter";

export const {
handlers: { GET, POST },
auth,
signIn,
signOut,
} = NextAuth({
callbacks: {
async session({ session, token }) {
console.log({
sessionToken: token,
});

  if (token.sub && session.user) {
    session.user.id = token.sub;
  }

  console.log(token.role);

  if (token.role && session.user) {
    session.user.role = token.role;
  }

  return {
    ...session,
  };
},
async jwt({ token }) {
  if (!token.sub) return token;

  const existingUser = await getUserById(token.sub);

  if (!existingUser) return token;

  token.role = existingUser.role;

  return token;
},

},
adapter: PrismaAdapter(db),
session: { strategy: "jwt" },
...authConfig,
});

Type 'import("e:/Users/abdurahman a.mohamed/Desktop/next/mad/node_modules/@auth/core/adapters").Adapter' is not assignable to type 'import("e:/Users/abdurahman a.mohamed/Desktop/next/mad/node_modules/next-auth/node_modules/@auth/core/adapters").Adapter'.
Types of property 'createUser' are incompatible.
Type '((user: import("e:/Users/abdurahman a.mohamed/Desktop/next/mad/node_modules/@auth/core/adapters").AdapterUser) => import("e:/Users/abdurahman a.mohamed/Desktop/next/mad/node_modules/@auth/core/types").Awaitable<import("e:/Users/abdurahman a.mohamed/Desktop/next/mad/node_modules/@auth/core/adapters").AdapterUser>) | ...' is not assignable to type '((user: import("e:/Users/abdurahman a.mohamed/Desktop/next/mad/node_modules/next-auth/node_modules/@auth/core/adapters").AdapterUser) => import("e:/Users/abdurahman a.mohamed/Desktop/next/mad/node_modules/next-auth/node_modules/@auth/core/types").Awaitable<import("e:/Users/abdurahman a.mohamed/Desktop/next/mad/node_...'.
Type '(user: import("e:/Users/abdurahman a.mohamed/Desktop/next/mad/node_modules/@auth/core/adapters").AdapterUser) => import("e:/Users/abdurahman a.mohamed/Desktop/next/mad/node_modules/@auth/core/types").Awaitable<import("e:/Users/abdurahman a.mohamed/Desktop/next/mad/node_modules/@auth/core/adapters").AdapterUser>' is not assignable to type '(user: import("e:/Users/abdurahman a.mohamed/Desktop/next/mad/node_modules/next-auth/node_modules/@auth/core/adapters").AdapterUser) => import("e:/Users/abdurahman a.mohamed/Desktop/next/mad/node_modules/next-auth/node_modules/@auth/core/types").Awaitable<import("e:/Users/abdurahman a.mohamed/Desktop/next/mad/node_m...'.
Type 'import("e:/Users/abdurahman a.mohamed/Desktop/next/mad/node_modules/@auth/core/types").Awaitable<import("e:/Users/abdurahman a.mohamed/Desktop/next/mad/node_modules/@auth/core/adapters").AdapterUser>' is not assignable to type 'import("e:/Users/abdurahman a.mohamed/Desktop/next/mad/node_modules/next-auth/node_modules/@auth/core/types").Awaitable<import("e:/Users/abdurahman a.mohamed/Desktop/next/mad/node_modules/next-auth/node_modules/@auth/core/adapters").AdapterUser>'.
Type 'AdapterUser' is not assignable to type 'Awaitable'.
Property 'role' is missing in type 'import("e:/Users/abdurahman a.mohamed/Desktop/next/mad/node_modules/@auth/core/adapters").AdapterUser' but required in type 'import("e:/Users/abdurahman a.mohamed/Desktop/next/mad/node_modules/next-auth/node_modules/@auth/core/adapters").AdapterUser'.

@shashankbhat2
Copy link

import ObjectId from 'bson-objectid'

Thank you so much!!! was struggling for a long time to fix this issue!!!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
adapters Changes related to the core code concerning database adapters bug Something isn't working core Refers to `@auth/core` priority Priority fix or enhancement
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants