NextAuth + DiscordProvider

I feel like I might be going about my setup wrong, but just wanted to check in. I basically want a bunch of users that are purely based on Discord accounts, and then to use their data for my Discord dashboard. Here is my prisma user schema (rest are default)
model User {
discordId String @id @unique
name String?
email String? @unique
emailVerified DateTime?
image String?
accounts Account[]
sessions Session[]
posts Post[]
}
model User {
discordId String @id @unique
name String?
email String? @unique
emailVerified DateTime?
image String?
accounts Account[]
sessions Session[]
posts Post[]
}
The error I am getting is
Argument `discordId` is missing.
[next-auth][error][adapter_error_createUser]
https://next-auth.js.org/errors#adapter_error_createuser
Invalid `prisma.user.create()` invocation:

{
data: {
email: undefined,
emailVerified: null,
+ discordId: String
}
}
Argument `discordId` is missing.
[next-auth][error][adapter_error_createUser]
https://next-auth.js.org/errors#adapter_error_createuser
Invalid `prisma.user.create()` invocation:

{
data: {
email: undefined,
emailVerified: null,
+ discordId: String
}
}
But can I disable this somehow?? I am overriding the signIn function so I presumed that would do it:
async signIn({ profile }) {
if (!profile) return false;
const DisP = profile as DiscordProfile;

const user = await Prisma.user.findUnique({
where: { discordId: DisP.id },
});

if (!user) {
console.log("CREATING USER", DisP.id);

await Prisma.user.create({
data: {
discordId: DisP.id,
email: DisP.email,
// Include other properties as needed
},
});
}

return true;
}
}
async signIn({ profile }) {
if (!profile) return false;
const DisP = profile as DiscordProfile;

const user = await Prisma.user.findUnique({
where: { discordId: DisP.id },
});

if (!user) {
console.log("CREATING USER", DisP.id);

await Prisma.user.create({
data: {
discordId: DisP.id,
email: DisP.email,
// Include other properties as needed
},
});
}

return true;
}
}
Any ideas? This setup works fine as the database has the user ID in it fine, but its just that this is now giving me that above error. Plus, is this the best way to handle this? Seems a bit convuluted. Thanks!
13 Replies
Rhys
Rhys15mo ago
You’re not actually overriding sign in sign in is just a callback function that runs when the sign in function is called, instead you need to override it on the adapter
Rhys
Rhys15mo ago
Create an adapter | NextAuth.js
Using a custom adapter you can connect to any database back-end or even several different databases. Official adapters created and maintained by our community can be found in the adapters packages. Feel free to add a custom adapter from your project to the repository, or even become a maintainer of a certain adapter. Custom adapters can still be...
Sam
SamOP15mo ago
Ahh I see That makes sense because I knew the invocation had to come from somewhere that wasn’t me 😂
Rhys
Rhys15mo ago
GitHub
AnswerOverflow/packages/auth/src/adapter.ts at main · AnswerOverflo...
Indexing Discord Help Channel Questions into Google - AnswerOverflow/AnswerOverflow
Sam
SamOP15mo ago
What’s better practise though for only a single OAuth account? Do people normally modify the User table or would they just find the Account entry every session callback? Thanks for this! This seems like the best way to do it by the looks of it for full control
Rhys
Rhys15mo ago
I kept the normal NextAuth model but added on a Discord account reference to the account table I think, it’s been a little while Yeah all the adapters are open source so it’s pretty easy to just copy paste and modify them
Sam
SamOP15mo ago
I’ll take a look through your implementation and see how it works compared to my use case Cool bot by the way! I like the concept of the website too
Rhys
Rhys15mo ago
You can also only override a portion of it by doing: { …oldAdapter new function:() => {} } Thanks!
Sam
SamOP15mo ago
Ahhh true
Rhys
Rhys15mo ago
Say hi to the website visitors 👋
Sam
SamOP15mo ago
🫡 Nice that it’s open source too 🙂 I’ll give that a test out then and see where I get with it Thanks for those resources! Actually one (small) other question, all the data inputs you’re taking in the adapter, where are they defined? Is that in one of the callbacks of the Provider or?
Rhys
Rhys15mo ago
They’re defined in the Adapter type from next auth
Sam
SamOP15mo ago
Ah so they’re auto-typed Nice

Did you find this page helpful?