Next-auth: Document how to use custom models

Created on 18 Jun 2020  路  3Comments  路  Source: nextauthjs/next-auth

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:

  • The id property (_id in MongoDB) is assumed on all models.
  • The name property is assumed for the users name.
  • The email property is assumed in the user model.

Impact / mitigation:

  • If the name property is renamed then the users name will not be returned in the session / JWT. This could be mitigated by adding an additional property to the session option that is an array of all properties on the model that should be returned in the session.
    e.g. with a default value of of ['name', 'email']
    Alternatively, this could be a function on the model (to keep all that logic together).
  • If the email property is renamed then email sign in will not work correctly. This could be mitigated by making this field an option.

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

  • Provides a way to address an issue raised in #254 (RE: UUID's).
enhancement

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:

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:

  • override the id property without changing its name, to add the PrimaryGeneratedColumn.
  • don't include other required properties like email, name, password, created_at, updated_at.
  • add a new property : avatarKey

Is it the way to extend?

All 3 comments

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 same
  • createdAt, updatedAt have different names
  • activatedAt, avatarKey are additional properties

I 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:

  • override the id property without changing its name, to add the PrimaryGeneratedColumn.
  • don't include other required properties like email, name, password, created_at, updated_at.
  • add a new property : avatarKey

Is it the way to extend?

Closing this as this has been added in #401

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ghoshnirmalya picture ghoshnirmalya  路  3Comments

loonskai picture loonskai  路  3Comments

benoror picture benoror  路  3Comments

iaincollins picture iaincollins  路  3Comments

alex-cory picture alex-cory  路  3Comments