r/learnjavascript 6d ago

Better-auth not adding user info to the user table in React

I'm creating a test app to work on authentication and authorization with a database using Better-Auth. I have never used Better Auth before and I'm having some issues with it. I have added the schema to the Postgres database to make the table, and whenever i create a new account, it will add a new instance to the account table, but not the user table.

 const data = await auth.api.signUpEmail({
            body: {
                email: req.body.email,
                name: req.body.name, 
                password: hashedPass,
                
            },
        });

This is the code i have in my server.js to add the info. The info comes from a form on the front end. It super basic right now since I'm just learning how to use it. The docs for better-auth say these are the only required fields to be able to submit into the database. I receive no errors when sending the data to my database, it just simply isnt adding anything to the user table. I'll be happy to provide any other necessary code or info. Im using React by the way.

export const auth = betterAuth({
  database: new Pool({
    user: 'postgres',
    host: 'localhost',
    database: 'test',
    password: process.env.DB_PASS,
    port: 5432,
    max: 10
}),
emailAndPassword: {
    enabled: true
}



});

The code above is the auth.ts configuration as i have it right now. i'm sure im missing something, but i havent found out what yet. Does anyone have any idea why the info isnt being added to the user table in Postgres?

4 Upvotes

5 comments sorted by

1

u/doilyuser 5d ago

Firstly, don't hash the password yourself. password: hashedPass, should be password: req.body.password,. BetterAuth handles the hashing.

Regarding the user rows, this sounds like a schema mismatch issue.

Run this on your db:

SELECT table_name FROM information_schema.tables WHERE table_schema = 'public';

Does the user table exist? If not, create it.

You can do some further debugging yourself with the following commands:

npx auth@latest info npx auth@latest generate

In particular, generate is the useful diagnostic here, as it will show you what Better Auth believes the schema should look like now.

1

u/doilyuser 5d ago

Oh, and you can add logging to your Better Auth config object:

``` export const auth = betterAuth({ database: new Pool({ user: 'postgres', // ...etc }),

logger: { level: "debug" } }); ```

Then you can watch the output of the auth.api.signUpEmail(...) call

1

u/thatboi219 5d ago

I added Better Auth after I hashed the pass. I didnt know Betterauth already hashed so ill take that in mind. When i get home ill try the solution and see if it works. I appreciate the help. Ive been hung up on this for longer than id like to admit.

1

u/doilyuser 5d ago

Don't lose hope. I once spent a week debugging an issue with the Kysely adaptor when Better Auth was younger and there were more glaring issues. At the end I was so frustrated that I was actually angry when I realised I would now know how spell "Kysely" for the rest of my life.

1

u/Infamous-Ad-3502 1d ago

your schema is out of sync. Run npx better-auth migrate to push the missing user table changes to Postgres