feat: creating users from the front end for admin users
This commit is contained in:
parent
e4ab34b97f
commit
100c51c865
@ -11,7 +11,7 @@ public class CreateUserRequest {
|
|||||||
private String email;
|
private String email;
|
||||||
|
|
||||||
@NotBlank
|
@NotBlank
|
||||||
@Size(min = 8)
|
@Size(min = 6)
|
||||||
private String password;
|
private String password;
|
||||||
|
|
||||||
@NotBlank
|
@NotBlank
|
||||||
|
|||||||
@ -10,7 +10,7 @@ public class UpdateUserRequest {
|
|||||||
|
|
||||||
private String avatar;
|
private String avatar;
|
||||||
|
|
||||||
@Size(min = 8, message = "Password must be at least 8 characters long")
|
@Size(min = 6, message = "Password must be at least 8 characters long")
|
||||||
private String password;
|
private String password;
|
||||||
|
|
||||||
public UpdateUserRequest() {}
|
public UpdateUserRequest() {}
|
||||||
|
|||||||
@ -23,7 +23,7 @@ public class User {
|
|||||||
|
|
||||||
@Column(nullable = false)
|
@Column(nullable = false)
|
||||||
@NotBlank
|
@NotBlank
|
||||||
@Size(min = 8)
|
@Size(min = 6)
|
||||||
private String password;
|
private String password;
|
||||||
|
|
||||||
@Column(nullable = false)
|
@Column(nullable = false)
|
||||||
|
|||||||
@ -6,6 +6,7 @@ import '../../services/auth_service.dart';
|
|||||||
import '../../services/post_service.dart';
|
import '../../services/post_service.dart';
|
||||||
import '../../models/post_models.dart';
|
import '../../models/post_models.dart';
|
||||||
import '../../widgets/posts_list.dart';
|
import '../../widgets/posts_list.dart';
|
||||||
|
import '../../utils/password_generator.dart';
|
||||||
|
|
||||||
class ProfilePage extends StatefulWidget {
|
class ProfilePage extends StatefulWidget {
|
||||||
@override
|
@override
|
||||||
@ -288,15 +289,28 @@ class _SettingsPageState extends State<SettingsPage> {
|
|||||||
final TextEditingController _tokenController = TextEditingController();
|
final TextEditingController _tokenController = TextEditingController();
|
||||||
bool isLoading = false;
|
bool isLoading = false;
|
||||||
|
|
||||||
|
// Admin user creation
|
||||||
|
Map<String, dynamic>? userData;
|
||||||
|
bool isLoadingUser = true;
|
||||||
|
bool isCreatingUser = false;
|
||||||
|
final TextEditingController _emailController = TextEditingController();
|
||||||
|
final TextEditingController _passwordController = TextEditingController();
|
||||||
|
final TextEditingController _displayNameController = TextEditingController();
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void initState() {
|
void initState() {
|
||||||
super.initState();
|
super.initState();
|
||||||
_loadFCMToken();
|
_loadFCMToken();
|
||||||
|
_loadUserData();
|
||||||
|
_generatePassword();
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void dispose() {
|
void dispose() {
|
||||||
_tokenController.dispose();
|
_tokenController.dispose();
|
||||||
|
_emailController.dispose();
|
||||||
|
_passwordController.dispose();
|
||||||
|
_displayNameController.dispose();
|
||||||
super.dispose();
|
super.dispose();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -333,6 +347,68 @@ class _SettingsPageState extends State<SettingsPage> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Future<void> _loadUserData() async {
|
||||||
|
setState(() {
|
||||||
|
isLoadingUser = true;
|
||||||
|
});
|
||||||
|
|
||||||
|
final result = await UserService.getCurrentUser();
|
||||||
|
|
||||||
|
setState(() {
|
||||||
|
isLoadingUser = false;
|
||||||
|
if (result['success'] == true) {
|
||||||
|
userData = result['data'];
|
||||||
|
} else {
|
||||||
|
userData = null;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
void _generatePassword() {
|
||||||
|
_passwordController.text = PasswordGenerator.generateReadablePassword();
|
||||||
|
}
|
||||||
|
|
||||||
|
bool get _isAdmin {
|
||||||
|
return userData?['role'] == 'ADMIN';
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<Map<String, dynamic>> _createUserAccount() async {
|
||||||
|
if (_emailController.text.trim().isEmpty ||
|
||||||
|
_displayNameController.text.trim().isEmpty) {
|
||||||
|
ScaffoldMessenger.of(context).showSnackBar(
|
||||||
|
SnackBar(
|
||||||
|
content: Text('Please fill in all fields'),
|
||||||
|
backgroundColor: Colors.red,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
return {'success': false, 'message': 'Please fill in all fields'};
|
||||||
|
}
|
||||||
|
|
||||||
|
setState(() {
|
||||||
|
isCreatingUser = true;
|
||||||
|
});
|
||||||
|
|
||||||
|
try {
|
||||||
|
final result = await UserService.createUser(
|
||||||
|
email: _emailController.text.trim(),
|
||||||
|
password: _passwordController.text,
|
||||||
|
displayName: _displayNameController.text.trim(),
|
||||||
|
);
|
||||||
|
|
||||||
|
setState(() {
|
||||||
|
isCreatingUser = false;
|
||||||
|
});
|
||||||
|
|
||||||
|
return result;
|
||||||
|
} catch (e) {
|
||||||
|
setState(() {
|
||||||
|
isCreatingUser = false;
|
||||||
|
});
|
||||||
|
|
||||||
|
return {'success': false, 'message': 'Error creating user: $e'};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void _signOut() async {
|
void _signOut() async {
|
||||||
showDialog(
|
showDialog(
|
||||||
context: context,
|
context: context,
|
||||||
@ -363,6 +439,193 @@ class _SettingsPageState extends State<SettingsPage> {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void _showCredentialsDialog(String email, String password) {
|
||||||
|
final credentialsText = 'Email: $email\nPassword: $password';
|
||||||
|
final TextEditingController credentialsController = TextEditingController(
|
||||||
|
text: credentialsText,
|
||||||
|
);
|
||||||
|
|
||||||
|
showDialog(
|
||||||
|
context: context,
|
||||||
|
builder: (BuildContext dialogContext) {
|
||||||
|
return AlertDialog(
|
||||||
|
title: Text('Account Created Successfully!'),
|
||||||
|
content: Column(
|
||||||
|
mainAxisSize: MainAxisSize.min,
|
||||||
|
crossAxisAlignment: CrossAxisAlignment.start,
|
||||||
|
children: [
|
||||||
|
Text(
|
||||||
|
'Share these credentials with the user:',
|
||||||
|
style: TextStyle(fontSize: 16, fontWeight: FontWeight.w500),
|
||||||
|
),
|
||||||
|
SizedBox(height: 16),
|
||||||
|
|
||||||
|
TextField(
|
||||||
|
controller: credentialsController,
|
||||||
|
decoration: InputDecoration(
|
||||||
|
border: OutlineInputBorder(),
|
||||||
|
suffixIcon: IconButton(
|
||||||
|
icon: Icon(Icons.copy),
|
||||||
|
onPressed: () async {
|
||||||
|
await Clipboard.setData(
|
||||||
|
ClipboardData(text: credentialsText),
|
||||||
|
);
|
||||||
|
ScaffoldMessenger.of(context).showSnackBar(
|
||||||
|
SnackBar(
|
||||||
|
content: Text('Credentials copied to clipboard'),
|
||||||
|
backgroundColor: Color(0xFF6A4C93),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
},
|
||||||
|
tooltip: 'Copy credentials',
|
||||||
|
),
|
||||||
|
),
|
||||||
|
maxLines: 2,
|
||||||
|
readOnly: true,
|
||||||
|
style: TextStyle(fontFamily: 'monospace', fontSize: 14),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
actions: [
|
||||||
|
ElevatedButton(
|
||||||
|
onPressed: () => Navigator.of(dialogContext).pop(),
|
||||||
|
style: ElevatedButton.styleFrom(
|
||||||
|
backgroundColor: Color(0xFF6A4C93),
|
||||||
|
foregroundColor: Colors.white,
|
||||||
|
),
|
||||||
|
child: Text('Done'),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
void _showCreateAccountDialog(BuildContext context) {
|
||||||
|
showDialog(
|
||||||
|
context: context,
|
||||||
|
builder: (BuildContext dialogContext) {
|
||||||
|
return StatefulBuilder(
|
||||||
|
builder: (context, setDialogState) {
|
||||||
|
return AlertDialog(
|
||||||
|
title: Text('Create Account'),
|
||||||
|
content: SingleChildScrollView(
|
||||||
|
child: Column(
|
||||||
|
mainAxisSize: MainAxisSize.min,
|
||||||
|
children: [
|
||||||
|
TextField(
|
||||||
|
controller: _emailController,
|
||||||
|
decoration: InputDecoration(
|
||||||
|
labelText: 'Email',
|
||||||
|
border: OutlineInputBorder(),
|
||||||
|
prefixIcon: Icon(Icons.email),
|
||||||
|
),
|
||||||
|
keyboardType: TextInputType.emailAddress,
|
||||||
|
),
|
||||||
|
SizedBox(height: 16),
|
||||||
|
|
||||||
|
TextField(
|
||||||
|
controller: _displayNameController,
|
||||||
|
decoration: InputDecoration(
|
||||||
|
labelText: 'Display Name',
|
||||||
|
border: OutlineInputBorder(),
|
||||||
|
prefixIcon: Icon(Icons.person),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
SizedBox(height: 16),
|
||||||
|
|
||||||
|
TextField(
|
||||||
|
controller: _passwordController,
|
||||||
|
decoration: InputDecoration(
|
||||||
|
labelText: 'Password',
|
||||||
|
border: OutlineInputBorder(),
|
||||||
|
prefixIcon: Icon(Icons.lock),
|
||||||
|
suffixIcon: IconButton(
|
||||||
|
icon: Icon(Icons.refresh),
|
||||||
|
onPressed: () {
|
||||||
|
setDialogState(() {
|
||||||
|
_generatePassword();
|
||||||
|
});
|
||||||
|
},
|
||||||
|
tooltip: 'Generate new password',
|
||||||
|
),
|
||||||
|
),
|
||||||
|
readOnly: true,
|
||||||
|
style: TextStyle(
|
||||||
|
fontFamily: 'monospace',
|
||||||
|
fontWeight: FontWeight.bold,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
SizedBox(height: 8),
|
||||||
|
|
||||||
|
Text(
|
||||||
|
'Password is auto-generated for security',
|
||||||
|
style: TextStyle(fontSize: 12, color: Colors.grey[600]),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
),
|
||||||
|
actions: [
|
||||||
|
TextButton(
|
||||||
|
onPressed: () => Navigator.of(dialogContext).pop(),
|
||||||
|
child: Text('Cancel'),
|
||||||
|
),
|
||||||
|
ElevatedButton(
|
||||||
|
onPressed: isCreatingUser
|
||||||
|
? null
|
||||||
|
: () async {
|
||||||
|
final email = _emailController.text.trim();
|
||||||
|
final password = _passwordController.text;
|
||||||
|
|
||||||
|
final result = await _createUserAccount();
|
||||||
|
|
||||||
|
if (mounted) {
|
||||||
|
Navigator.of(dialogContext).pop();
|
||||||
|
|
||||||
|
if (result['success']) {
|
||||||
|
_showCredentialsDialog(email, password);
|
||||||
|
// Clear form
|
||||||
|
_emailController.clear();
|
||||||
|
_displayNameController.clear();
|
||||||
|
_generatePassword();
|
||||||
|
} else {
|
||||||
|
ScaffoldMessenger.of(context).showSnackBar(
|
||||||
|
SnackBar(
|
||||||
|
content: Text(
|
||||||
|
result['message'] ??
|
||||||
|
'Failed to create user',
|
||||||
|
),
|
||||||
|
backgroundColor: Colors.red,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
style: ElevatedButton.styleFrom(
|
||||||
|
backgroundColor: Color(0xFF6A4C93),
|
||||||
|
foregroundColor: Colors.white,
|
||||||
|
),
|
||||||
|
child: isCreatingUser
|
||||||
|
? SizedBox(
|
||||||
|
width: 16,
|
||||||
|
height: 16,
|
||||||
|
child: CircularProgressIndicator(
|
||||||
|
strokeWidth: 2,
|
||||||
|
valueColor: AlwaysStoppedAnimation<Color>(
|
||||||
|
Colors.white,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
: Text('Create'),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
return Scaffold(
|
return Scaffold(
|
||||||
@ -382,6 +645,37 @@ class _SettingsPageState extends State<SettingsPage> {
|
|||||||
child: Column(
|
child: Column(
|
||||||
crossAxisAlignment: CrossAxisAlignment.start,
|
crossAxisAlignment: CrossAxisAlignment.start,
|
||||||
children: [
|
children: [
|
||||||
|
if (!isLoadingUser && _isAdmin) ...[
|
||||||
|
Text(
|
||||||
|
'Admin Tools',
|
||||||
|
style: TextStyle(
|
||||||
|
fontSize: 20,
|
||||||
|
fontWeight: FontWeight.w600,
|
||||||
|
color: Color(0xFF6A4C93),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
SizedBox(height: 16),
|
||||||
|
|
||||||
|
Container(
|
||||||
|
width: double.infinity,
|
||||||
|
child: ElevatedButton.icon(
|
||||||
|
onPressed: () => _showCreateAccountDialog(context),
|
||||||
|
icon: Icon(Icons.person_add, size: 18),
|
||||||
|
label: Text('Create an Account'),
|
||||||
|
style: ElevatedButton.styleFrom(
|
||||||
|
backgroundColor: Color(0xFF6A4C93),
|
||||||
|
foregroundColor: Colors.white,
|
||||||
|
shape: RoundedRectangleBorder(
|
||||||
|
borderRadius: BorderRadius.circular(8),
|
||||||
|
),
|
||||||
|
padding: EdgeInsets.symmetric(vertical: 12, horizontal: 16),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
|
||||||
|
SizedBox(height: 32),
|
||||||
|
],
|
||||||
|
|
||||||
Text(
|
Text(
|
||||||
'Development Tools',
|
'Development Tools',
|
||||||
style: TextStyle(
|
style: TextStyle(
|
||||||
|
|||||||
@ -78,4 +78,39 @@ class UserService {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static Future<Map<String, dynamic>> createUser({
|
||||||
|
required String email,
|
||||||
|
required String password,
|
||||||
|
required String displayName,
|
||||||
|
}) async {
|
||||||
|
try {
|
||||||
|
final response = await HttpService.post('/admin/createUser', {
|
||||||
|
'email': email,
|
||||||
|
'password': password,
|
||||||
|
'displayName': displayName,
|
||||||
|
});
|
||||||
|
|
||||||
|
final responseData = jsonDecode(response.body);
|
||||||
|
|
||||||
|
if (response.statusCode == 200 || response.statusCode == 201) {
|
||||||
|
return {
|
||||||
|
'success': responseData['status'] ?? false,
|
||||||
|
'message': responseData['message'] ?? 'User created successfully',
|
||||||
|
'data': responseData['data'],
|
||||||
|
};
|
||||||
|
} else {
|
||||||
|
return {
|
||||||
|
'success': false,
|
||||||
|
'message': responseData['message'] ?? 'Failed to create user',
|
||||||
|
};
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
print('Error creating user: $e');
|
||||||
|
return {
|
||||||
|
'success': false,
|
||||||
|
'message': 'Network error. Please check your connection.',
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
16
frontend/lib/utils/password_generator.dart
Normal file
16
frontend/lib/utils/password_generator.dart
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
import 'dart:math';
|
||||||
|
|
||||||
|
class PasswordGenerator {
|
||||||
|
static const String _readableChars = 'ABCDEFGHJKLMNPQRSTUVWXYZ23456789';
|
||||||
|
static final Random _random = Random();
|
||||||
|
|
||||||
|
static String generateReadablePassword({int length = 6}) {
|
||||||
|
StringBuffer password = StringBuffer();
|
||||||
|
|
||||||
|
for (int i = 0; i < length; i++) {
|
||||||
|
password.write(_readableChars[_random.nextInt(_readableChars.length)]);
|
||||||
|
}
|
||||||
|
|
||||||
|
return password.toString();
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user