283 lines
10 KiB
Dart
283 lines
10 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:pocketbase/pocketbase.dart';
|
|
import 'package:uae_stat/config/api_config.dart';
|
|
import 'package:uae_stat/presentation/Screens/profilepage.dart';
|
|
|
|
class ConfirmPassword extends StatefulWidget {
|
|
final String email;
|
|
final String userId;
|
|
ConfirmPassword({required this.email, required this.userId});
|
|
|
|
@override
|
|
State<ConfirmPassword> createState() => _ConfirmPasswordState();
|
|
}
|
|
|
|
class _ConfirmPasswordState extends State<ConfirmPassword> {
|
|
final _pb = PocketBase(apiUrl);
|
|
// final pb = PocketBase('http://127.0.0.1:8090');
|
|
final _formKey = GlobalKey<FormState>();
|
|
bool _obscurePassword = true;
|
|
bool _obscureConfirmPassword = true;
|
|
String? _password;
|
|
|
|
String? _validatePassword(String? value) {
|
|
if (value == null || value.isEmpty) {
|
|
return 'Required';
|
|
} else if (value.length < 6) {
|
|
return 'Password must be at least 6 characters';
|
|
}
|
|
_password = value; // Store the password for confirm password validation
|
|
return null;
|
|
}
|
|
|
|
String? _validateConfirmPassword(String? value) {
|
|
if (value == null || value.isEmpty) {
|
|
return 'Required';
|
|
} else if (value != _password) {
|
|
return 'Passwords do not match';
|
|
}
|
|
return null;
|
|
}
|
|
|
|
// Function to check and update password
|
|
Future<void> updatePassword(String userId, String newPassword) async {
|
|
try {
|
|
//print('userRecord- $userId');
|
|
|
|
// Authenticate as admin
|
|
|
|
final adminAuth = await _pb.admins
|
|
.authWithPassword('pb@venbainfotech.com', 'pb@venbainfotech.com');
|
|
final token = adminAuth.token;
|
|
|
|
final headers = {
|
|
'Authorization': 'Bearer $token',
|
|
};
|
|
|
|
final userRecord = await _pb.collection('users').getOne(userId);
|
|
//print('userRecord- $userRecord');
|
|
final oldPassword = userRecord.data['password'];
|
|
print(oldPassword);
|
|
|
|
// Check if the new password is the same as the old password
|
|
if (oldPassword == newPassword) {
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
SnackBar(
|
|
content: Text(
|
|
'New password is similar to the old password. Please try a different one.'),
|
|
backgroundColor: Colors.orange,
|
|
),
|
|
);
|
|
return; // Exit early
|
|
}
|
|
|
|
// Update password
|
|
await _pb.collection('users').update(
|
|
userId, // User ID
|
|
body: {
|
|
'password': newPassword,
|
|
'passwordConfirm': newPassword,
|
|
}, // Updated password
|
|
headers: headers,
|
|
);
|
|
|
|
// Show success Snackbar
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
SnackBar(
|
|
content: Text('Password updated successfully'),
|
|
backgroundColor: Colors.blue,
|
|
),
|
|
);
|
|
|
|
Navigator.push(
|
|
context,
|
|
MaterialPageRoute(builder: (context) => ProfileScreen(userId: userId)),
|
|
);
|
|
} catch (e) {
|
|
print('Error updating password: $e');
|
|
// Show error Snackbar
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
SnackBar(
|
|
content: Text('Failed to update password'),
|
|
backgroundColor: Colors.red,
|
|
),
|
|
);
|
|
}
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
double screenheight = MediaQuery.of(context).size.height;
|
|
double screenwidth = MediaQuery.of(context).size.width;
|
|
return Scaffold(
|
|
backgroundColor: Colors.white,
|
|
body: SingleChildScrollView(
|
|
child: SafeArea(
|
|
child: Form(
|
|
key: _formKey,
|
|
child: Column(
|
|
mainAxisAlignment: MainAxisAlignment.spaceAround,
|
|
children: [
|
|
Padding(
|
|
padding: const EdgeInsets.all(24.0),
|
|
child: Column(
|
|
children: [
|
|
SizedBox(
|
|
height: screenheight / 6,
|
|
),
|
|
Text(
|
|
"Create New Password",
|
|
style: TextStyle(
|
|
fontSize: 24,
|
|
fontWeight: FontWeight.bold,
|
|
),
|
|
),
|
|
SizedBox(
|
|
height: screenheight / 35,
|
|
),
|
|
Text(
|
|
"Your new password must de different\nform previously used password",
|
|
textAlign: TextAlign.center,
|
|
style: TextStyle(
|
|
fontSize: 16,
|
|
color: Colors.grey[600],
|
|
),
|
|
),
|
|
SizedBox(
|
|
height: screenheight / 35,
|
|
),
|
|
TextFormField(
|
|
obscureText: _obscurePassword,
|
|
decoration: InputDecoration(
|
|
hintText: 'Enter your password',
|
|
hintStyle: const TextStyle(
|
|
color: Color(0xFFC3C6CB),
|
|
fontSize: 14,
|
|
),
|
|
prefixIcon: Icon(
|
|
Icons.lock,
|
|
color: Colors.blue,
|
|
),
|
|
suffixIcon: IconButton(
|
|
icon: Icon(
|
|
_obscurePassword
|
|
? Icons.visibility_off
|
|
: Icons.visibility,
|
|
color: Colors.blue,
|
|
),
|
|
onPressed: () {
|
|
setState(() {
|
|
_obscurePassword = !_obscurePassword;
|
|
});
|
|
},
|
|
),
|
|
border: OutlineInputBorder(),
|
|
),
|
|
validator: _validatePassword,
|
|
),
|
|
SizedBox(
|
|
height: screenheight / 35,
|
|
),
|
|
TextFormField(
|
|
obscureText: _obscureConfirmPassword,
|
|
decoration: InputDecoration(
|
|
hintText: 'Confirm password',
|
|
hintStyle: const TextStyle(
|
|
color: Color(0xFFC3C6CB),
|
|
fontSize: 14,
|
|
),
|
|
prefixIcon: Icon(
|
|
Icons.lock,
|
|
color: Colors.blue,
|
|
),
|
|
suffixIcon: IconButton(
|
|
icon: Icon(
|
|
_obscureConfirmPassword
|
|
? Icons.visibility_off
|
|
: Icons.visibility,
|
|
color: Colors.blue,
|
|
),
|
|
onPressed: () {
|
|
setState(() {
|
|
_obscureConfirmPassword =
|
|
!_obscureConfirmPassword;
|
|
});
|
|
},
|
|
),
|
|
border: OutlineInputBorder(),
|
|
),
|
|
validator: _validateConfirmPassword,
|
|
),
|
|
SizedBox(
|
|
height: screenheight / 35,
|
|
),
|
|
SizedBox(
|
|
width: screenwidth / 1.1,
|
|
child: ElevatedButton(
|
|
onPressed: () async {
|
|
if ((_formKey.currentState?.validate() ?? false)) {
|
|
//String userId = '4hai9cbn4lg6jt4'; // Replace with the actual user ID
|
|
String newPassword = _password ??
|
|
''; // Replace with the new password
|
|
|
|
//print("Confirm Passwd- $widget.userId, $newPassword");
|
|
|
|
await updatePassword(widget.userId, newPassword);
|
|
}
|
|
// Add verification logic here
|
|
},
|
|
style: ElevatedButton.styleFrom(
|
|
backgroundColor: Colors.brown[300],
|
|
padding: EdgeInsets.symmetric(
|
|
horizontal: 80, vertical: 16),
|
|
shape: RoundedRectangleBorder(
|
|
borderRadius: BorderRadius.circular(10),
|
|
),
|
|
),
|
|
child: Row(
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
children: [
|
|
Text(
|
|
"Save",
|
|
style: TextStyle(
|
|
fontSize: 18, color: Colors.white),
|
|
),
|
|
SizedBox(
|
|
width: 2,
|
|
),
|
|
Icon(
|
|
Icons.arrow_forward_ios,
|
|
color: Colors.white38,
|
|
size: 18,
|
|
),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
SizedBox(
|
|
height: screenheight / 15,
|
|
),
|
|
Center(
|
|
child: Container(
|
|
height: screenheight / 10,
|
|
width: screenwidth / 2.5,
|
|
decoration: BoxDecoration(
|
|
image: DecorationImage(
|
|
image: AssetImage(
|
|
"assets/splash_screen/logo.png"), // Background image asset
|
|
fit: BoxFit.fill,
|
|
),
|
|
),
|
|
))
|
|
],
|
|
),
|
|
),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|