[GH-ISSUE #289] Credential account not found (MongoDB) #16836

Closed
opened 2026-04-15 14:49:48 -05:00 by GiteaMirror · 1 comment
Owner

Originally created by @khantseithu on GitHub (Oct 22, 2024).
Original GitHub issue: https://github.com/better-auth/better-auth/issues/289

Describe the bug
Credential account not found on sign in when using custom mongodb models, signUp is working,

To Reproduce
Steps to reproduce the behavior:

// auth.ts
import { betterAuth } from 'better-auth';
import { mongodbAdapter } from 'better-auth/adapters/mongodb';
import { type Db } from 'mongodb';
import mongoose from 'mongoose';
import connectDB from './db';

connectDB();

export const auth = betterAuth({
	database: mongodbAdapter(mongoose.connection.db as Db, { generateId: false }),
	baseURL: 'http://localhost:5173/api/auth',
	user: {
		modelName: 'groups',
		fields: {
			id: '_id',
			email: 'email',
			password: 'password',
			createdAt: 'created_at',
			updatedAt: 'updated_at',
			name: 'name',
			emailVerified: 'email_verified'
		},
		additionalFields: {
			group_id: {
				type: 'number'
			}
		}
	},
	account: {
		modelName: 'accounts',
		fields: {
			id: '_id',
			userId: 'user_id',
			accountId: 'account_id',
			providerId: 'provider_id',
			accessToken: 'access_token',
			refreshToken: 'refresh_token',
			expiresAt: 'expires_at',
			password: 'password'
		}
	},
	session: {
		modelName: 'sessions',
		fields: {
			userId: 'user_id',
			expiresAt: 'expires_at',
			ipAddress: 'ip_address',
			userAgent: 'user_agent'
		}
	},
	verification: {
		modelName: 'verifications',
		fields: {
			identifier: 'identifier',
			value: 'value',
			expiresAt: 'expires_at'
		}
	},
	emailAndPassword: {
		enabled: true,
		autoSignIn: true,
		requireEmailVerification: false
	}
});

Records in mongodb

groups collection:
{
  "_id": {
    "$oid": "67173ce954f0e26fb6c196d6"
  },
  "id": null,
  "name": "John smith",
  "email": "john@email.com",
  "email_verified": false,
  "image": null,
  "created_at": {
    "$date": "2024-10-22T05:49:29.088Z"
  },
  "updated_at": {
    "$date": "2024-10-22T05:49:29.088Z"
  },
  "group_id": 114
}

accounts:
{
  "_id": {
    "$oid": "67173ce954f0e26fb6c196d7"
  },
  "id": null,
  "account_id": {
    "$oid": "67173ce954f0e26fb6c196d6"
  },
  "provider_id": "credential",
  "user_id": {
    "$oid": "67173ce954f0e26fb6c196d6"
  },
  "access_token": null,
  "refresh_token": null,
  "idToken": null,
  "expires_at": {
    "$date": "2024-11-21T05:49:29.456Z"
  },
  "password": <hashps>
}

Expected behavior
Should be able to signin.

Screenshots
Screen Shot 2024-10-22 at 12 25 06 PM

Additional context
SvelteKit: "^2.0.0"
"better-auth": "^0.5.3-beta.7",

Originally created by @khantseithu on GitHub (Oct 22, 2024). Original GitHub issue: https://github.com/better-auth/better-auth/issues/289 **Describe the bug** Credential account not found on sign in when using custom mongodb models, signUp is working, **To Reproduce** Steps to reproduce the behavior: ```ts // auth.ts import { betterAuth } from 'better-auth'; import { mongodbAdapter } from 'better-auth/adapters/mongodb'; import { type Db } from 'mongodb'; import mongoose from 'mongoose'; import connectDB from './db'; connectDB(); export const auth = betterAuth({ database: mongodbAdapter(mongoose.connection.db as Db, { generateId: false }), baseURL: 'http://localhost:5173/api/auth', user: { modelName: 'groups', fields: { id: '_id', email: 'email', password: 'password', createdAt: 'created_at', updatedAt: 'updated_at', name: 'name', emailVerified: 'email_verified' }, additionalFields: { group_id: { type: 'number' } } }, account: { modelName: 'accounts', fields: { id: '_id', userId: 'user_id', accountId: 'account_id', providerId: 'provider_id', accessToken: 'access_token', refreshToken: 'refresh_token', expiresAt: 'expires_at', password: 'password' } }, session: { modelName: 'sessions', fields: { userId: 'user_id', expiresAt: 'expires_at', ipAddress: 'ip_address', userAgent: 'user_agent' } }, verification: { modelName: 'verifications', fields: { identifier: 'identifier', value: 'value', expiresAt: 'expires_at' } }, emailAndPassword: { enabled: true, autoSignIn: true, requireEmailVerification: false } }); ``` Records in mongodb ```js groups collection: { "_id": { "$oid": "67173ce954f0e26fb6c196d6" }, "id": null, "name": "John smith", "email": "john@email.com", "email_verified": false, "image": null, "created_at": { "$date": "2024-10-22T05:49:29.088Z" }, "updated_at": { "$date": "2024-10-22T05:49:29.088Z" }, "group_id": 114 } accounts: { "_id": { "$oid": "67173ce954f0e26fb6c196d7" }, "id": null, "account_id": { "$oid": "67173ce954f0e26fb6c196d6" }, "provider_id": "credential", "user_id": { "$oid": "67173ce954f0e26fb6c196d6" }, "access_token": null, "refresh_token": null, "idToken": null, "expires_at": { "$date": "2024-11-21T05:49:29.456Z" }, "password": <hashps> } ``` **Expected behavior** Should be able to signin. **Screenshots** ![Screen Shot 2024-10-22 at 12 25 06 PM](https://github.com/user-attachments/assets/91c8d3a2-0fd3-4e14-8bdc-db6a8f6ab2b5) **Additional context** SvelteKit: "^2.0.0" "better-auth": "^0.5.3-beta.7",
GiteaMirror added the locked label 2026-04-15 14:49:48 -05:00
Author
Owner

@Iulian-Dragomirescu commented on GitHub (Nov 4, 2024):

the bug was fixed in v0.7.2-beta.5 release

<!-- gh-comment-id:2455499315 --> @Iulian-Dragomirescu commented on GitHub (Nov 4, 2024): the bug was fixed in [v0.7.2-beta.5](https://github.com/better-auth/better-auth/releases/tag/v0.7.2-beta.5) release
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#16836