Skip to content

Commit ee35533

Browse files
authored
feat(registration): disable registration + handle env booleans (#501)
* feat: disable registration + handle env booleans * chore: removing pre-process because we need to use util * chore: format
1 parent 751c36a commit ee35533

File tree

5 files changed

+51
-14
lines changed

5 files changed

+51
-14
lines changed

apps/sim/app/(auth)/signup/page.tsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { env, isTruthy } from '@/lib/env'
12
import { getOAuthProviderStatus } from '../components/oauth-provider-checker'
23
import SignupForm from './signup-form'
34

@@ -7,6 +8,10 @@ export const dynamic = 'force-dynamic'
78
export default async function SignupPage() {
89
const { githubAvailable, googleAvailable, isProduction } = await getOAuthProviderStatus()
910

11+
if (isTruthy(env.DISABLE_REGISTRATION)) {
12+
return <div>Registration is disabled, please contact your admin.</div>
13+
}
14+
1015
return (
1116
<SignupForm
1217
githubAvailable={githubAvailable}

apps/sim/app/(auth)/verify/use-verification.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import { useEffect, useState } from 'react'
44
import { useRouter, useSearchParams } from 'next/navigation'
55
import { client } from '@/lib/auth-client'
6+
import { env, isTruthy } from '@/lib/env'
67
import { createLogger } from '@/lib/logs/console-logger'
78
import { useNotificationStore } from '@/stores/notifications/store'
89

@@ -47,7 +48,9 @@ export function useVerification({
4748

4849
// Debug notification store
4950
useEffect(() => {
50-
logger.info('Notification store state:', { addNotification: !!addNotification })
51+
logger.info('Notification store state:', {
52+
addNotification: !!addNotification,
53+
})
5154
}, [addNotification])
5255

5356
useEffect(() => {
@@ -154,7 +157,10 @@ export function useVerification({
154157
// Set both state variables to ensure the error shows
155158
setIsInvalidOtp(true)
156159
setErrorMessage(message)
157-
logger.info('Error state after API error:', { isInvalidOtp: true, errorMessage: message })
160+
logger.info('Error state after API error:', {
161+
isInvalidOtp: true,
162+
errorMessage: message,
163+
})
158164
// Clear the OTP input on invalid code
159165
setOtp('')
160166
}
@@ -173,7 +179,10 @@ export function useVerification({
173179
// Set both state variables to ensure the error shows
174180
setIsInvalidOtp(true)
175181
setErrorMessage(message)
176-
logger.info('Error state after caught error:', { isInvalidOtp: true, errorMessage: message })
182+
logger.info('Error state after caught error:', {
183+
isInvalidOtp: true,
184+
errorMessage: message,
185+
})
177186

178187
// Clear the OTP input on error
179188
setOtp('')
@@ -218,7 +227,7 @@ export function useVerification({
218227
logger.info('Auto-verifying user', { email: storedEmail })
219228
}
220229

221-
const isDevOrDocker = !isProduction || process.env.DOCKER_BUILD === 'true'
230+
const isDevOrDocker = !isProduction || isTruthy(env.DOCKER_BUILD)
222231

223232
// Auto-verify and redirect in development/docker environments
224233
if (isDevOrDocker || !hasResendKey) {

apps/sim/lib/auth.ts

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { stripe } from '@better-auth/stripe'
22
import { betterAuth } from 'better-auth'
33
import { drizzleAdapter } from 'better-auth/adapters/drizzle'
44
import { 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'
66
import { and, eq } from 'drizzle-orm'
77
import { headers } from 'next/headers'
88
import { Resend } from 'resend'
@@ -16,7 +16,7 @@ import {
1616
import { createLogger } from '@/lib/logs/console-logger'
1717
import { db } from '@/db'
1818
import * as schema from '@/db/schema'
19-
import { env } from './env'
19+
import { env, isTruthy } from './env'
2020
import { getEmailDomain } from './urls/utils'
2121

2222
const 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

apps/sim/lib/env.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export const env = createEnv({
1111
DATABASE_URL: z.string().url(),
1212
BETTER_AUTH_URL: z.string().url(),
1313
BETTER_AUTH_SECRET: z.string().min(32),
14+
DISABLE_REGISTRATION: z.boolean().optional(),
1415
ENCRYPTION_KEY: z.string().min(32),
1516

1617
POSTGRES_URL: z.string().url().optional(),
@@ -110,3 +111,7 @@ export const env = createEnv({
110111
NEXT_PUBLIC_GOOGLE_PROJECT_NUMBER: getEnv('NEXT_PUBLIC_GOOGLE_PROJECT_NUMBER'),
111112
},
112113
})
114+
115+
// Needing this utility because t3-env is returning string for boolean values.
116+
export const isTruthy = (value: string | boolean | number | undefined) =>
117+
typeof value === 'string' ? value === 'true' || value === '1' : Boolean(value)

apps/sim/next.config.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import path from 'path'
22
import { withSentryConfig } from '@sentry/nextjs'
33
import type { NextConfig } from 'next'
4-
import { env } from './lib/env'
4+
import { env, isTruthy } from './lib/env'
55

66
const nextConfig: NextConfig = {
77
devIndicators: false,
@@ -13,12 +13,12 @@ const nextConfig: NextConfig = {
1313
],
1414
},
1515
typescript: {
16-
ignoreBuildErrors: env.DOCKER_BUILD,
16+
ignoreBuildErrors: isTruthy(env.DOCKER_BUILD),
1717
},
1818
eslint: {
19-
ignoreDuringBuilds: env.DOCKER_BUILD,
19+
ignoreDuringBuilds: isTruthy(env.DOCKER_BUILD),
2020
},
21-
output: env.DOCKER_BUILD ? 'standalone' : undefined,
21+
output: isTruthy(env.DOCKER_BUILD) ? 'standalone' : undefined,
2222
turbopack: {
2323
resolveExtensions: ['.tsx', '.ts', '.jsx', '.js', '.mjs', '.json'],
2424
},
@@ -99,7 +99,10 @@ const nextConfig: NextConfig = {
9999
source: '/api/workflows/:id/execute',
100100
headers: [
101101
{ key: 'Access-Control-Allow-Origin', value: '*' },
102-
{ key: 'Access-Control-Allow-Methods', value: 'GET,POST,OPTIONS,PUT' },
102+
{
103+
key: 'Access-Control-Allow-Methods',
104+
value: 'GET,POST,OPTIONS,PUT',
105+
},
103106
{
104107
key: 'Access-Control-Allow-Headers',
105108
value:

0 commit comments

Comments
 (0)