-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
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
Typescript errors when creating a custom User model #861
Comments
I'm getting exactly the same errors on a new project. As a temp fix, I updated the import method: import Adapters, {TypeORMUserModel} from 'next-auth/adapters'
export default class User extends TypeORMUserModel {
...
} |
Using
|
So - it's not pretty, but this works for me. FYI I'm adding a Let me know if you know a way to tidy this up a little.
import Adapters from 'next-auth/adapters'
import { EntitySchemaColumnOptions } from 'typeorm'
export default class User extends (<any>Adapters.TypeORM.Models.User.model) {
constructor(name, email, image, emailVerified) {
super(name, email, image, emailVerified)
}
}
type UserSchema = {
name: string
target: typeof User
columns: {
username?: {
type: 'varchar'
nullable: boolean
}
name?: EntitySchemaColumnOptions
email?: EntitySchemaColumnOptions
image?: EntitySchemaColumnOptions
emailVerified?: EntitySchemaColumnOptions
}
}
export const UserSchema: UserSchema = {
name: 'User',
target: User,
columns: {
...Adapters.TypeORM.Models.User.schema.columns,
username: {
type: 'varchar',
nullable: true,
},
},
} |
This appears to work for me and allows for proper type completions. import Adapters, {TypeORMUserModel} from 'next-auth/adapters';
export default class User extends (Adapters.TypeORM.Models.User.model as typeof TypeORMUserModel) { |
I got it working with the following code, I specified each model needed for Adapter:
adapter: Adapters.TypeORM.Adapter(process.env.MONGODB_URI!, {
models: {
// I suppose you could also spread models here ...Adapters.TypeORM.Models
Account: Adapters.TypeORM.Models.Account,
Session: Adapters.TypeORM.Models.Session,
VerificationRequest: Adapters.TypeORM.Models.VerificationRequest,
User: Models.User,
},
})
export default class User extends (<any>Adapters.TypeORM.Models.User.model) {
constructor(
name: string,
email: string,
image: string,
emailVerified: Date | undefined
) {
super(name, email, image, emailVerified);
if (!this.username) this.username = this.name;
if (!this.isFollowed) this.isFollowed = false;
}
}
type UserSchema = {
name: string;
target: typeof TypeORMUserModel;
columns: {
username?: {
type: "varchar";
nullable: boolean;
};
isFollowed?: {
type: "boolean";
};
name?: EntitySchemaColumnOptions;
email?: EntitySchemaColumnOptions;
image?: EntitySchemaColumnOptions;
emailVerified?: EntitySchemaColumnOptions;
};
};
export const UserSchema: UserSchema = {
name: "User",
target: User,
columns: {
...Adapters.TypeORM.Models.User.schema.columns,
username: {
type: "varchar",
nullable: true,
},
isFollowed: {
type: "boolean",
},
},
}; |
Gives me following error "TypeError: Class extends value undefined is not a constructor or null" at local dev |
Not really pretty, but works perfect |
How to make a custom User model in Typescript and add it to NextAuth options?
I am trying to make a custom user model (following the tutorial https://next-auth.js.org/tutorials/typeorm-custom-models), but the app won't build because of typescript errors. It runs with "yarn dev" command and works ok with this model, but it throws errors with "yarn build" command.
The first error is in the custom model User.ts which I created, in the class definition:
The string "Adapters.TypeORM.Models.User.model" is underlined in red and the error says: "Type 'TypeORMUserModel' is not a constructor function type"
The second typescript error is in pages/api/auth/[...nextauth].ts in options:
The string "User:" is underlined in red and the error says:
My custom User model (copied from the tutorial):
The text was updated successfully, but these errors were encountered: