import React, { useState, useEffect } from 'react'; import { toast } from 'sonner'; import { api } from '@/api'; import { Button } from '@/components/ui/button'; import { Badge } from '@/components/ui/badge'; import { Label } from '@/components/ui/label'; import { Card, CardHeader, CardTitle, CardContent } from '@/components/ui/card'; import { Select, SelectTrigger, SelectValue, SelectContent, SelectItem, } from '@/components/ui/select'; import { AlertDialog, AlertDialogContent, AlertDialogHeader, AlertDialogTitle, AlertDialogDescription, AlertDialogFooter, AlertDialogCancel, AlertDialogAction, } from '@/components/ui/alert-dialog'; export default function LoginModeCard({ users }) { const [modeData, setModeData] = useState(null); const [loading, setLoading] = useState(true); const [saving, setSaving] = useState(false); const [selectedUser, setSelectedUser] = useState(''); const [confirmSingle, setConfirmSingle] = useState(false); const [pendingUserId, setPendingUserId] = useState(null); useEffect(() => { api.authModeConfig() .then(d => { setModeData(d); setSelectedUser(d.default_user_id?.toString() || ''); }) .catch(() => {}) .finally(() => setLoading(false)); }, []); const doSetMode = async (mode, userId) => { setSaving(true); try { await api.setAuthMode({ auth_mode: mode, default_user_id: mode === 'single' ? parseInt(userId, 10) : null, }); const d = await api.authModeConfig(); setModeData(d); toast.success(mode === 'single' ? 'Single-user mode enabled.' : 'Login requirement restored.'); } catch (err) { toast.error(err.message || 'Failed to update auth mode.'); } finally { setSaving(false); } }; const handleRequestSingle = () => { if (!selectedUser) { toast.error('Select a user first.'); return; } setPendingUserId(selectedUser); setConfirmSingle(true); }; const handleConfirmSingle = () => { setConfirmSingle(false); doSetMode('single', pendingUserId); }; if (loading) return Loading…; const isMulti = !modeData || modeData.auth_mode === 'multi'; const activeUser = users?.find(u => u.id === modeData?.default_user_id); const selectedUsername = users?.find(u => u.id.toString() === selectedUser)?.username ?? selectedUser; return ( <>
Login Mode {isMulti ? 'Multi-user' : 'Single-user'}
{isMulti ? ( <>

Single-user mode bypasses the login screen and automatically signs in as the selected user.

) : ( <>

Currently auto-signing in as{' '} {activeUser?.username ?? '—'}. Restoring login requirement will require all users to sign in manually.

)}
Enable Single-User Mode? Anyone who opens the app will be automatically signed in as{' '} {selectedUsername}. The admin login still requires a password. Cancel Enable Single-User Mode ); }