[GH-ISSUE #233] Bug: Google Social Login State Inconsistency #8182

Closed
opened 2026-04-13 03:17:03 -05:00 by GiteaMirror · 9 comments
Owner

Originally created by @rafayexalter on GitHub (Oct 18, 2024).
Original GitHub issue: https://github.com/better-auth/better-auth/issues/233

Describe the bug
After successfully logging in with Google authentication, the functionality works as expected. However, when I clear the user data (database, local storage and cookies) and attempt to log in again, there's an inconsistency: while the login appears successful, error messages appear in the console.

Expected behavior
It goes to social provider login then ask us to sign in again

Screenshots
Screenshot 2024-10-19 021536

Originally created by @rafayexalter on GitHub (Oct 18, 2024). Original GitHub issue: https://github.com/better-auth/better-auth/issues/233 **Describe the bug** After successfully logging in with Google authentication, the functionality works as expected. However, when I clear the user data (database, local storage and cookies) and attempt to log in again, there's an inconsistency: while the login appears successful, error messages appear in the console. **Expected behavior** It goes to social provider login then ask us to sign in again **Screenshots** ![Screenshot 2024-10-19 021536](https://github.com/user-attachments/assets/900721e7-47af-4cf6-b77c-e441b4980a4f)
GiteaMirror added the locked label 2026-04-13 03:17:03 -05:00
Author
Owner

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

I think this is cause you forgot to clear the account from the accounts table

<!-- gh-comment-id:2423721293 --> @Bekacru commented on GitHub (Oct 19, 2024): I think this is cause you forgot to clear the account from the accounts table
Author
Owner

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

I think this is cause you forgot to clear the account from the accounts table

Just rechecked it, after cleaning all tables, same issue. I might be missing something.

auth.ts

import { betterAuth } from "better-auth";
import prisma from "./db";
import { prismaAdapter } from "better-auth/adapters/prisma";

export const auth = betterAuth({
  database: prismaAdapter(prisma, {
    provider: "postgresql",
  }),
  socialProviders: {
    google: {
      clientId: process.env.GOOGLE_CLIENT_ID as string,
      clientSecret: process.env.GOOGLE_CLIENT_SECRET as string,
      redirectUrl: "http://localhost:3000/api/auth/callback/google",
    },
  },
});

auth-client.ts

import { createAuthClient } from "better-auth/client";

export const authClient = createAuthClient({});

export const { signOut, useSession, signIn } = authClient;

sign-in.tsx

<Button
        variant="secondary"
        className="w-full gap-2"
        onClick={async () => {
          await signIn.social({
            provider: "google",
          });
        }}
      >
        Continue with Google
</Button>
<!-- gh-comment-id:2423729079 --> @rafayexalter commented on GitHub (Oct 19, 2024): > I think this is cause you forgot to clear the account from the accounts table Just rechecked it, after cleaning all tables, same issue. I might be missing something. auth.ts ``` import { betterAuth } from "better-auth"; import prisma from "./db"; import { prismaAdapter } from "better-auth/adapters/prisma"; export const auth = betterAuth({ database: prismaAdapter(prisma, { provider: "postgresql", }), socialProviders: { google: { clientId: process.env.GOOGLE_CLIENT_ID as string, clientSecret: process.env.GOOGLE_CLIENT_SECRET as string, redirectUrl: "http://localhost:3000/api/auth/callback/google", }, }, }); ``` auth-client.ts ``` import { createAuthClient } from "better-auth/client"; export const authClient = createAuthClient({}); export const { signOut, useSession, signIn } = authClient; ``` sign-in.tsx ``` <Button variant="secondary" className="w-full gap-2" onClick={async () => { await signIn.social({ provider: "google", }); }} > Continue with Google </Button> ```
Author
Owner

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

The issue seems to be that userId isn't returned from Prisma after user creation. Not sure why it works on first but not on subsequent tries. If the database was fully cleared, that shouldn't happen. I've published a potential fix. Please try it out and let me know if it resolves the issue. You can install it with pnpm add better-auth@next.

<!-- gh-comment-id:2423735008 --> @Bekacru commented on GitHub (Oct 19, 2024): The issue seems to be that `userId` isn't returned from Prisma after user creation. Not sure why it works on first but not on subsequent tries. If the database was fully cleared, that shouldn't happen. I've published a potential fix. Please try it out and let me know if it resolves the issue. You can install it with `pnpm add better-auth@next`.
Author
Owner

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

Thanks for the quick update, I tried npm install better-auth@next

I have created a video of the behaviour.
https://www.loom.com/share/2992c7e90c2f46e699b08d1a73e1470f

<!-- gh-comment-id:2423746671 --> @rafayexalter commented on GitHub (Oct 19, 2024): Thanks for the quick update, I tried `npm install better-auth@next` I have created a video of the behaviour. [https://www.loom.com/share/2992c7e90c2f46e699b08d1a73e1470f](https://www.loom.com/share/2992c7e90c2f46e699b08d1a73e1470f)
Author
Owner

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

so does it still show the prisma error?

<!-- gh-comment-id:2423749916 --> @Bekacru commented on GitHub (Oct 19, 2024): so does it still show the prisma error?
Author
Owner

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

Yes, also it's loggin in directly with google oauth, when I have deleted the data. How's it happening?

<!-- gh-comment-id:2423765161 --> @rafayexalter commented on GitHub (Oct 19, 2024): Yes, also it's loggin in directly with google oauth, when I have deleted the data. How's it happening?
Author
Owner

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

That's expected. If you use social login it'll create you an account if an account with the email doesn't already exist.

<!-- gh-comment-id:2423766902 --> @Bekacru commented on GitHub (Oct 19, 2024): That's expected. If you use social login it'll create you an account if an account with the email doesn't already exist.
Author
Owner

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

understood, well then there's only this prisma issue

PrismaClientValidationError: 
Invalid `prisma.account.create()` invocation:
{
  data: {
    id: "google:26262",
    accountId: "6262626",
    providerId: "google",
    userId: undefined,
    accessToken: "",
    refreshToken: undefined,
    idToken: undefined,
    expiresAt: undefined,
    password: undefined,
+   user: {
+     create: UserCreateWithoutAccountInput | UserUncheckedCreateWithoutAccountInput,
+     connectOrCreate: UserCreateOrConnectWithoutAccountInput,
+     connect: UserWhereUniqueInput
+   }
  }
}
Argument `user` is missing.
<!-- gh-comment-id:2423769809 --> @rafayexalter commented on GitHub (Oct 19, 2024): understood, well then there's only this prisma issue ``` PrismaClientValidationError: Invalid `prisma.account.create()` invocation: { data: { id: "google:26262", accountId: "6262626", providerId: "google", userId: undefined, accessToken: "", refreshToken: undefined, idToken: undefined, expiresAt: undefined, password: undefined, + user: { + create: UserCreateWithoutAccountInput | UserUncheckedCreateWithoutAccountInput, + connectOrCreate: UserCreateOrConnectWithoutAccountInput, + connect: UserWhereUniqueInput + } } } Argument `user` is missing. ```
Author
Owner

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

this is fixed on latest!

<!-- gh-comment-id:2423822890 --> @Bekacru commented on GitHub (Oct 19, 2024): this is fixed on latest!
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#8182