nextauthjs / next-auth

Authentication for the Web.
https://authjs.dev
ISC License
24.91k stars 3.51k forks source link

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

Closed igmtink closed 9 months ago

igmtink commented 10 months ago

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.

bocarw121 commented 10 months ago

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 commented 10 months ago

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 commented 10 months ago

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 commented 9 months ago

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 commented 9 months ago

@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 next-auth@5.0.0-beta.4 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 commented 9 months ago

@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 next-auth@5.0.0-beta.4 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 commented 9 months ago

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

bocarw121 commented 9 months ago

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 commented 9 months ago

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 commented 9 months ago

Awesome, my pleasure!

meruiden commented 9 months ago

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

bocarw121 commented 9 months ago

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

ahmedivy commented 9 months ago

also solves my issue ❤️

thearmanahmed commented 9 months ago

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/nodemodules/@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 commented 7 months ago

import ObjectId from 'bson-objectid'

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