which type should I set for nextauth's credentials parameter in authorize callback?
Unanswered
Tomistoma posted this in #help-forum
TomistomaOP
I'm looking at setting up a authorize function for the CredentialsProvider, but my eslint is complaining that there should not be an explicit "any" type.
I'm looking at the credentials parameter inside of authorize and don't know which type to give it.
https://next-auth.js.org/getting-started/typescript
Doesn't yield much information for me either.
I'm looking at the credentials parameter inside of authorize and don't know which type to give it.
https://next-auth.js.org/getting-started/typescript
Doesn't yield much information for me either.
1 Reply
TomistomaOP
I fixed my issue by looking at the type definitions inside next auth...
My example code now looks like this (very weird type definitions):
By overwriting the record type within the function I'm able to actually get the email field from the credentials that are returned... I'm confused as to why this isn't included in the type definitions for next auth itself, as the returned credentials usually end up being:
email:
password:
csrfToken:
callbackUrl:
json:
My example code now looks like this (very weird type definitions):
async authorize(credentials: Record<"username" | "password", string> | undefined) {
let email;
let username;
if (credentials && "username" in credentials) {
username = credentials.username;
} else if (credentials && "email" in credentials) {
email = (credentials as Record<"email" | "password", string>).email;
}
const password = credentials?.password;
await mongoose.connect(process.env.MONGO_URL!);
let user;
if (email) {
user = await User.findOne({email});
} else if (username) {
user = await User.findOne({username});
}
const passwordOk = user && bcrypt.compareSync(password!, user.password);
if (passwordOk) {
return user;
}
return null;
}By overwriting the record type within the function I'm able to actually get the email field from the credentials that are returned... I'm confused as to why this isn't included in the type definitions for next auth itself, as the returned credentials usually end up being:
email:
password:
csrfToken:
callbackUrl:
json: