TypeScript type error in NextAuth
Unanswered
Spectacled bear posted this in #help-forum
Spectacled bearOP
I'm getting the following errors in the NextAuth route.
and
Code file is below:
Property 'hashedPassword' does not exist on type 'User | AdapterUser'.
Property 'hashedPassword' does not exist on type 'User'.and
Property 'password' does not exist on type 'Profile'.Code file is below:
41 Replies
@Spectacled bear I'm getting the following errors in the NextAuth route.
Property 'hashedPassword' does not exist on type 'User | AdapterUser'.
Property 'hashedPassword' does not exist on type 'User'.
and
Property 'password' does not exist on type 'Profile'.
Code file is below:
As far as I can see, your user object only has a property called
hashPassword, not hashedPasswordSpectacled bearOP
you mean the model?
@Spectacled bear you mean the model?
Basically yes. Prisma creates a type for that with the properties you can see there and that's what your user object is. So you have to either use
hashPassword or you extend the interface/typeSpectacled bearOP
so I should create a type using these properties? Will that work? I'm actually new to typescript. I'm learning it while working on this project
Does this look right?
@Spectacled bear Does this look right?
Looks fine to me
Spectacled bearOP
I'm also getting issue with password property on profile
Property "password" does not exist on type "Profile".
@Spectacled bear Property "password" does not exist on type "Profile".
Does it exist? According to the
types.ts of NextAuth, profile is either undefined orexport interface Profile {
sub?: string
name?: string
email?: string
image?: string
}Spectacled bearOP
Oh
Where can I find this types.ts file of NextAuth?
Then maybe password in some other parameter of signIn function...
@Spectacled bear Then maybe password in some other parameter of signIn function...
Try doing
console.log(account.credentials). It should be somewhere in there. Probably something like account.credentials.password.value? Just try aroundSpectacled bearOP
Ok
Checking
Spectacled bearOP
What is this AdapterUser type? It keep giving me error when I try to use it with my User type
user: User | AdapterUser
user: User | AdapterUser
documentation doesn't cover the types of these parameters at all...
Yeah, the docs are really not meant for TS. NextAuth also had/still has a couple issues with TS that you have to work around manually.
Here's the definition of
Here's the definition of
AdapterUser:export interface AdapterUser extends User {
id: string
email: string
emailVerified: Date | null
}Spectacled bearOP
This is how I'm trying to assign the custom type to user, what can I do to avoid this AdapterUser type error?
callbacks: {
async signIn(props) {
const {
user,
account,
profile,
}: { user: User; account: Account; profile: Profile } = props;I imported the types Account and Profile from next-auth
but it's giving this error
Type '{ user: User | AdapterUser; account: Account | null; profile?: Profile | undefined; email?: { verificationRequest?: boolean | undefined; } | undefined; credentials?: Record<...> | undefined; }' is not assignable to type '{ user: User; account: Account; profile: Profile; }'.
Types of property 'user' are incompatible.
Type 'User | AdapterUser' is not assignable to type 'User'.
Type 'User' is missing the following properties from type 'User': username, hashedPassword, googleId, discordId, createdAt@Spectacled bear Does this look right?
Spectacled bearOP
my User type is this that I created
Don't up there.
Just do something like this:
Just do something like this:
(user as User).hashedPassword = await hashPassword(account.credentials.password.value);Spectacled bearOP
ok let me try that
@ncls. Don't up there.
Just do something like this:
TS
(user as User).hashedPassword = await hashPassword(account.credentials.password.value);
Spectacled bearOP
On this I'm getting the error:
Also, there is an error on account.credentials.password.value as well
Conversion of type 'User | AdapterUser' to type 'User' may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert the expression to 'unknown' first.
Type 'AdapterUser' is missing the following properties from type 'User': username, hashedPassword, googleId, discordId, createdAtAlso, there is an error on account.credentials.password.value as well
'account.credentials' is of type 'unknown'.Ok, maybe all of this isn't even necessary. What exactly do you want to do even in the
signIn callback?Spectacled bearOP
I am hashing the password of the user
callbacks: {
async signIn({ user, account, profile }) {
if (account?.provider === "credentials") {
// For local sign-in, hash the provided password
(user as User).hashedPassword = await hashPassword(
account.credentials.password.value
);
// Remove plain password from user profile
delete profile!.password;
}
await prisma.user.update({
where: {
id: user.id,
},
data: {
...profile,
},
});
return true;
},
},But why?
Spectacled bearOP
so I shouldn't hash it?
I at least don't know why you would hash it on sign in
Spectacled bearOP
yea I'm confused now
Usually you hash the password when the user registers, then store it in the database and only get it to compare when the user tries to sign in
Spectacled bearOP
hmm but nextauth only have signIn and signOut functions, right? What should I do for Sign Up?
So basically a custom API endpoint
Spectacled bearOP
ok got it
Thanks