Getting INVALID_USERNAME_OR_PASSWORD Error when using correct credentials

I am new to Better Auth and am using the username() plugin to enable username/password logins in my ElysiaJS backend. I have the following configurations for both the server and client for Better Auth:
import { betterAuth } from "better-auth";
import { drizzleAdapter } from "better-auth/adapters/drizzle";
import { username } from "better-auth/plugins";

import { db } from "../drizzle/db";
import {
SessionTable,
UserAccountTable,
UserTable,
VerificationTable,
} from "../drizzle/schemas/auth";

export const auth = betterAuth({
database: drizzleAdapter(db, {
provider: "pg",
schema: {
user: UserTable,
account: UserAccountTable,
session: SessionTable,
verification: VerificationTable,
},
}),
plugins: [username()],
trustedOrigins: ["http://localhost:3000"],
session: {
cookieCache: {
enabled: true,
maxAge: 5 * 60, // Cache duration in seconds
},
},
});
import { betterAuth } from "better-auth";
import { drizzleAdapter } from "better-auth/adapters/drizzle";
import { username } from "better-auth/plugins";

import { db } from "../drizzle/db";
import {
SessionTable,
UserAccountTable,
UserTable,
VerificationTable,
} from "../drizzle/schemas/auth";

export const auth = betterAuth({
database: drizzleAdapter(db, {
provider: "pg",
schema: {
user: UserTable,
account: UserAccountTable,
session: SessionTable,
verification: VerificationTable,
},
}),
plugins: [username()],
trustedOrigins: ["http://localhost:3000"],
session: {
cookieCache: {
enabled: true,
maxAge: 5 * 60, // Cache duration in seconds
},
},
});
import { usernameClient } from "better-auth/client/plugins";
import { createAuthClient } from "better-auth/solid";

export const authClient = createAuthClient({
baseURL: "http://localhost:3000",
plugins: [usernameClient()],
});
import { usernameClient } from "better-auth/client/plugins";
import { createAuthClient } from "better-auth/solid";

export const authClient = createAuthClient({
baseURL: "http://localhost:3000",
plugins: [usernameClient()],
});
I made some changes to the default Drizzle tables that Better Auth generated in my project:
import { sql } from "drizzle-orm";
import { boolean, check, decimal, pgSchema, text, timestamp, uuid } from "drizzle-orm/pg-core";

export const authSchema = pgSchema("auth");
export const UserTable = authSchema.table("user", {...});
export const UserAccountTable = authSchema.table("user_account", {...});
export const SessionTable = authSchema.table("session", {...});
export const VerificationTable = authSchema.table("verification", {...});
import { sql } from "drizzle-orm";
import { boolean, check, decimal, pgSchema, text, timestamp, uuid } from "drizzle-orm/pg-core";

export const authSchema = pgSchema("auth");
export const UserTable = authSchema.table("user", {...});
export const UserAccountTable = authSchema.table("user_account", {...});
export const SessionTable = authSchema.table("session", {...});
export const VerificationTable = authSchema.table("verification", {...});
With the above said, when I am doing local development, I am spinning up a Postgres docker container and run a custom seed script that I have to populate the tables with fake data. I'm probably not doing something correctly there in that step because when I attempt to log in with the fake admin user I added to the UserTable, I keep getting INVALID_USERNAME_OR_PASSWORD back as a response. Here is a small snippet of what I am doing:
const passwordMap: Record<string, string> = {
admin: "administrator",
};

const users = [
{
name: "Super User",
username: "admin",
emailVerified: true,
},
];

for (const user of users) {
const inserted = await db
.insert(UserTable)
.values(user)
.returning({ userId: UserTable.id, username: UserTable.username });

const insertedUser = inserted[0] as { userId: string; username: string };

await db.insert(UserAccountTable).values({
userId: insertedUser?.userId,
password: passwordMap[insertedUser.username] ?? faker.internet.password(),
});
const passwordMap: Record<string, string> = {
admin: "administrator",
};

const users = [
{
name: "Super User",
username: "admin",
emailVerified: true,
},
];

for (const user of users) {
const inserted = await db
.insert(UserTable)
.values(user)
.returning({ userId: UserTable.id, username: UserTable.username });

const insertedUser = inserted[0] as { userId: string; username: string };

await db.insert(UserAccountTable).values({
userId: insertedUser?.userId,
password: passwordMap[insertedUser.username] ?? faker.internet.password(),
});
Has anybody else run into this problem when using the username login?
0 Replies
No replies yetBe the first to reply to this messageJoin

Did you find this page helpful?