feat(auth): refine demo mode support using SHUMAI_DEMO_MODE API

This commit is contained in:
yiling
2026-06-19 00:36:23 +08:00
parent 5c285b172b
commit 53c132e41d
9 changed files with 16 additions and 17 deletions
-1
View File
@@ -1,5 +1,4 @@
[serve.static] [serve.static]
env = "PUBLIC_*"
plugins = ["bun-plugin-tailwind"] plugins = ["bun-plugin-tailwind"]
[install] [install]
+4 -2
View File
@@ -17,7 +17,8 @@ describe('authn api', () => {
it('GET /signup-info', async () => { it('GET /signup-info', async () => {
mockGetSignupInfo.mockResolvedValue({ mockGetSignupInfo.mockResolvedValue({
userCount: 5, initialized: true,
demoMode: false,
}) })
const app = new Hono().route('/', authnRoute) const app = new Hono().route('/', authnRoute)
@@ -25,7 +26,8 @@ describe('authn api', () => {
expect(res.status).toBe(200) expect(res.status).toBe(200)
expect(await res.json()).toEqual({ expect(await res.json()).toEqual({
userCount: 5, initialized: true,
demoMode: false,
}) })
}) })
}) })
+2 -1
View File
@@ -17,7 +17,8 @@ const route = app
.get('/signup-info', async (c) => { .get('/signup-info', async (c) => {
const info = await teamService.getSignupInfo() const info = await teamService.getSignupInfo()
return c.json({ return c.json({
userCount: info.userCount, initialized: info.initialized,
demoMode: info.demoMode,
}) })
}) })
.get('/me', async (c) => { .get('/me', async (c) => {
+2 -2
View File
@@ -32,7 +32,7 @@ describe('authMiddleware', () => {
beforeEach(() => { beforeEach(() => {
vi.resetAllMocks() vi.resetAllMocks()
delete process.env.PUBLIC_DEMO_MODE delete process.env.SHUMAI_DEMO_MODE
}) })
it('allows access when authenticated', async () => { it('allows access when authenticated', async () => {
@@ -54,7 +54,7 @@ describe('authMiddleware', () => {
describe('read-only mode', () => { describe('read-only mode', () => {
beforeEach(() => { beforeEach(() => {
process.env.PUBLIC_DEMO_MODE = '1' process.env.SHUMAI_DEMO_MODE = '1'
}) })
it('allows GET for reviewer', async () => { it('allows GET for reviewer', async () => {
+1 -1
View File
@@ -29,7 +29,7 @@ export const authMiddleware = createMiddleware<{
c.set('user', user) c.set('user', user)
if (process.env.PUBLIC_DEMO_MODE === '1') { if (process.env.SHUMAI_DEMO_MODE === '1') {
const method = c.req.method.toUpperCase() const method = c.req.method.toUpperCase()
const path = c.req.path const path = c.req.path
if (['POST', 'PUT', 'PATCH', 'DELETE'].includes(method) && !path.endsWith('/search')) { if (['POST', 'PUT', 'PATCH', 'DELETE'].includes(method) && !path.endsWith('/search')) {
+1 -1
View File
@@ -34,7 +34,7 @@ export const auth = betterAuth({
const info = await teamService.getSignupInfo() const info = await teamService.getSignupInfo()
if (info.userCount > 0) { if (info.initialized) {
if (!inviteCode) { if (!inviteCode) {
console.log('[Better Auth Hook] Public signup is disabled') console.log('[Better Auth Hook] Public signup is disabled')
return ctx.json( return ctx.json(
+2
View File
@@ -349,7 +349,9 @@ export class TeamService {
const count = await prisma.user.count() const count = await prisma.user.count()
return { return {
initialized: count > 0,
userCount: count, userCount: count,
demoMode: process.env.SHUMAI_DEMO_MODE === '1',
} }
} }
+3 -8
View File
@@ -27,13 +27,6 @@ function LoginPage() {
const [error, setError] = useState<string | null>(null) const [error, setError] = useState<string | null>(null)
const [loading, setLoading] = useState(false) const [loading, setLoading] = useState(false)
let isDemoEnv
try {
isDemoEnv = process.env.PUBLIC_DEMO_MODE === '1'
} catch {
isDemoEnv = false
}
const { data: signupInfo, isLoading: isSignupInfoLoading } = useQuery({ const { data: signupInfo, isLoading: isSignupInfoLoading } = useQuery({
queryKey: ['/signup-info'], queryKey: ['/signup-info'],
queryFn: async () => { queryFn: async () => {
@@ -44,11 +37,13 @@ function LoginPage() {
}) })
useEffect(() => { useEffect(() => {
if (signupInfo?.userCount === 0) { if (signupInfo && !signupInfo.initialized) {
navigate({ to: '/signup', search: { inviteCode: '' } }) navigate({ to: '/signup', search: { inviteCode: '' } })
} }
}, [signupInfo, navigate]) }, [signupInfo, navigate])
const isDemoEnv = signupInfo?.demoMode === true
const form = useForm<LoginSchema>({ const form = useForm<LoginSchema>({
resolver: zodResolver(loginSchema), resolver: zodResolver(loginSchema),
}) })
+1 -1
View File
@@ -96,7 +96,7 @@ function SignupPage() {
) )
} }
const isFirstUser = signupInfo?.userCount === 0 const isFirstUser = !signupInfo?.initialized
const isPublicSignupDisabled = !isFirstUser && !inviteCode const isPublicSignupDisabled = !isFirstUser && !inviteCode
if (isPublicSignupDisabled) { if (isPublicSignupDisabled) {