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]
|
[serve.static]
|
||||||
env = "PUBLIC_*"
|
|
||||||
plugins = ["bun-plugin-tailwind"]
|
plugins = ["bun-plugin-tailwind"]
|
||||||
|
|
||||||
[install]
|
[install]
|
||||||
|
|||||||
@@ -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,
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -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) => {
|
||||||
|
|||||||
@@ -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 () => {
|
||||||
|
|||||||
@@ -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')) {
|
||||||
|
|||||||
@@ -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(
|
||||||
|
|||||||
@@ -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',
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -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),
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -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) {
|
||||||
|
|||||||
Reference in New Issue
Block a user