nextauthjs / nextauthjs/next-auth

More Prisma adapter fails when trying to create a user when using Keycloak

Open
#7,129 5 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

adapters prisma triage
Dominant language
TypeScript
Stars
28.4k
Forks
4k
PR merge metrics
No merged PRs in 30d

Description

Adapter type

@next-auth/prisma-adapter

Environment

System:
OS: Windows 10 10.0.22621
CPU: (16) x64 12th Gen Intel(R) Core(TM) i7-1260P
Memory: 1.90 GB / 15.62 GB
Binaries:
Node: 16.19.0 - C:\Program Files\nodejs\node.EXE
Yarn: 3.5.0 - C:\Program Files\nodejs\yarn.CMD
npm: 8.19.3 - C:\Program Files\nodejs\npm.CMD
Browsers:
Edge: Spartan (44.22621.1413.0), Chromium (111.0.1661.54)
Internet Explorer: 11.0.22621.1

npmPackages:
@next-auth/prisma-adapter: ^1.0.5 => 1.0.5

Reproduction URL

https://github.com/hrueger/authjs-prisma-issue

Describe the issue

This is a duplicate of #3828, but that was closed because it did not include a reproduction.

If keycloak is used as the authentication provider, it returns quite a lot of attributes. As they are spread into the prisma user / account create call, things break because prisma does not like extra attributes.

14:36:46 [vite] page reload src/hooks.server.ts
[auth][error][AdapterError]: Read more at https://errors.authjs.dev#adaptererror
[auth][cause]: Error: 
Invalid `p.account.create()` invocation in
C:\_Source\authjs-prisma-isue\node_modules\@next-auth\prisma-adapter\dist\index.js:19:42

  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({
       data: {
         provider: 'keycloak',
         type: 'oidc',
         providerAccountId: '46dc0eda-e92d-41b4-a06d-10f3daac251a',   
         access_token: 'redacted',     
         expires_in: 300,
         ~~~~~~~~~~
         refresh_expires_in: 1800,
         ~~~~~~~~~~~~~~~~~~
         refresh_token: 'redacted',
         token_type: 'bearer',
         id_token: 'redacted',
         'not-before-policy': 0,
         session_state: '8d0d41cc-26b9-479f-92c1-02c2d0201bb5',       
         scope: 'openid profile email',
         userId: 'clfyhumnm0000rv1kvqxknus5'
       }
     })

Unknown arg `expires_in` in data.expires_in for type AccountUncheckedCreateInput. Did you mean `expires_at`? Available args:
type AccountUncheckedCreateInput {
  id?: String
  userId: String
  type: String
  provider: String
  providerAccountId: String
  refresh_token?: String | Null
  access_token?: String | Null
  expires_at?: Int | Null
  token_type?: String | Null
  scope?: String | Null
  id_token?: String | Null
  session_state?: String | Null
}
Unknown arg `refresh_expires_in` in data.refresh_expires_in for type AccountUncheckedCreateInput. Available args:

type AccountUncheckedCreateInput {
  id?: String
  userId: String
  type: String
  provider: String
  providerAccountId: String
  refresh_token?: String | Null
  access_token?: String | Null
  expires_at?: Int | Null
  token_type?: String | Null
  scope?: String | Null
  id_token?: String | Null
  session_state?: String | Null
}
Unknown arg `not-before-policy` in data.not-before-policy for type AccountUncheckedCreateInput. Available args:

type AccountUncheckedCreateInput {
  id?: String
  userId: String
  type: String
  provider: String
  providerAccountId: String
  refresh_token?: String | Null
  access_token?: String | Null
  expires_at?: Int | Null
  token_type?: String | Null
  scope?: String | Null
  id_token?: String | Null
  session_state?: String | Null
}


    at mi.validate (C:\_Source\authjs-prisma-isue\node_modules\@prisma\client\runtime\library.js:149:91)
    at rn.createMessage (C:\_Source\authjs-prisma-isue\node_modules\@prisma\client\runtime\library.js:164:13651)
    at C:\_Source\authjs-prisma-isue\node_modules\@prisma\client\runtime\library.js:177:10955
    at runInChildSpan (C:\_Source\authjs-prisma-isue\node_modules\@prisma\client\runtime\library.js:70:25817)
    at t._executeRequest (C:\_Source\authjs-prisma-isue\node_modules\@prisma\client\runtime\library.js:177:10944)
[auth][details]: {}
[auth][error][AdapterError]: Read more at https://errors.authjs.dev#adaptererror
How to reproduce

See the readme of the repository, but here are the steps again:

  • clone the repo
  • docker compose up -d
  • wait for a minute, until keycloak is ready
  • go to localhost:8095
  • login with username user and password bitnami
  • Create a new realm (by clicking on master), call it Test
  • Go to clients and create a client (call it authjs)
  • Select authentication: yes
  • set redirect uris to * and save
  • go to credentials, copy client secret and add the following to .env:
    KEYCLOAK_CLIENT_ID=authjs
    KEYCLOAK_CLIENT_SECRET=YOUR_CLIENT_SECRET
    
  • run yarn
  • run yarn dev
  • go to localhost:5137, click on login, create a user and login. Then, you get the error.
Expected behavior

The database row is created successfully and the user can login.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the reproduction repository and its README, then inspect the failing @next-auth/prisma-adapter entry point shown at node_modules/@next-auth/prisma-adapter/dist/index.js. Run the listed Keycloak login steps and compare the account data with the Prisma Account fields in the error. Done means the database row is created successfully and the user can log in.

Written by the indexing model from the issue text.

Assessment

Tech stack
node.js, typescript
Domain
authentication, database
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.