nextauthjs / next-auth

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

message: 'No metadata for "SessionEntity" was found.' after upgrade to latest #8822

Closed gkorland closed 1 year ago

gkorland commented 1 year ago

Environment

System: OS: Linux 6.2 Ubuntu 23.04 23.04 (Lunar Lobster) CPU: (8) x64 11th Gen Intel(R) Core(TM) i7-1165G7 @ 2.80GHz Memory: 5.44 GB / 31.06 GB Container: Yes Shell: 5.2.15 - /bin/bash Binaries: Node: 17.6.0 - ~/.nvm/versions/node/v17.6.0/bin/node npm: 8.5.1 - ~/.nvm/versions/node/v17.6.0/bin/npm pnpm: 8.7.4 - ~/.nvm/versions/node/v17.6.0/bin/pnpm Browsers: Chrome: 117.0.5938.149

Reproduction URL

https://github.com/FalkorDB/falkordb-cloud/

Describe the issue

After I upgrade to the latest version I started to get this error:

https://next-auth.js.org/errors#oauth_callback_handler_error No metadata for "AccountEntity" was found. s: No metadata for "AccountEntity" was found.
    at t.DataSource.getMetadata (/var/task/.next/server/chunks/982.js:52:42515)
    at t.EntityManager.findOne (/var/task/.next/server/chunks/982.js:149:25680)
    at getUserByAccount (/var/task/.next/server/chunks/982.js:150:427302)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5) {
  name: 'GetUserByAccountError',
  code: undefined
}
"next": "^13.4.19" -> "next": "^13.5.4"
"next-auth": "^4.23.0" -> "next-auth": "^4.23.2"

"typeorm": "^0.3.17" "@auth/typeorm-adapter": "^1.0.4"

How to reproduce

  1. Use latest versions of Next and Auth.js
  2. Use TypeORM-Adapter
  3. Login with Google/Github

Expected behavior

Login without any error

gkorland commented 1 year ago

Just tried with the latest next:"13.5.5" and I'm still getting the same error

gkorland commented 1 year ago

@balazsorban44

gkorland commented 1 year ago

I found the issue: If the application redefines the entities used by the TypeORM-Adapter (like I do) As explained in https://authjs.dev/reference/adapter/typeorm#advanced-usage

After Next 13.5 it seems like Next is shortening the classnames in build time, e.g. from AccountEntity to a The problem is that TypeORM is using the classname by default for the class lookup. The solution is to set the classname in the decorator e.g.

@Entity("AccountEntity", { name: "accounts" })
export class AccountEntity {

https://nextjs-forum.com/post/1168081358984331274

gkorland commented 1 year ago

@balazsorban44 I suggest to fix the docs