Sign up with OAuth providers throws errors #101

Closed
opened 2026-03-13 07:33:23 -05:00 by GiteaMirror · 10 comments
Owner

Originally created by @PawelPotempa on GitHub (Oct 19, 2024).

Version: "better-auth": "^0.5.1",

Sign up with Google provider: error: null value in column "userId" of relation "account" violates not-null constraint
Creates an entry in the session and user table, no entry in account table.

Sign up with Facebook provider: error: null value in column "emailVerified" of relation "user" violates not-null constraint
Doesn't create any entries in the db.

I've also tried the newest @beta.

auth.ts

import { drizzleAdapter } from "better-auth/adapters/drizzle";
import db from "./db";
import { sendVerificationEmail, sendPasswordResetEmail } from "@/emails/resend";
import { User } from "better-auth/types";

export const auth = betterAuth({
    database: drizzleAdapter(db, {
        provider: "pg",
    }),
    emailAndPassword: {
        enabled: true,
        minPasswordLength: 8,
        async sendVerificationEmail(user: User, url: string) {
            await sendVerificationEmail(user, url);
        },
        sendEmailVerificationOnSignUp: true,
        sendResetPasswordEmail: true,
        async sendResetPassword(user: User, url: string) {
            await sendPasswordResetEmail(user, url);
        }
    },
    socialProviders: {
        google: {
            clientId: process.env.GOOGLE_CLIENT_ID || "",
            clientSecret: process.env.GOOGLE_CLIENT_SECRET || "",
            scope: ["openid", "profile", "email"],
        },
        facebook: {
            clientId: process.env.FACEBOOK_CLIENT_ID || "",
            clientSecret: process.env.FACEBOOK_CLIENT_SECRET || "",
            scope: ["email", "public_profile"],
        }
    },
    account: {
        accountLinking: {
            enabled: true,
            trustedProviders: ["google", "facebook"],
        }
    },
    rateLimit: {
        window: 60,
        max: 10,
    },
});

schema.ts


export const roleEnum = pgEnum('role', ['admin', 'user', 'retailer', 'partner']);

export const user = pgTable("user", {
	id: text("id").primaryKey(),
	name: text('name').notNull(),
	email: text('email').notNull().unique(),
	emailVerified: boolean('emailVerified').notNull(),
	image: text('image'),
	role: roleEnum('role').notNull().default('user'),
	createdAt: timestamp('createdAt').notNull(),
	updatedAt: timestamp('updatedAt').notNull()
});

export const session = pgTable("session", {
	id: text("id").primaryKey(),
	expiresAt: timestamp('expiresAt').notNull(),
	ipAddress: text('ipAddress'),
	userAgent: text('userAgent'),
	userId: text('userId').notNull().references(() => user.id)
});

export const account = pgTable("account", {
	id: text("id").primaryKey(),
	accountId: text('accountId').notNull(),
	providerId: text('providerId').notNull(),
	userId: text('userId').notNull().references(() => user.id),
	accessToken: text('accessToken'),
	refreshToken: text('refreshToken'),
	idToken: text('idToken'),
	expiresAt: timestamp('expiresAt'),
	password: text('password')
});

export const verification = pgTable("verification", {
	id: text("id").primaryKey(),
	identifier: text('identifier').notNull(),
	value: text('value').notNull(),
	expiresAt: timestamp('expiresAt').notNull()
});
Originally created by @PawelPotempa on GitHub (Oct 19, 2024). Version: "better-auth": "^0.5.1", Sign up with Google provider: `error: null value in column "userId" of relation "account" violates not-null constraint` Creates an entry in the session and user table, no entry in account table. Sign up with Facebook provider: `error: null value in column "emailVerified" of relation "user" violates not-null constraint` Doesn't create any entries in the db. I've also tried the newest @beta. **auth.ts** ```import { betterAuth } from "better-auth"; import { drizzleAdapter } from "better-auth/adapters/drizzle"; import db from "./db"; import { sendVerificationEmail, sendPasswordResetEmail } from "@/emails/resend"; import { User } from "better-auth/types"; export const auth = betterAuth({ database: drizzleAdapter(db, { provider: "pg", }), emailAndPassword: { enabled: true, minPasswordLength: 8, async sendVerificationEmail(user: User, url: string) { await sendVerificationEmail(user, url); }, sendEmailVerificationOnSignUp: true, sendResetPasswordEmail: true, async sendResetPassword(user: User, url: string) { await sendPasswordResetEmail(user, url); } }, socialProviders: { google: { clientId: process.env.GOOGLE_CLIENT_ID || "", clientSecret: process.env.GOOGLE_CLIENT_SECRET || "", scope: ["openid", "profile", "email"], }, facebook: { clientId: process.env.FACEBOOK_CLIENT_ID || "", clientSecret: process.env.FACEBOOK_CLIENT_SECRET || "", scope: ["email", "public_profile"], } }, account: { accountLinking: { enabled: true, trustedProviders: ["google", "facebook"], } }, rateLimit: { window: 60, max: 10, }, }); ``` **schema.ts** ```import { pgTable, text, timestamp, boolean, pgEnum } from "drizzle-orm/pg-core"; export const roleEnum = pgEnum('role', ['admin', 'user', 'retailer', 'partner']); export const user = pgTable("user", { id: text("id").primaryKey(), name: text('name').notNull(), email: text('email').notNull().unique(), emailVerified: boolean('emailVerified').notNull(), image: text('image'), role: roleEnum('role').notNull().default('user'), createdAt: timestamp('createdAt').notNull(), updatedAt: timestamp('updatedAt').notNull() }); export const session = pgTable("session", { id: text("id").primaryKey(), expiresAt: timestamp('expiresAt').notNull(), ipAddress: text('ipAddress'), userAgent: text('userAgent'), userId: text('userId').notNull().references(() => user.id) }); export const account = pgTable("account", { id: text("id").primaryKey(), accountId: text('accountId').notNull(), providerId: text('providerId').notNull(), userId: text('userId').notNull().references(() => user.id), accessToken: text('accessToken'), refreshToken: text('refreshToken'), idToken: text('idToken'), expiresAt: timestamp('expiresAt'), password: text('password') }); export const verification = pgTable("verification", { id: text("id").primaryKey(), identifier: text('identifier').notNull(), value: text('value').notNull(), expiresAt: timestamp('expiresAt').notNull() }); ```
Author
Owner

@Bekacru commented on GitHub (Oct 19, 2024):

can you check on latest?

@Bekacru commented on GitHub (Oct 19, 2024): can you check on latest?
Author
Owner

@PawelPotempa commented on GitHub (Oct 19, 2024):

Just did, the exact same issue persists.
Version: "better-auth": "0.5.2-beta.1"

@PawelPotempa commented on GitHub (Oct 19, 2024): Just did, the exact same issue persists. Version: `"better-auth": "0.5.2-beta.1"`
Author
Owner

@Bekacru commented on GitHub (Oct 19, 2024):

It should be fixed on latest!

@Bekacru commented on GitHub (Oct 19, 2024): It should be fixed on latest!
Author
Owner

@PawelPotempa commented on GitHub (Oct 19, 2024):

Google worked just fine now! The same issue remains with facebook.

Also worth noting that if an account for email mail@example.com exists, an attempt to sign up with Facebook for the same email doesn't return any error - but doesn't create the account/link it either, despite having trustedProviders: ["google", "facebook"] which, as I understand - should allow linking.

@PawelPotempa commented on GitHub (Oct 19, 2024): Google worked just fine now! The same issue remains with facebook. Also worth noting that if an account for email mail@example.com exists, an attempt to sign up with Facebook for the same email doesn't return any error - but doesn't create the account/link it either, despite having `trustedProviders: ["google", "facebook"]` which, as I understand - should allow linking.
Author
Owner

@Bekacru commented on GitHub (Oct 19, 2024):

can you check for both issues on latest

@Bekacru commented on GitHub (Oct 19, 2024): can you check for both issues on latest
Author
Owner

@PawelPotempa commented on GitHub (Oct 19, 2024):

We're getting there!

Account linking worked just fine. I've tried Facebook to Google, Facebook to unverified email credentials and Facebook to verified email credentials.

What still doesn't work is creating the user with facebook (when there's no account for that email in the database), still the same error:

error: null value in column "emailVerified" of relation "user" violates not-null constraint

@PawelPotempa commented on GitHub (Oct 19, 2024): We're getting there! Account linking worked just fine. I've tried Facebook to Google, Facebook to unverified email credentials and Facebook to verified email credentials. What still doesn't work is creating the user with facebook (when there's no account for that email in the database), still the same error: `error: null value in column "emailVerified" of relation "user" violates not-null constraint`
Author
Owner

@Bekacru commented on GitHub (Oct 19, 2024):

should be fixed on latest!

@Bekacru commented on GitHub (Oct 19, 2024): should be fixed on latest!
Author
Owner

@PawelPotempa commented on GitHub (Oct 19, 2024):

It works, nice one! A quick question, just to clarify the flow - when a fresh account is created via facebook and we have trustedProviders: ["facebook"], should the emailVerified be false (as it is now)?

@PawelPotempa commented on GitHub (Oct 19, 2024): It works, nice one! A quick question, just to clarify the flow - when a fresh account is created via facebook and we have `trustedProviders: ["facebook"]`, should the emailVerified be `false` (as it is now)?
Author
Owner

@Bekacru commented on GitHub (Oct 19, 2024):

That's intentional. Even if a provider is trusted, you don't want them claiming an email is verified when it isn't. Unless the provider returns email is verified it will be false by default and if it's enabled email verification is sent to the user email.

@Bekacru commented on GitHub (Oct 19, 2024): That's intentional. Even if a provider is trusted, you don't want them claiming an email is verified when it isn't. Unless the provider returns email is verified it will be false by default and if it's enabled email verification is sent to the user email.
Author
Owner

@PawelPotempa commented on GitHub (Oct 19, 2024):

Got it. Thanks for assistance!

@PawelPotempa commented on GitHub (Oct 19, 2024): Got it. Thanks for assistance!
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#101