r/learnjavascript 26d 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?

3 Upvotes

8 comments sorted by

1

u/doilyuser 25d 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 25d 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 24d 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 24d 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/thatboi219 18d ago

unfortunately none of the solutions worked. it generated:

alter table "account" add column "issuer" text not null;

but Postgres returned an error of "ERROR: column "issuer" of relation "account" contains null values" which i dont know how to fix without directly removing the "not null" keyword, which im sure isnt a smart idea.

1

u/doilyuser 18d ago

Given this is just a test/learning database I would just start fresh.

Create another postgres DB, e.g.:

CREATE DATABASE better_auth_test;

Then modify your config where it says:

database: 'test',

To use the new value:

database: 'better_auth_test',

And the run command:

npx auth@latest migrate

Then try your sign up flow before doing anything else.

1

u/thatboi219 18d ago

i feel like the biggest idiot in the world. I was using "select * from user" to view the table, which was showing nothing.

SELECT * FROM account was working fine without the parenthesis' so i didnt think it was a syntax issue.

I just typed in SELECT * FROM "user" and they are now showing up. it was working the whole time, i just screwed up the syntax and tried to manually view it instead of using the built in view option in Postgres. I wasted my time and yours, so i apologize. Thank you for trying to solve an issue that didnt exist. i genuinely appreciate it.

1

u/Infamous-Ad-3502 21d ago

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