@@ -2,7 +2,7 @@ import { stripe } from '@better-auth/stripe'
22import { betterAuth } from 'better-auth'
33import { drizzleAdapter } from 'better-auth/adapters/drizzle'
44import { nextCookies } from 'better-auth/next-js'
5- import { emailOTP , genericOAuth , organization } from 'better-auth/plugins'
5+ import { createAuthMiddleware , emailOTP , genericOAuth , organization } from 'better-auth/plugins'
66import { and , eq } from 'drizzle-orm'
77import { headers } from 'next/headers'
88import { Resend } from 'resend'
@@ -16,7 +16,7 @@ import {
1616import { createLogger } from '@/lib/logs/console-logger'
1717import { db } from '@/db'
1818import * as schema from '@/db/schema'
19- import { env } from './env'
19+ import { env , isTruthy } from './env'
2020import { getEmailDomain } from './urls/utils'
2121
2222const logger = createLogger ( 'Auth' )
@@ -93,10 +93,15 @@ export const auth = betterAuth({
9393 } ,
9494 }
9595 }
96- logger . info ( 'No organizations found for user' , { userId : session . userId } )
96+ logger . info ( 'No organizations found for user' , {
97+ userId : session . userId ,
98+ } )
9799 return { data : session }
98100 } catch ( error ) {
99- logger . error ( 'Error setting active organization' , { error, userId : session . userId } )
101+ logger . error ( 'Error setting active organization' , {
102+ error,
103+ userId : session . userId ,
104+ } )
100105 return { data : session }
101106 }
102107 } ,
@@ -158,6 +163,14 @@ export const auth = betterAuth({
158163 }
159164 } ,
160165 } ,
166+ hooks : {
167+ before : createAuthMiddleware ( async ( ctx ) => {
168+ if ( ctx . path . startsWith ( '/sign-up' ) && isTruthy ( env . DISABLE_REGISTRATION ) )
169+ throw new Error ( 'Registration is disabled, please contact your admin.' )
170+
171+ return
172+ } ) ,
173+ } ,
161174 plugins : [
162175 nextCookies ( ) ,
163176 emailOTP ( {
@@ -469,7 +482,9 @@ export const auth = betterAuth({
469482 userId = decodedToken . sub
470483 }
471484 } catch ( e ) {
472- logger . warn ( 'Failed to decode Supabase ID token' , { error : e } )
485+ logger . warn ( 'Failed to decode Supabase ID token' , {
486+ error : e ,
487+ } )
473488 }
474489 }
475490
0 commit comments