Storing Account Usename With Next Auth

I'm looking to store the username of an account when they sign in i.e
model Account {
...
provider String
providerAccountId String
providerAccountName String <--- New field
model Account {
...
provider String
providerAccountId String
providerAccountName String <--- New field
I see it's possible to extend the Prisma adapter and you can create your own linkAccount but that doesn't expose the name. Is it possible to add fields to the account adapter and extend that? Thanks
11 Replies
Rhys
Rhys2y ago
It seems the account interface isn't extendable so what may be better is making that field optional, having a custom callback when they sign in with that account and to get that information
rocawear
rocawear2y ago
You already have it on User model
Rhys
Rhys2y ago
What if you wanted to say display the name of both their Google account and Discord account and those are two different names? In my case, I'm working on rewriting a Discord bot of mine and it's possible for me to store information about Discord users (i.e name, avatar) before they've signed in on my website I'm thinking of making it so the account table works without having a user created for it so when my bot first creates a Discord user who hasn't signed in, it just stores that to the account table It might just be work making a separate Discord users table to prevent breaking things with next auth Relevant code is here: https://github.com/AnswerOverflow/AnswerOverflow/blob/API/packages/api/src/router/accounts/discord-accounts.ts and here: https://github.com/AnswerOverflow/AnswerOverflow/blob/API/packages/auth/src/auth-options.ts Here we go, I found an implementation I like
async linkAccount(account) {
if (account.provider !== "discord") {
throw Error("Unknown account provider");
}
if (!account.access_token) {
throw Error("No access token");
}
const user = await getDiscordUser(account.access_token);
await prisma.discordAccount.upsert({
where: {
id: user.id,
},
update: {
name: user.username,
avatar: user.avatar,
},
create: {
id: user.id,
name: user.username,
avatar: user.avatar,
},
});
return PrismaAdapter(prisma).linkAccount(account) as unknown as AdapterAccount;
},
async linkAccount(account) {
if (account.provider !== "discord") {
throw Error("Unknown account provider");
}
if (!account.access_token) {
throw Error("No access token");
}
const user = await getDiscordUser(account.access_token);
await prisma.discordAccount.upsert({
where: {
id: user.id,
},
update: {
name: user.username,
avatar: user.avatar,
},
create: {
id: user.id,
name: user.username,
avatar: user.avatar,
},
});
return PrismaAdapter(prisma).linkAccount(account) as unknown as AdapterAccount;
},
model Account {
...
providerAccountId String @unique
discordAccount DiscordAccount? @relation(fields: [providerAccountId], references: [id], onDelete: Cascade)

@@unique([provider, providerAccountId])
}

model DiscordAccount {
id String @id @unique
name String
avatar String?
Account Account?
}
model Account {
...
providerAccountId String @unique
discordAccount DiscordAccount? @relation(fields: [providerAccountId], references: [id], onDelete: Cascade)

@@unique([provider, providerAccountId])
}

model DiscordAccount {
id String @id @unique
name String
avatar String?
Account Account?
}
So when you link your account now, it fetches your Discord information and creates/updates it as its own table in the database, then creats the account with the provided account id action as the foreign key to the discord account Only issue with this is it requires IDs to be unique, this would need to be revisited if i wanted to support multiple auth provides but that's not needed
rocawear
rocawear2y ago
Not sure if you check if that account already exist before creating a new one, would be good
Rhys
Rhys2y ago
It’s doing an upset with Prisma so it’ll create / update it in the same SQL call
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Rhys
Rhys2y ago
Ah yeah that’s unfinished Possibly, but then let’s say I want to store something like their discord discriminator then that becomes an unused field
Perfect
Perfect2y ago
Can u not add it to the User model? Thats what I do edit - not exactly following ur idea with accounts existing before creation, nvm nvm
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Rhys
Rhys2y ago
Yeah it’s possible for some of them to be undefined, in my mind this way it’s more modular where let’s say you wanted to strip out discord support - all you have to do is delete the discord table and the thing that syncs it and you’re good The main thing is I need to store a lot of users who may not have an account and need that to be as simple as possible - along with that I need their discord ID and name etc My project is for displaying discord help channel messages on the web so it’s possible to index a users content and have their name / profile picture before they’ve signed into the website - so it needs to support displaying their information that way
Want results from more Discord servers?
Add your server