[GH-ISSUE #1282] Customize Ban user login response #8674

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

Originally created by @iamhananbaig on GitHub (Jan 26, 2025).
Original GitHub issue: https://github.com/better-auth/better-auth/issues/1282

Is this suited for github?

  • Yes, this is suited for github

I want to customize the response when a banned user tries to login. currently it shows failed to create session.

Describe the solution you'd like

It should show some thing like your account is banned please contact administrator

Describe alternatives you've considered

I have not considered anything.

Additional context

No response

Originally created by @iamhananbaig on GitHub (Jan 26, 2025). Original GitHub issue: https://github.com/better-auth/better-auth/issues/1282 ### Is this suited for github? - [x] Yes, this is suited for github ### Is your feature request related to a problem? Please describe. I want to customize the response when a banned user tries to login. currently it shows failed to create session. ### Describe the solution you'd like It should show some thing like your account is banned please contact administrator ### Describe alternatives you've considered I have not considered anything. ### Additional context _No response_
GiteaMirror added the locked label 2026-04-13 03:50:03 -05:00
Author
Owner

@jaaneh commented on GitHub (Jan 29, 2025):

Personally, I would like the ability of setting a redirect in auth config, or using middleware to control it manually.

// Next.js - middleware.ts
const publicRoutes = ["/", "/terms", "/privacy"]
const isPublicRoute = publicRoutes.some(route => request.nextUrl.pathname.startsWith(route))
if (session?.user?.banned && !isPublicRoute) {
  return NextResponse.redirect(new URL("/banned", request.url))
}
<!-- gh-comment-id:2621187452 --> @jaaneh commented on GitHub (Jan 29, 2025): Personally, I would like the ability of setting a redirect in auth config, or using middleware to control it manually. ```js // Next.js - middleware.ts const publicRoutes = ["/", "/terms", "/privacy"] const isPublicRoute = publicRoutes.some(route => request.nextUrl.pathname.startsWith(route)) if (session?.user?.banned && !isPublicRoute) { return NextResponse.redirect(new URL("/banned", request.url)) } ```
Author
Owner

@ieedan commented on GitHub (Apr 24, 2025):

Personally, I would like the ability of setting a redirect in auth config, or using middleware to control it manually.

// Next.js - middleware.ts
const publicRoutes = ["/", "/terms", "/privacy"]
const isPublicRoute = publicRoutes.some(route => request.nextUrl.pathname.startsWith(route))
if (session?.user?.banned && !isPublicRoute) {
return NextResponse.redirect(new URL("/banned", request.url))
}

You probably already had this figured out but you can do:

after: createAuthMiddleware(async (ctx) => {
	if (ctx.context.returned instanceof APIError) {
		const returned = ctx.context.returned;

		if (returned.body?.code === "BANNED_USER") {
			throw ctx.redirect('/banned')
		}
	}
}
<!-- gh-comment-id:2828336172 --> @ieedan commented on GitHub (Apr 24, 2025): > Personally, I would like the ability of setting a redirect in auth config, or using middleware to control it manually. > > // Next.js - middleware.ts > const publicRoutes = ["/", "/terms", "/privacy"] > const isPublicRoute = publicRoutes.some(route => request.nextUrl.pathname.startsWith(route)) > if (session?.user?.banned && !isPublicRoute) { > return NextResponse.redirect(new URL("/banned", request.url)) > } You probably already had this figured out but you can do: ```ts after: createAuthMiddleware(async (ctx) => { if (ctx.context.returned instanceof APIError) { const returned = ctx.context.returned; if (returned.body?.code === "BANNED_USER") { throw ctx.redirect('/banned') } } } ```
Author
Owner

@H7ioo commented on GitHub (Jun 24, 2025):

Well I'm currently having this issue
http://localhost:3000/api/auth/error?error=banned&error_description=You%20have%20been%20banned%20from%20this%20application.%20Please%20contact%20support%20if%20you%20believe%20this%20is%20an%20error.

Image

This is what I'm getting.

The after hook didn't work for me

Version: 1.2.10

<!-- gh-comment-id:3001603017 --> @H7ioo commented on GitHub (Jun 24, 2025): Well I'm currently having this issue `http://localhost:3000/api/auth/error?error=banned&error_description=You%20have%20been%20banned%20from%20this%20application.%20Please%20contact%20support%20if%20you%20believe%20this%20is%20an%20error.` ![Image](https://github.com/user-attachments/assets/d6b58cc6-ad8f-49b6-958c-012e618d6bce) This is what I'm getting. The after hook didn't work for me Version: 1.2.10
Author
Owner

@H7ioo commented on GitHub (Jun 24, 2025):

This is the workaround that I used

  hooks: {
    before: createAuthMiddleware(async (ctx) => {
      if (ctx.path === "/error") {
        if (ctx.query && "error" in ctx.query && ctx.query.error === "banned") {
          throw ctx.redirect(`/banned`);
        }
      }
    }),
  },
<!-- gh-comment-id:3001970850 --> @H7ioo commented on GitHub (Jun 24, 2025): This is the workaround that I used ```ts hooks: { before: createAuthMiddleware(async (ctx) => { if (ctx.path === "/error") { if (ctx.query && "error" in ctx.query && ctx.query.error === "banned") { throw ctx.redirect(`/banned`); } } }), }, ```
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#8674