Skip to content

Commit 2490a3c

Browse files
authored
fix(auth): return 403 instead of 500 for blocked sign-in/sign-up attempts (#4783)
The hooks.before middleware threw plain Errors for the four auth-policy gates (registration disabled, email/password disabled, login allowlist, blocked signup domains). better-auth surfaces an uncaught hook Error as a generic 500 SERVER_ERROR, so users hitting these gates saw 'Failed to create account' with no actionable message. Throw APIError('FORBIDDEN', { message }) instead so the endpoints return a clean 403 with the policy message, which the client surfaces directly. Internal/server failures (email send, provider userinfo fetch, ID-token parse) intentionally remain plain Errors so they continue to surface as 500s.
1 parent 2ede04d commit 2490a3c

1 file changed

Lines changed: 13 additions & 5 deletions

File tree

apps/sim/lib/auth/auth.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { toError } from '@sim/utils/errors'
99
import { generateId } from '@sim/utils/id'
1010
import { betterAuth } from 'better-auth'
1111
import { drizzleAdapter } from 'better-auth/adapters/drizzle'
12-
import { createAuthMiddleware } from 'better-auth/api'
12+
import { APIError, createAuthMiddleware } from 'better-auth/api'
1313
import { nextCookies } from 'better-auth/next-js'
1414
import {
1515
admin,
@@ -793,12 +793,16 @@ export const auth = betterAuth({
793793
hooks: {
794794
before: createAuthMiddleware(async (ctx) => {
795795
if (ctx.path.startsWith('/sign-up') && isRegistrationDisabled)
796-
throw new Error('Registration is disabled, please contact your admin.')
796+
throw new APIError('FORBIDDEN', {
797+
message: 'Registration is disabled, please contact your admin.',
798+
})
797799

798800
if (!isEmailPasswordEnabled) {
799801
const emailPasswordPaths = ['/sign-in/email', '/sign-up/email', '/email-otp']
800802
if (emailPasswordPaths.some((path) => ctx.path.startsWith(path)))
801-
throw new Error('Email/password authentication is disabled. Please use SSO to sign in.')
803+
throw new APIError('FORBIDDEN', {
804+
message: 'Email/password authentication is disabled. Please use SSO to sign in.',
805+
})
802806
}
803807

804808
if (
@@ -826,13 +830,17 @@ export const auth = betterAuth({
826830
}
827831

828832
if (!isAllowed) {
829-
throw new Error('Access restricted. Please contact your administrator.')
833+
throw new APIError('FORBIDDEN', {
834+
message: 'Access restricted. Please contact your administrator.',
835+
})
830836
}
831837
}
832838
}
833839

834840
if (ctx.path.startsWith('/sign-up') && isSignupEmailBlocked(ctx.body?.email)) {
835-
throw new Error('Sign-ups from this email domain are not allowed.')
841+
throw new APIError('FORBIDDEN', {
842+
message: 'Sign-ups from this email domain are not allowed.',
843+
})
836844
}
837845

838846
if (ctx.path === '/oauth2/authorize' || ctx.path === '/oauth2/token') {

0 commit comments

Comments
 (0)