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:
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:
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!2 Replies
Next-auth uses Module Augmentation to customize the types, so you don't need to change the node_modules: https://next-auth.js.org/getting-started/typescript#module-augmentation
Anyways, to solve this, type the following:
Read all my comments in the code, also make sure to retrieve the username from the provider in my case using Github I get the username like this:
TypeScript | NextAuth.js
NextAuth.js has its own type definitions to use in your TypeScript projects safely. Even if you don't use TypeScript, IDEs like VSCode will pick this up to provide you with a better developer experience. While you are typing, you will get suggestions about what certain objects/functions look like, and sometimes links to documentation, examples, ...
That is extremely helpful, thank you so much!