NextAuth Credentials, adding more to the user scheme

Nextauth with mysql persisting users. I’m trying out this NextAuth thing to see if this … Read more NextAuth Credentials, adding more to the user scheme

Nextauth with mysql persisting users.

I’m trying out this NextAuth thing to see if this is something I like. So far so good. There is one thing tho which is buggin me and that would be the user scheme. By default it returns a name, image and the last one I forgot.

I’d like to add more to this scheme and found some ways to do it by looking at google, however those I tried did not work.

One example I found is by extending the model which clearly makes sense…
The issue here is then me, I do not know what to change in the code below to make it work with my NextAuth credentials provider. As shown below, this doesnt work.

projectfolder -> models -> index.js

import User, { UserSchema } from "./User"
export default {
  User: {
    model: User,
    schema: UserSchema
  }
}

projectfolder -> models -> user.js

import Adapters from "next-auth/adapters"

// Extend the built-in models using class inheritance
export default class User extends Adapters.TypeORM.Models.User.model {
  constructor(name, email, image, emailVerified, roles) {
    super(name, email, image, emailVerified)
    if (roles) { this.roles = roles}
  }
}

export const UserSchema = {
  name: "User",
  target: User,
  columns: {
    ...Adapters.TypeORM.Models.User.schema.columns,
    roles: {
      type: "varchar",
      nullable: true
    },
  },
}

In my […nextauth].js file I have my provider, in this provider i’ve added an profile() field with the extra fields. This did not solve the issue.

profile(profile) {
    return {
        name: profile.name,
        email: profile.email,
        role: profile.role
    };
  },

Please correct me if I am wrong but if I am using credentials, then I need to replace the "TypeORM" with something else, correct? How about the path for the files, are they correct?

This should clearly be quite easy but am I missing something or am I doing something wrong? I feel like there is a lack of documentation on extending the user model for mysql.

I’ve doubled checked that the role is being retrieved from the database and then added to the user variable shown here:

async authorize ….

const user = {
   name: result.display_name,
   role: result.role_name,
   email: result.email
}

Although I can see the role being set in the variable with my console.log(), I still cannot access the role and that I suspect is because of the model. How would I resolve this? Thanks a lot in advance.

———————– UPDATES ————————

Btw, here is my callback

callbacks: {  
    async signIn({ user, account, profile, email }) {
      console.log("user", user);
      return true;
    },

  },

and this is what it returns (shortened)

token: {
        token: { name: 'Firstname Lastname', email: '[email protected]' },
        user: {
          name: 'Firstname Lastname',
          role: 'administrator',
          email: '[email protected]'
        },
        account: { type: 'credentials', provider: 'credentials' },
        isNewUser: false,
        iat: 1634193197,
        exp: 1636785197
      }

Source: JavaSript – Stack Overflow



Leave a Reply

Your email address will not be published. Required fields are marked *