mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-11-19 14:16:29 +00:00
feat(auth): refine demo mode support using SHUMAI_DEMO_MODE API
This commit is contained in:
@@ -1,5 +1,4 @@
|
||||
[serve.static]
|
||||
env = "PUBLIC_*"
|
||||
plugins = ["bun-plugin-tailwind"]
|
||||
|
||||
[install]
|
||||
|
||||
@@ -17,7 +17,8 @@ describe('authn api', () => {
|
||||
|
||||
it('GET /signup-info', async () => {
|
||||
mockGetSignupInfo.mockResolvedValue({
|
||||
userCount: 5,
|
||||
initialized: true,
|
||||
demoMode: false,
|
||||
})
|
||||
|
||||
const app = new Hono().route('/', authnRoute)
|
||||
@@ -25,7 +26,8 @@ describe('authn api', () => {
|
||||
|
||||
expect(res.status).toBe(200)
|
||||
expect(await res.json()).toEqual({
|
||||
userCount: 5,
|
||||
initialized: true,
|
||||
demoMode: false,
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
@@ -17,7 +17,8 @@ const route = app
|
||||
.get('/signup-info', async (c) => {
|
||||
const info = await teamService.getSignupInfo()
|
||||
return c.json({
|
||||
userCount: info.userCount,
|
||||
initialized: info.initialized,
|
||||
demoMode: info.demoMode,
|
||||
})
|
||||
})
|
||||
.get('/me', async (c) => {
|
||||
|
||||
@@ -32,7 +32,7 @@ describe('authMiddleware', () => {
|
||||
|
||||
beforeEach(() => {
|
||||
vi.resetAllMocks()
|
||||
delete process.env.PUBLIC_DEMO_MODE
|
||||
delete process.env.SHUMAI_DEMO_MODE
|
||||
})
|
||||
|
||||
it('allows access when authenticated', async () => {
|
||||
@@ -54,7 +54,7 @@ describe('authMiddleware', () => {
|
||||
|
||||
describe('read-only mode', () => {
|
||||
beforeEach(() => {
|
||||
process.env.PUBLIC_DEMO_MODE = '1'
|
||||
process.env.SHUMAI_DEMO_MODE = '1'
|
||||
})
|
||||
|
||||
it('allows GET for reviewer', async () => {
|
||||
|
||||
@@ -29,7 +29,7 @@ export const authMiddleware = createMiddleware<{
|
||||
|
||||
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 path = c.req.path
|
||||
if (['POST', 'PUT', 'PATCH', 'DELETE'].includes(method) && !path.endsWith('/search')) {
|
||||
|
||||
@@ -34,7 +34,7 @@ export const auth = betterAuth({
|
||||
|
||||
const info = await teamService.getSignupInfo()
|
||||
|
||||
if (info.userCount > 0) {
|
||||
if (info.initialized) {
|
||||
if (!inviteCode) {
|
||||
console.log('[Better Auth Hook] Public signup is disabled')
|
||||
return ctx.json(
|
||||
|
||||
@@ -349,7 +349,9 @@ export class TeamService {
|
||||
const count = await prisma.user.count()
|
||||
|
||||
return {
|
||||
initialized: count > 0,
|
||||
userCount: count,
|
||||
demoMode: process.env.SHUMAI_DEMO_MODE === '1',
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -27,13 +27,6 @@ function LoginPage() {
|
||||
const [error, setError] = useState<string | null>(null)
|
||||
const [loading, setLoading] = useState(false)
|
||||
|
||||
let isDemoEnv
|
||||
try {
|
||||
isDemoEnv = process.env.PUBLIC_DEMO_MODE === '1'
|
||||
} catch {
|
||||
isDemoEnv = false
|
||||
}
|
||||
|
||||
const { data: signupInfo, isLoading: isSignupInfoLoading } = useQuery({
|
||||
queryKey: ['/signup-info'],
|
||||
queryFn: async () => {
|
||||
@@ -44,11 +37,13 @@ function LoginPage() {
|
||||
})
|
||||
|
||||
useEffect(() => {
|
||||
if (signupInfo?.userCount === 0) {
|
||||
if (signupInfo && !signupInfo.initialized) {
|
||||
navigate({ to: '/signup', search: { inviteCode: '' } })
|
||||
}
|
||||
}, [signupInfo, navigate])
|
||||
|
||||
const isDemoEnv = signupInfo?.demoMode === true
|
||||
|
||||
const form = useForm<LoginSchema>({
|
||||
resolver: zodResolver(loginSchema),
|
||||
})
|
||||
|
||||
@@ -96,7 +96,7 @@ function SignupPage() {
|
||||
)
|
||||
}
|
||||
|
||||
const isFirstUser = signupInfo?.userCount === 0
|
||||
const isFirstUser = !signupInfo?.initialized
|
||||
const isPublicSignupDisabled = !isFirstUser && !inviteCode
|
||||
|
||||
if (isPublicSignupDisabled) {
|
||||
|
||||
Reference in New Issue
Block a user