Use existing User Database

Hey, I already have an existing Discord user database from my Discord. This database will continue to be populated and updated by my Discord bot backend. Now I want to create a Next.js app for the backend. I want to use BetterAuth there, but it should utilize the existing Discord user database. There’s no need for duplicate entries in the database. The app will only support Discord login. Are there ways for BetterAuth to work with this setup, or do I need to adjust my database schema?
18 Replies
FalconiZzare
FalconiZzare2w ago
I think you need to migrate your schema according to better auth schema as better auth needs those specific tables to work with.
DevPanda
DevPandaOP2w ago
Okay, I’ve already tried that. I adjusted the User table from BetterAuth and added my data to it. However, when I try to log in, it attempts to create the user in the database, which doesn’t work because the Discord ID already exists. I think this might have something to do with the Account table.
FalconiZzare
FalconiZzare2w ago
u need the user, account and session table.
DevPanda
DevPandaOP2w ago
I have added all the tables that BetterAuth requires. I simply merged my current DiscordUser table with the User table from BetterAuth. However, it always tries to create the user during login, even though they already exist. I think this is because there’s no corresponding account in the Account table yet. If there’s no other way, I’ll have to link the User table with the Discord table and then manually add the values to the session or load them into a store/context.
FalconiZzare
FalconiZzare2w ago
That sounds expected as idToken and accessTokens are stored on account table, not usertable. If your previous user table contains all of these together (email, image, idToken, accessToken,refreshToken) then you have to manually separate them and insert into new user and acocunt table. userTabale and accountTable has one to many relationship.
DevPanda
DevPandaOP2w ago
here my Prisma Schema Models for better-auth https://pastie.io/vzseuq.dart
FalconiZzare
FalconiZzare2w ago
looks ok, how are populating these fields?
DevPanda
DevPandaOP2w ago
also for the user I can fill the mandatory fields without problems with the mapProfileToUser function I can customize everything well. the problem is simply he always wants to create it directly. I don't even see in the debug log of Prisma that it checks if the user is there
FalconiZzare
FalconiZzare2w ago
What data of a user do you currently have? can u show me a dummy row of ur existing data?
DevPanda
DevPandaOP2w ago
{ "id": "w9pw4ydz1v0cgjtiglwxrn80", "discordId": "283414457193988096", "username": "devpanda_", "logoUrl": "", "currentOrganizationId": null, "guildIds": [], "createdAt": "2025-05-01 21:46:07.241", "updatedAt": "2025-05-01 21:47:44.179", "email": null, "emailVerified": false, "image": null }
FalconiZzare
FalconiZzare2w ago
Oh. No this is not a proper OAuth authenticated data. If you must use better auth, your users must login/signup again. However, the discordId will remain the same for users even if they sign in again, so any data tied to this Id should still be usable. make sure to map "discordId" to "accountId" in accounts table
DevPanda
DevPandaOP2w ago
What do you mean exactly?
FalconiZzare
FalconiZzare2w ago
this one = accountId in better auth schema. so it should be removed from user table and be added to accounttable
No description
FalconiZzare
FalconiZzare2w ago
So you have to manually populate the accounts table with your existing discord user ids, keep the idToken, accessToken and refreshToken as NULL and when the user signs in it will be aquired automcatically. Make sure to register in discord developer console first
No description
FalconiZzare
FalconiZzare2w ago
No description
DevPanda
DevPandaOP2w ago
I’ll just do it in two tables. It’s simpler.
FalconiZzare
FalconiZzare2w ago
Whatever you do, you have to adapt to better auth schema if you want to use better auth.

Did you find this page helpful?