import React, { useState } from 'react'; import { Dialog, DialogTitle, DialogContent, DialogActions, Button, TextField, Box, Typography, Alert, IconButton, InputAdornment, List, ListItem, ListItemIcon, ListItemText, Tooltip, } from '@mui/material'; import { Visibility, VisibilityOff, CheckCircle, Cancel, ContentCopy, } from '@mui/icons-material'; import userService from '../../services/userService'; interface CreateUserDialogProps { open: boolean; onClose: () => void; onSuccess: () => void; } const CreateUserDialog: React.FC = ({ open, onClose, onSuccess, }) => { const [username, setUsername] = useState(''); const [password, setPassword] = useState(''); const [showPassword, setShowPassword] = useState(false); const [loading, setLoading] = useState(false); const [error, setError] = useState(''); const [showSuccess, setShowSuccess] = useState(false); const passwordValidation = userService.validatePassword(password); const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); setError(''); if (!username.trim()) { setError('Username is required'); return; } if (!passwordValidation.valid) { setError('Please fix password validation errors'); return; } setLoading(true); try { await userService.createUser({ accessKey: username, secretKey: password, }); setShowSuccess(true); } catch (err) { setError(err instanceof Error ? err.message : 'Failed to create user'); setLoading(false); } }; const handleCopyCredentials = () => { const credentials = `Username: ${username}\nPassword: ${password}`; navigator.clipboard.writeText(credentials); }; const handleClose = () => { if (!showSuccess) { onClose(); resetForm(); } }; const handleSuccessClose = () => { onSuccess(); onClose(); resetForm(); }; const resetForm = () => { setUsername(''); setPassword(''); setShowPassword(false); setError(''); setShowSuccess(false); setLoading(false); }; const getValidationIcon = (isValid: boolean) => { return isValid ? ( ) : ( ); }; return (
{showSuccess ? 'User Created Successfully' : 'Create New User'} {error && ( {error} )} {!showSuccess ? ( <> setUsername(e.target.value)} required disabled={loading} helperText="This will be the user's Access Key for MinIO" sx={{ mb: 2 }} /> setPassword(e.target.value)} required disabled={loading} helperText="This will be the user's Secret Key for MinIO" InputProps={{ endAdornment: ( setShowPassword(!showPassword)} edge="end" > {showPassword ? : } ), }} /> {password && ( Password Requirements: {getValidationIcon(password.length >= 8)} = 8 ? 'text.primary' : 'text.secondary', }} /> {getValidationIcon(/[A-Z]/.test(password))} {getValidationIcon(/[a-z]/.test(password))} {getValidationIcon(/[0-9]/.test(password))} )} ) : ( User created successfully! Save these MinIO credentials securely - the secret key cannot be retrieved later. MinIO Access Credentials Access Key: {username}
Secret Key: {password}
Use these credentials to connect via MinIO Client: mc alias set myminio http://your-minio-server:9000 {username} {password}
)}
{!showSuccess ? ( <> ) : ( )}
); }; export default CreateUserDialog;