Next.js Discord

Discord Forum

After I upgraded to NextJS 14/13.5 from 13.4 next-auth is failing

Answered
Transvaal lion posted this in #help-forum
Open in Discord
Avatar
Transvaal lionOP
I tried to get an answer to this issue for weeks already and didn't manage to get any help.

After I upgraded to NextJS 13.5/14 from 13.4 I started to get this error each time I'm trying to login. I'm using next-auth with TypeORM-Adapter connected to PostgreSQL. The strange thing about it, is that I only get this error in production mode and not in development mode.

[next-auth][error][adapter_error_getSessionAndUser] 
https://next-auth.js.org/errors#adapter_error_getsessionanduser No metadata for "SessionEntity" was found. {
  message: 'No metadata for "SessionEntity" was found.',
  stack: 'EntityMetadataNotFoundError: No metadata for "SessionEntity" was found.\n' +
    '    at t.DataSource.getMetadata (/home/guy/workspace/falkordb-cloud/.next/server/chunks/871.js:52:45111)\n' +
    '    at t.EntityManager.findOne (/home/guy/workspace/falkordb-cloud/.next/server/chunks/871.js:149:25750)\n' +
    '    at getSessionAndUser (/home/guy/workspace/falkordb-cloud/.next/server/chunks/871.js:150:415819)\n' +
    '    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)',
  name: 'EntityMetadataNotFoundError'
}


I tried to play with the code, but I couldn't find a solution it seems like something has changed in the way nextjs is building the code in 13.5/14

Published on stackoverflow
https://stackoverflow.com/questions/77348457/error-https-next-auth-js-org-errorsadapter-error-getsessionanduser-no-metada

On next-auth repo
https://github.com/nextauthjs/next-auth/issues/8822
Answered by Transvaal lion
I found the issue:
If the application redefine 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 {
View full answer

1 Reply

Avatar
Transvaal lionOP
I found the issue:
If the application redefine 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 {
Answer