genericOAuth with Square does not return session #1419

Closed
opened 2026-03-13 08:39:06 -05:00 by GiteaMirror · 1 comment
Owner

Originally created by @mi3lix9 on GitHub (Jun 26, 2025).

Is this suited for github?

  • Yes, this is suited for github

To Reproduce

  1. add square credentials
  2. add the backend return uri in Square
  3. use getUserInfo to get merchant
  4. sign in with square in the front end, and callbackURL as the frontend url

Current vs. Expected behavior

It should create a user and return a session, but the user is created without a session

What version of Better Auth are you using?

1.2.7

Provide environment information

- OS: macOS
- Browser: Zen
- Backend Hosting: Railway
- Frontend: locally

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

Client

Auth config (if applicable)

import { betterAuth } from "better-auth"
export const auth = betterAuth({
  emailAndPassword: {  
    enabled: true
  },
  genericOAuth({
     config: [
       {
         providerId: "square",
         clientId: "",
         clientSecret: "",
         authorizationUrl: `https://squareup.com/oauth2/authorize`,
         tokenUrl: `https://squareup.com/oauth2/token`,
         scopes: ["MERCHANT_PROFILE_READ"],
         redirectURI: `http://localhost:3000/api/auth/oauth2/callback/square`,

         getUserInfo: async (tokens) => {
           const square = createSquareClient({ token: tokens.accessToken! });
           const merchant = await square.merchants.list();
           const user = merchant.data[0];

           return {
             id: user?.id!,
             name: user?.businessName!,
             email: user?.businessName + "@jaicome_square.com",
             createdAt: new Date(),
             updatedAt: new Date(),
             emailVerified: true,
           };
         },
       },
     ],
  }),
});

Additional context

https://developer.squareup.com/docs/oauth-api/overview

Originally created by @mi3lix9 on GitHub (Jun 26, 2025). ### Is this suited for github? - [x] Yes, this is suited for github ### To Reproduce 1. add square credentials 2. add the backend return uri in Square 3. use getUserInfo to get merchant 4. sign in with square in the front end, and callbackURL as the frontend url ### Current vs. Expected behavior It should create a user and return a session, but the user is created without a session ### What version of Better Auth are you using? 1.2.7 ### Provide environment information ```bash - OS: macOS - Browser: Zen - Backend Hosting: Railway - Frontend: locally ``` ### Which area(s) are affected? (Select all that apply) Client ### Auth config (if applicable) ```typescript import { betterAuth } from "better-auth" export const auth = betterAuth({ emailAndPassword: { enabled: true }, genericOAuth({ config: [ { providerId: "square", clientId: "", clientSecret: "", authorizationUrl: `https://squareup.com/oauth2/authorize`, tokenUrl: `https://squareup.com/oauth2/token`, scopes: ["MERCHANT_PROFILE_READ"], redirectURI: `http://localhost:3000/api/auth/oauth2/callback/square`, getUserInfo: async (tokens) => { const square = createSquareClient({ token: tokens.accessToken! }); const merchant = await square.merchants.list(); const user = merchant.data[0]; return { id: user?.id!, name: user?.businessName!, email: user?.businessName + "@jaicome_square.com", createdAt: new Date(), updatedAt: new Date(), emailVerified: true, }; }, }, ], }), }); ``` ### Additional context https://developer.squareup.com/docs/oauth-api/overview
Author
Owner

@dosubot[bot] commented on GitHub (Jun 26, 2025):

Hi @mi3lix9! I'm Dosu and I’m helping the better-auth team.

The most common reason for this issue is that the user object returned by your getUserInfo function is missing a required field, usually a valid email. Better Auth requires a unique, valid email for session creation—if it's missing or malformed, the session won't be created, even if the user is created in the database. This is enforced in the genericOAuth flow and will silently fail or redirect with an error if the email is missing or invalid (source).

In your config, you're generating the email as user?.businessName + "@jaicome_square.com". If businessName is missing or not unique, this could cause issues. Try logging the user object returned by getUserInfo to confirm that id and email are always present and unique for each Square account.

Also, check your browser's network tab or backend logs for any errors or redirects during the OAuth callback—if you see an error like email_is_missing, that's confirmation.

If you want to guarantee emailVerified is set, you can use the mapProfileToUser option as well (example).

If everything looks correct and it still fails, there are known limitations with custom providers in some Better Auth versions; upgrading to a newer or beta version may help (reference).

To reply, just mention @dosu.


How did I do? Good | Irrelevant | Incorrect | Verbose | Hallucination | Report 🐛 | Other  Join Discord Share on X

@dosubot[bot] commented on GitHub (Jun 26, 2025): <!-- Greeting --> Hi @mi3lix9! I'm [Dosu](https://go.dosu.dev/dosubot) and I’m helping the better-auth team. <!-- Answer --> The most common reason for this issue is that the user object returned by your getUserInfo function is missing a required field, usually a valid email. Better Auth requires a unique, valid email for session creation—if it's missing or malformed, the session won't be created, even if the user is created in the database. This is enforced in the genericOAuth flow and will silently fail or redirect with an error if the email is missing or invalid ([source](https://github.com/better-auth/better-auth/blob/58919f854391158bace7d8b9469a948c2d9b8e64/packages/better-auth/src/plugins/generic-oauth/index.ts)). In your config, you're generating the email as user?.businessName + "@jaicome_square.com". If businessName is missing or not unique, this could cause issues. Try logging the user object returned by getUserInfo to confirm that id and email are always present and unique for each Square account. Also, check your browser's network tab or backend logs for any errors or redirects during the OAuth callback—if you see an error like email_is_missing, that's confirmation. If you want to guarantee emailVerified is set, you can use the mapProfileToUser option as well ([example](https://github.com/better-auth/better-auth/issues/2731)). If everything looks correct and it still fails, there are known limitations with custom providers in some Better Auth versions; upgrading to a newer or beta version may help ([reference](https://github.com/better-auth/better-auth/issues/2916)). <!-- Dosu Comment Footer --> *To reply, just mention [@dosu](https://go.dosu.dev/dosubot).* --- <sup>How did I do? [Good](https://app.dosu.dev/response-feedback/1ae600b6-2093-4134-ad9d-6caa61443b4d?feedback_type=great_response) | [Irrelevant](https://app.dosu.dev/response-feedback/1ae600b6-2093-4134-ad9d-6caa61443b4d?feedback_type=irrelevant_answer) | [Incorrect](https://app.dosu.dev/response-feedback/1ae600b6-2093-4134-ad9d-6caa61443b4d?feedback_type=incorrect_sources) | [Verbose](https://app.dosu.dev/response-feedback/1ae600b6-2093-4134-ad9d-6caa61443b4d?feedback_type=too_verbose) | [Hallucination](https://app.dosu.dev/response-feedback/1ae600b6-2093-4134-ad9d-6caa61443b4d?feedback_type=hallucination) | [Report 🐛](https://app.dosu.dev/response-feedback/1ae600b6-2093-4134-ad9d-6caa61443b4d?feedback_type=bug_report) | [Other](https://app.dosu.dev/response-feedback/1ae600b6-2093-4134-ad9d-6caa61443b4d?feedback_type=other)</sup>&nbsp;&nbsp;[![Join Discord](https://img.shields.io/badge/join-5865F2?logo=discord&logoColor=white&label=)](https://go.dosu.dev/discord-bot)&nbsp;[![Share on X](https://img.shields.io/badge/X-share-black)](https://twitter.com/intent/tweet?text=%40dosu_ai%20helped%20me%20solve%20this%20issue!&url=https%3A//github.com/better-auth/better-auth/issues/3177)
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#1419