[GH-ISSUE #1025] error: unable_to_create_user #8555

Closed
opened 2026-04-13 03:40:18 -05:00 by GiteaMirror · 4 comments
Owner

Originally created by @eliabexp on GitHub (Dec 26, 2024).
Original GitHub issue: https://github.com/better-auth/better-auth/issues/1025

Is this suited for github?

  • Yes, this is suited for github

To Reproduce

  1. Create a oauth sign in with any provider
  2. Sign in with an account that doesn't have a user yet

Current vs. Expected behavior

When i try to sign up with oauth, the redirect goes correctly but the user is not created.

The following error appears in logs:

 GET /login 200 in 426ms
 POST /api/auth/sign-in/social?currentURL=http%3A%2F%2Flocalhost%3A3000%2Flogin 200 in 192ms
 ERROR [Better Auth]: unable_to_create_user
 GET /api/auth/callback/google?state=98ZijIGJFRkH7ofpzhOUKYc6ZxmooVEs&code=4%2F0AanRRrsc__TeK-WWp24G9qIo4ZFAxDuIX0FRCiJ-vE5whaCGGxRIfMkeEv9jF9Nx_dSvYUg&scope=email+profile+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.email+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.profile+openid&authuser=0&prompt=none 302 in 553ms
 GET /login?error=unable_to_create_user 200 in 269ms

What version of Better Auth are you using?

1.1.3

Provide environment information

- Framework: Next.js
- Database adapter: Drizzle
- Database: postgres

Which area(s) are affected? (Select all that apply)

Backend

Auth config (if applicable)

import { betterAuth } from 'better-auth';
import { drizzleAdapter } from 'better-auth/adapters/drizzle';
import { db } from '@/db';
import { accounts, authSessions as sessions, users, verifications } from '@/db/schema';

export const auth = betterAuth({
    database: drizzleAdapter(db, {
        provider: 'pg',
        schema: { accounts, sessions, users, verifications },
        usePlural: true
    }),
    socialProviders: {
        google: {
            clientId: process.env.GOOGLE_CLIENT_ID!,
            clientSecret: process.env.GOOGLE_CLIENT_SECRET!
        }
    }
});

Additional context

I tested with google and github providers with different accounts in different databases, different devices and different environments, the same behavior happened to all of them.

Originally created by @eliabexp on GitHub (Dec 26, 2024). Original GitHub issue: https://github.com/better-auth/better-auth/issues/1025 ### Is this suited for github? - [X] Yes, this is suited for github ### To Reproduce 1. Create a oauth sign in with any provider 2. Sign in with an account that doesn't have a user yet ### Current vs. Expected behavior When i try to sign up with oauth, the redirect goes correctly but the user is not created. The following error appears in logs: ``` GET /login 200 in 426ms POST /api/auth/sign-in/social?currentURL=http%3A%2F%2Flocalhost%3A3000%2Flogin 200 in 192ms ERROR [Better Auth]: unable_to_create_user GET /api/auth/callback/google?state=98ZijIGJFRkH7ofpzhOUKYc6ZxmooVEs&code=4%2F0AanRRrsc__TeK-WWp24G9qIo4ZFAxDuIX0FRCiJ-vE5whaCGGxRIfMkeEv9jF9Nx_dSvYUg&scope=email+profile+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.email+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.profile+openid&authuser=0&prompt=none 302 in 553ms GET /login?error=unable_to_create_user 200 in 269ms ``` ### What version of Better Auth are you using? 1.1.3 ### Provide environment information ```bash - Framework: Next.js - Database adapter: Drizzle - Database: postgres ``` ### Which area(s) are affected? (Select all that apply) Backend ### Auth config (if applicable) ```typescript import { betterAuth } from 'better-auth'; import { drizzleAdapter } from 'better-auth/adapters/drizzle'; import { db } from '@/db'; import { accounts, authSessions as sessions, users, verifications } from '@/db/schema'; export const auth = betterAuth({ database: drizzleAdapter(db, { provider: 'pg', schema: { accounts, sessions, users, verifications }, usePlural: true }), socialProviders: { google: { clientId: process.env.GOOGLE_CLIENT_ID!, clientSecret: process.env.GOOGLE_CLIENT_SECRET! } } }); ``` ### Additional context I tested with google and github providers with different accounts in different databases, different devices and different environments, the same behavior happened to all of them.
GiteaMirror added the lockedbug labels 2026-04-13 03:40:19 -05:00
Author
Owner

@Bekacru commented on GitHub (Dec 26, 2024):

unable_to_create_user is returned when it fails to to create a user in the database. check if you have correct schema expected by better auth and you don't have any extra field that's required.

<!-- gh-comment-id:2563009994 --> @Bekacru commented on GitHub (Dec 26, 2024): unable_to_create_user is returned when it fails to to create a user in the database. check if you have correct schema expected by better auth and you don't have any extra field that's required.
Author
Owner

@eliabexp commented on GitHub (Dec 26, 2024):

I also thought that was the problem, but I've checked the schema and it appears all right, I also tried to remove the 'not nulls' to check if some field wasn't being generated

Drizzle doesn't log any errors btw

These are the schemas:

export const authSessions = pgTable('sessions', {
    id: text('id').primaryKey(),
    expiresAt: timestamp('expires_at'),
    token: text('token').unique(),
    createdAt: timestamp('created_at').defaultNow(),
    updatedAt: timestamp('updated_at'),
    ipAddress: text('ip_address'),
    userAgent: text('user_agent'),
    userId: text('user_id')
        .notNull()
        .references(() => users.id)
});

export const accounts = pgTable('accounts', {
    id: text('id').primaryKey(),
    accountId: text('account_id'),
    providerId: text('provider_id'),
    userId: text('user_id')
        .notNull()
        .references(() => users.id),
    accessToken: text('access_token'),
    refreshToken: text('refresh_token'),
    idToken: text('id_token'),
    accessTokenExpiresAt: timestamp('access_token_expires_at'),
    refreshTokenExpiresAt: timestamp('refresh_token_expires_at'),
    scope: text('scope'),
    password: text('password'),
    createdAt: timestamp('created_at').defaultNow(),
    updatedAt: timestamp('updated_at')
});

export const users = pgTable('users', {
    id: text('id').primaryKey(),
    name: text('name'),
    email: text('email').unique(),
    emailVerified: boolean('email_verified').default(false),
    createdAt: timestamp('created_at').defaultNow(),
    updatedAt: timestamp('updated_at'),
    isAnonymous: boolean('is_anonymous').default(false),
});

export const verifications = pgTable('verifications', {
    id: text('id').primaryKey(),
    identifier: text('identifier').notNull(),
    value: text('value').notNull(),
    expiresAt: timestamp('expires_at').notNull(),
    createdAt: timestamp('created_at').defaultNow(),
    updatedAt: timestamp('updated_at')
});

p.s. the bug just happens with oauth sign up, when the user is anonymous it is created succefully

<!-- gh-comment-id:2563036629 --> @eliabexp commented on GitHub (Dec 26, 2024): I also thought that was the problem, but I've checked the schema and it appears all right, I also tried to remove the 'not nulls' to check if some field wasn't being generated Drizzle doesn't log any errors btw These are the schemas: ```js export const authSessions = pgTable('sessions', { id: text('id').primaryKey(), expiresAt: timestamp('expires_at'), token: text('token').unique(), createdAt: timestamp('created_at').defaultNow(), updatedAt: timestamp('updated_at'), ipAddress: text('ip_address'), userAgent: text('user_agent'), userId: text('user_id') .notNull() .references(() => users.id) }); export const accounts = pgTable('accounts', { id: text('id').primaryKey(), accountId: text('account_id'), providerId: text('provider_id'), userId: text('user_id') .notNull() .references(() => users.id), accessToken: text('access_token'), refreshToken: text('refresh_token'), idToken: text('id_token'), accessTokenExpiresAt: timestamp('access_token_expires_at'), refreshTokenExpiresAt: timestamp('refresh_token_expires_at'), scope: text('scope'), password: text('password'), createdAt: timestamp('created_at').defaultNow(), updatedAt: timestamp('updated_at') }); export const users = pgTable('users', { id: text('id').primaryKey(), name: text('name'), email: text('email').unique(), emailVerified: boolean('email_verified').default(false), createdAt: timestamp('created_at').defaultNow(), updatedAt: timestamp('updated_at'), isAnonymous: boolean('is_anonymous').default(false), }); export const verifications = pgTable('verifications', { id: text('id').primaryKey(), identifier: text('identifier').notNull(), value: text('value').notNull(), expiresAt: timestamp('expires_at').notNull(), createdAt: timestamp('created_at').defaultNow(), updatedAt: timestamp('updated_at') }); ``` p.s. the bug just happens with oauth sign up, when the user is anonymous it is created succefully
Author
Owner

@eliabexp commented on GitHub (Dec 26, 2024):

image

database table

<!-- gh-comment-id:2563040437 --> @eliabexp commented on GitHub (Dec 26, 2024): ![image](https://github.com/user-attachments/assets/0a66a134-fe04-4aaf-928c-d393bac6d6ea) database table
Author
Owner

@eliabexp commented on GitHub (Dec 26, 2024):

update: I found the bug, if the image field isn't in user's schema, the user is not created when using oauth

sorry about the issue 😅

maybe we could have an option to exclude the optional fields

<!-- gh-comment-id:2563047845 --> @eliabexp commented on GitHub (Dec 26, 2024): update: I found the bug, if the image field isn't in user's schema, the user is not created when using oauth sorry about the issue 😅 maybe we could have an option to exclude the optional fields
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#8555