Theo's Typesafe CultTTC
Theo's Typesafe Cult3y ago
3 replies
ruhroh

Typescript error when trying to access more user properties in my JWT token

Hello! I am using Next Auth and I have it built so that users will also have a username, and in my auth file, my jwt callback looks like this:
    jwt: ({ token, user }) => {
      console.log("JWT CALLBACK");
      console.log({ user });
      if (user) {
        token.id = user.id;
        token.email = user.email;
        token.name = user.name;
        token.username = user.username;
      }
      console.log({ token });
      return token;
    },

Unfortunately, I am getting this TypeScript error:
Property 'username' does not exist on type 'User | AdapterUser'.


After looking into it, I found that the user type is defined in the node_modules folder, and this is what both
User
and
AdapterUser
looks like:
export interface AdapterUser extends User {
  id: string
  email: string
  emailVerified: Date | null
  username: string
}
// and
export interface DefaultUser {
  id: string
  name?: string | null
  email?: string | null
  image?: string | null
  username?: string | null
}

export interface User extends DefaultUser {}


As you can see I've tried adding username to either or, and both, but I'm still getting the same error (after a ts server refresh too) and my app also will not build because of it. Any help would be appreciated. Thank you!
Was this page helpful?