[GH-ISSUE #4964] need ctx.redirect() in mapProfileToUser #10123

Closed
opened 2026-04-13 06:02:55 -05:00 by GiteaMirror · 3 comments
Owner

Originally created by @preAGIcoder on GitHub (Sep 29, 2025).
Original GitHub issue: https://github.com/better-auth/better-auth/issues/4964

Is this suited for github?

  • Yes, this is suited for github

To Reproduce

none

Current vs. Expected behavior

Currently i dont have any way to revoke "signup with google" flow in case the email is not a proffessional email. Email is not available in the before hooks in case of googlel signin and if i validate the email in the after hook the session is already created so it is not helpful. As a work around i validate it in mapProfileToUser where i have the email and session is not yet created.

expected behaviour, it would be great i could do ctx.redirect() in mapProfileToUser, because throwing an APIerror here shows error json to user which is not good.

  mapProfileToUser: async (profile) => {
    const user = await prisma.users.findFirst({
      where: {
        email: profile.email,
      },
    });
    // not existing user ie signup
    if (!user) {
      const error = await validateNewRegisterHelper({
        email: profile.email,
        name: profile.name,
      });
      if (error === 'PleaseUseProfessionalEmail') {
        throw new APIError('NOT_ACCEPTABLE', {
          message: 'Please use professional email',
          customQuery: { error: 'PleaseUseProfessionalEmail' },
        });
      } else if (error === 'CompanyAlreadyRegistered') {
        throw new APIError('NOT_ACCEPTABLE', { // here ctx.redirect() instead would be helpful
          message: 'Company already registered!',
          customQuery: { error: 'CompanyAlreadyRegistered' },
        });
      }
      return { ...profile, status: 'pending' };
    }
    return { id: String(user.id), ...profile };
  },

What version of Better Auth are you using?

https://pkg.pr.new/better-auth/better-auth@1828

System info

{
  "system": {
    "platform": "win32",
    "arch": "x64",
    "version": "Windows 10 Pro",
    "release": "10.0.19045",
    "cpuCount": 8,
    "cpuModel": "Intel(R) Core(TM) i7-6700T CPU @ 2.80GHz",
    "totalMemory": "15.92 GB",
    "freeMemory": "6.46 GB"
  },
  "node": {
    "version": "v22.11.0",
    "env": "development"
  },
  "packageManager": {
    "name": "npm",
    "version": "10.9.1"
  },
  "frameworks": [
    {
      "name": "next",
      "version": "^14.2.25"
    },
    {
      "name": "react",
      "version": "^18"
    }
  ],
  "databases": [
    {
      "name": "mysql2",
      "version": "^3.12.0"
    },
    {
      "name": "@prisma/client",
      "version": "^5.15.1"
    }
  ],
  "betterAuth": {
    "version": "https://pkg.pr.new/better-auth/better-auth@1828",
    "config": null
  }
}

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

Other

Auth config (if applicable)

import { betterAuth } from "better-auth"
export const auth = betterAuth({
  emailAndPassword: {  
    enabled: true
  },
});

Additional context

No response

Originally created by @preAGIcoder on GitHub (Sep 29, 2025). Original GitHub issue: https://github.com/better-auth/better-auth/issues/4964 ### Is this suited for github? - [x] Yes, this is suited for github ### To Reproduce none ### Current vs. Expected behavior Currently i dont have any way to revoke "signup with google" flow in case the email is not a proffessional email. Email is not available in the before hooks in case of googlel signin and if i validate the email in the after hook the session is already created so it is not helpful. As a work around i validate it in mapProfileToUser where i have the email and session is not yet created. expected behaviour, it would be great i could do ctx.redirect() in mapProfileToUser, because throwing an APIerror here shows error json to user which is not good. mapProfileToUser: async (profile) => { const user = await prisma.users.findFirst({ where: { email: profile.email, }, }); // not existing user ie signup if (!user) { const error = await validateNewRegisterHelper({ email: profile.email, name: profile.name, }); if (error === 'PleaseUseProfessionalEmail') { throw new APIError('NOT_ACCEPTABLE', { message: 'Please use professional email', customQuery: { error: 'PleaseUseProfessionalEmail' }, }); } else if (error === 'CompanyAlreadyRegistered') { throw new APIError('NOT_ACCEPTABLE', { // here ctx.redirect() instead would be helpful message: 'Company already registered!', customQuery: { error: 'CompanyAlreadyRegistered' }, }); } return { ...profile, status: 'pending' }; } return { id: String(user.id), ...profile }; }, ### What version of Better Auth are you using? https://pkg.pr.new/better-auth/better-auth@1828 ### System info ```bash { "system": { "platform": "win32", "arch": "x64", "version": "Windows 10 Pro", "release": "10.0.19045", "cpuCount": 8, "cpuModel": "Intel(R) Core(TM) i7-6700T CPU @ 2.80GHz", "totalMemory": "15.92 GB", "freeMemory": "6.46 GB" }, "node": { "version": "v22.11.0", "env": "development" }, "packageManager": { "name": "npm", "version": "10.9.1" }, "frameworks": [ { "name": "next", "version": "^14.2.25" }, { "name": "react", "version": "^18" } ], "databases": [ { "name": "mysql2", "version": "^3.12.0" }, { "name": "@prisma/client", "version": "^5.15.1" } ], "betterAuth": { "version": "https://pkg.pr.new/better-auth/better-auth@1828", "config": null } } ``` ### Which area(s) are affected? (Select all that apply) Other ### Auth config (if applicable) ```typescript import { betterAuth } from "better-auth" export const auth = betterAuth({ emailAndPassword: { enabled: true }, }); ``` ### Additional context _No response_
GiteaMirror added the lockedenhancement labels 2026-04-13 06:02:55 -05:00
Author
Owner

@dosubot[bot] commented on GitHub (Sep 29, 2025):

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

It's not possible to use ctx.redirect() inside mapProfileToUser because this function is designed as a pure transformation step and doesn't have access to the request context or any redirect mechanism. Redirects in Better Auth's OAuth flow are handled in the OAuth callback handler, after mapProfileToUser returns. If you need to redirect based on validation (like requiring a professional email), you should signal this in the return value or error from mapProfileToUser, then handle the redirect in the OAuth callback handler using ctx.redirect or a similar helper before the session is created. Supporting redirects directly in mapProfileToUser would require architectural changes to Better Auth's internals example example.

To reply, just mention @dosu.


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

<!-- gh-comment-id:3345091978 --> @dosubot[bot] commented on GitHub (Sep 29, 2025): <!-- Greeting --> Hi @preAGIcoder! I'm [Dosu](https://go.dosu.dev/dosubot) and I’m helping the better-auth team. <!-- Answer --> It's not possible to use ctx.redirect() inside mapProfileToUser because this function is designed as a pure transformation step and doesn't have access to the request context or any redirect mechanism. Redirects in Better Auth's OAuth flow are handled in the OAuth callback handler, after mapProfileToUser returns. If you need to redirect based on validation (like requiring a professional email), you should signal this in the return value or error from mapProfileToUser, then handle the redirect in the OAuth callback handler using ctx.redirect or a similar helper before the session is created. Supporting redirects directly in mapProfileToUser would require architectural changes to Better Auth's internals [example](https://github.com/better-auth/better-auth/blob/f6cbdcc84ee5d2971fdcc8b23ff7c174f88cf45b/packages/better-auth/src/plugins/generic-oauth/index.ts) [example](https://github.com/better-auth/better-auth/pull/4650). <!-- 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/a0a674fb-eda4-454e-999c-6a09f598d2cd?feedback_type=great_response) | [Irrelevant](https://app.dosu.dev/response-feedback/a0a674fb-eda4-454e-999c-6a09f598d2cd?feedback_type=irrelevant_answer) | [Incorrect](https://app.dosu.dev/response-feedback/a0a674fb-eda4-454e-999c-6a09f598d2cd?feedback_type=incorrect_sources) | [Verbose](https://app.dosu.dev/response-feedback/a0a674fb-eda4-454e-999c-6a09f598d2cd?feedback_type=too_verbose) | [Hallucination](https://app.dosu.dev/response-feedback/a0a674fb-eda4-454e-999c-6a09f598d2cd?feedback_type=hallucination) | [Report 🐛](https://app.dosu.dev/response-feedback/a0a674fb-eda4-454e-999c-6a09f598d2cd?feedback_type=bug_report) | [Other](https://app.dosu.dev/response-feedback/a0a674fb-eda4-454e-999c-6a09f598d2cd?feedback_type=other)</sup>&nbsp;&nbsp;[![Chat with Dosu](https://dosu.dev/dosu-chat-badge.svg)](https://app.dosu.dev/cdda13d9-dd27-4d31-b09a-5d8bec92de21/ask?utm_source=github)&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/4964)
Author
Owner

@DrJume commented on GitHub (Mar 26, 2026):

this would be really helpful

<!-- gh-comment-id:4133811092 --> @DrJume commented on GitHub (Mar 26, 2026): this would be really helpful
Author
Owner

@github-actions[bot] commented on GitHub (Apr 3, 2026):

This issue has been locked as it was closed more than 7 days ago. If you're experiencing a similar problem or you have additional context, please open a new issue and reference this one.

<!-- gh-comment-id:4181127276 --> @github-actions[bot] commented on GitHub (Apr 3, 2026): This issue has been locked as it was closed more than 7 days ago. If you're experiencing a similar problem or you have additional context, please open a new issue and reference this one.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#10123