Fix jwt-token

This commit is contained in:
2025-09-13 13:05:28 +02:00
parent 96ab92a20d
commit 04b103fe32
5 changed files with 1118 additions and 1 deletions

View File

@@ -0,0 +1,353 @@
import React, { useState, useEffect } from 'react'
import { XMarkIcon, EyeIcon, EyeSlashIcon } from '@heroicons/react/24/outline'
const UserModal = ({ isOpen, onClose, onSave, user, tenant, title = "Create User" }) => {
const [formData, setFormData] = useState({
username: '',
email: '',
first_name: '',
last_name: '',
password: '',
confirmPassword: '',
role: 'viewer',
phone: '',
is_active: true
})
const [loading, setLoading] = useState(false)
const [errors, setErrors] = useState({})
const [showPassword, setShowPassword] = useState(false)
const [showConfirmPassword, setShowConfirmPassword] = useState(false)
useEffect(() => {
if (user) {
// Editing existing user
setFormData({
username: user.username || '',
email: user.email || '',
first_name: user.first_name || '',
last_name: user.last_name || '',
password: '', // Never pre-fill passwords
confirmPassword: '',
role: user.role || 'viewer',
phone: user.phone || '',
is_active: user.is_active !== false
})
} else {
// Creating new user
setFormData({
username: '',
email: '',
first_name: '',
last_name: '',
password: '',
confirmPassword: '',
role: 'viewer',
phone: '',
is_active: true
})
}
setErrors({})
}, [user, isOpen])
const validateForm = () => {
const newErrors = {}
if (!formData.username.trim()) {
newErrors.username = 'Username is required'
}
if (!formData.email.trim()) {
newErrors.email = 'Email is required'
} else if (!/\S+@\S+\.\S+/.test(formData.email)) {
newErrors.email = 'Email is invalid'
}
if (!user) { // Only require password for new users
if (!formData.password) {
newErrors.password = 'Password is required'
} else if (formData.password.length < 6) {
newErrors.password = 'Password must be at least 6 characters'
}
if (formData.password !== formData.confirmPassword) {
newErrors.confirmPassword = 'Passwords do not match'
}
} else if (formData.password) { // If editing and password provided
if (formData.password.length < 6) {
newErrors.password = 'Password must be at least 6 characters'
}
if (formData.password !== formData.confirmPassword) {
newErrors.confirmPassword = 'Passwords do not match'
}
}
setErrors(newErrors)
return Object.keys(newErrors).length === 0
}
const handleSubmit = async (e) => {
e.preventDefault()
if (!validateForm()) return
setLoading(true)
try {
// Prepare data for submission
const submitData = { ...formData }
delete submitData.confirmPassword
// Don't send empty password for updates
if (user && !submitData.password) {
delete submitData.password
}
await onSave(submitData)
onClose()
} catch (error) {
console.error('Error saving user:', error)
setErrors({ general: error.message || 'Failed to save user' })
} finally {
setLoading(false)
}
}
const handleInputChange = (e) => {
const { name, value, type, checked } = e.target
setFormData(prev => ({
...prev,
[name]: type === 'checkbox' ? checked : value
}))
// Clear error when user starts typing
if (errors[name]) {
setErrors(prev => ({ ...prev, [name]: '' }))
}
}
if (!isOpen) return null
return (
<div className="fixed inset-0 bg-gray-600 bg-opacity-50 overflow-y-auto h-full w-full z-50">
<div className="relative top-20 mx-auto p-5 border w-full max-w-2xl shadow-lg rounded-md bg-white">
<div className="flex items-center justify-between mb-4">
<h3 className="text-lg font-medium text-gray-900">
{title} {tenant && `in ${tenant.name}`}
</h3>
<button
onClick={onClose}
className="text-gray-400 hover:text-gray-600"
>
<XMarkIcon className="w-6 h-6" />
</button>
</div>
{errors.general && (
<div className="mb-4 p-3 bg-red-100 border border-red-400 text-red-700 rounded">
{errors.general}
</div>
)}
<form onSubmit={handleSubmit} className="space-y-6">
{/* Basic Information */}
<div className="grid grid-cols-2 gap-4">
<div>
<label className="block text-sm font-medium text-gray-700 mb-1">
Username *
</label>
<input
type="text"
name="username"
value={formData.username}
onChange={handleInputChange}
disabled={!!user} // Can't change username for existing users
className="w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-blue-500 focus:border-blue-500 disabled:bg-gray-100"
placeholder="Enter username"
/>
{errors.username && (
<p className="mt-1 text-sm text-red-600">{errors.username}</p>
)}
</div>
<div>
<label className="block text-sm font-medium text-gray-700 mb-1">
Email *
</label>
<input
type="email"
name="email"
value={formData.email}
onChange={handleInputChange}
className="w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-blue-500 focus:border-blue-500"
placeholder="Enter email"
/>
{errors.email && (
<p className="mt-1 text-sm text-red-600">{errors.email}</p>
)}
</div>
</div>
<div className="grid grid-cols-2 gap-4">
<div>
<label className="block text-sm font-medium text-gray-700 mb-1">
First Name
</label>
<input
type="text"
name="first_name"
value={formData.first_name}
onChange={handleInputChange}
className="w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-blue-500 focus:border-blue-500"
placeholder="Enter first name"
/>
</div>
<div>
<label className="block text-sm font-medium text-gray-700 mb-1">
Last Name
</label>
<input
type="text"
name="last_name"
value={formData.last_name}
onChange={handleInputChange}
className="w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-blue-500 focus:border-blue-500"
placeholder="Enter last name"
/>
</div>
</div>
{/* Password Section */}
<div className="grid grid-cols-2 gap-4">
<div>
<label className="block text-sm font-medium text-gray-700 mb-1">
Password {!user && '*'}
{user && <span className="text-gray-500 text-xs">(leave blank to keep current)</span>}
</label>
<div className="relative">
<input
type={showPassword ? "text" : "password"}
name="password"
value={formData.password}
onChange={handleInputChange}
className="w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-blue-500 focus:border-blue-500 pr-10"
placeholder={user ? "Enter new password" : "Enter password"}
/>
<button
type="button"
className="absolute inset-y-0 right-0 pr-3 flex items-center"
onClick={() => setShowPassword(!showPassword)}
>
{showPassword ? (
<EyeSlashIcon className="h-4 w-4 text-gray-400" />
) : (
<EyeIcon className="h-4 w-4 text-gray-400" />
)}
</button>
</div>
{errors.password && (
<p className="mt-1 text-sm text-red-600">{errors.password}</p>
)}
</div>
<div>
<label className="block text-sm font-medium text-gray-700 mb-1">
Confirm Password {!user && '*'}
</label>
<div className="relative">
<input
type={showConfirmPassword ? "text" : "password"}
name="confirmPassword"
value={formData.confirmPassword}
onChange={handleInputChange}
className="w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-blue-500 focus:border-blue-500 pr-10"
placeholder="Confirm password"
/>
<button
type="button"
className="absolute inset-y-0 right-0 pr-3 flex items-center"
onClick={() => setShowConfirmPassword(!showConfirmPassword)}
>
{showConfirmPassword ? (
<EyeSlashIcon className="h-4 w-4 text-gray-400" />
) : (
<EyeIcon className="h-4 w-4 text-gray-400" />
)}
</button>
</div>
{errors.confirmPassword && (
<p className="mt-1 text-sm text-red-600">{errors.confirmPassword}</p>
)}
</div>
</div>
{/* Role and Settings */}
<div className="grid grid-cols-2 gap-4">
<div>
<label className="block text-sm font-medium text-gray-700 mb-1">
Role
</label>
<select
name="role"
value={formData.role}
onChange={handleInputChange}
className="w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-blue-500 focus:border-blue-500"
>
<option value="viewer">Viewer</option>
<option value="operator">Operator</option>
<option value="admin">Admin</option>
</select>
</div>
<div>
<label className="block text-sm font-medium text-gray-700 mb-1">
Phone
</label>
<input
type="tel"
name="phone"
value={formData.phone}
onChange={handleInputChange}
className="w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-blue-500 focus:border-blue-500"
placeholder="Enter phone number"
/>
</div>
</div>
{/* Active Status */}
<div className="flex items-center">
<input
type="checkbox"
name="is_active"
checked={formData.is_active}
onChange={handleInputChange}
className="h-4 w-4 text-blue-600 focus:ring-blue-500 border-gray-300 rounded"
/>
<label className="ml-2 block text-sm text-gray-900">
Active User
</label>
</div>
{/* Form Actions */}
<div className="flex justify-end space-x-3 pt-6">
<button
type="button"
onClick={onClose}
className="px-4 py-2 border border-gray-300 rounded-md shadow-sm text-sm font-medium text-gray-700 bg-white hover:bg-gray-50 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-blue-500"
>
Cancel
</button>
<button
type="submit"
disabled={loading}
className="px-4 py-2 border border-transparent rounded-md shadow-sm text-sm font-medium text-white bg-blue-600 hover:bg-blue-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-blue-500 disabled:opacity-50"
>
{loading ? 'Saving...' : (user ? 'Update User' : 'Create User')}
</button>
</div>
</form>
</div>
</div>
)
}
export default UserModal