Summary of proposed feature
Add documentation for how to use custom models with NextAuth.js
Purpose of proposed feature
It is possible to use custom models (e.g. for Users, Accounts, Sessions, etc) with NextAuth.js but there are no examples and documentation that cover how to actually do this.
Detail about proposed feature
We can add documentation that shows how to add or modify fields, and cover common use cases.
For example to use UUID's instead of int types for ID columns in an SQL databases, or to set a different default value for a property, or do expand an existing model to add additional fields or Foreign Key constraints.
Potential problems
Some properties on models are required / assumed by the Default Adapter and there will be problems if they are renamed in the model.
For example:
id property (_id in MongoDB) is assumed on all models.name property is assumed for the users name.email property is assumed in the user model. Impact / mitigation:
session option that is an array of all properties on the model that should be returned in the session.['name', 'email']I would not recommended we encourage people to rename properties, as it's likely to be problematic and there may lead to quite serious bugs that are not obvious at first.
Describe any alternatives you've considered
Alternatively , we could not let people pass custom models and instead document how to clone the default adapter do that instead.
Additional context
Hello @iaincollins,
I was trying to implement next-auth in my app and was just wondering how I could merge next-auth models with my own.
For now I declare my models in a entity.ts with typeorm:
import { Entity, PrimaryGeneratedColumn, BaseEntity, OneToMany } from "typeorm"
import { ObjectType, Field } from "type-graphql"
import { StringField } from "../shared/fields"
@ObjectType()
@Entity()
export class User extends BaseEntity {
@Field(() => ID)
@PrimaryGeneratedColumn()
id: number;
@StringField({ unique: true })
email: string
@StringField()
password: string
@StringField()
name: string
@Field()
createdAt: Date;
@Field({ nullable: true })
updatedAt: Date;
@Field({ nullable: true })
activatedAt: Date;
@StringField({ graphql: false, nullable: true })
avatarKey: string | null
}
Compared to next-auth models:
id, email, password, name are the samecreatedAt, updatedAt have different namesactivatedAt, avatarKey are additional propertiesI guess leaving it like that will conflict with next-auth. Best would be to have to declare models in one single place. Do you have tips for how to integrate next-auth with that kind of setup?
Hey @iaincollins,
I checked all projects currently using next-auth, unfortunately, none of them extend the models.
As next-auth uses typeorm itself, I was thinking of something like:
export class User extends NextAuthUserEntity {
@Field(() => ID)
@PrimaryGeneratedColumn()
id: number;
@StringField({ graphql: false, nullable: true })
avatarKey: string | null
}
With NextAuthUserEntity being the user model registered by next-auth. I would:
Is it the way to extend?
Closing this as this has been added in #401
Most helpful comment
Hey @iaincollins,
I checked all projects currently using next-auth, unfortunately, none of them extend the models.
As next-auth uses typeorm itself, I was thinking of something like:
With
NextAuthUserEntitybeing the user model registered by next-auth. I would:Is it the way to extend?