change password functionality added
This commit is contained in:
parent
e54998ac5f
commit
3beae20180
@ -1,5 +1,7 @@
|
|||||||
|
import 'dart:convert';
|
||||||
|
import 'package:http/http.dart' as http;
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:pocketbase/pocketbase.dart';
|
||||||
import 'otp_verification.dart';
|
import 'otp_verification.dart';
|
||||||
|
|
||||||
class Changepassword extends StatefulWidget {
|
class Changepassword extends StatefulWidget {
|
||||||
@ -10,22 +12,118 @@ class Changepassword extends StatefulWidget {
|
|||||||
}
|
}
|
||||||
|
|
||||||
class _ResetPasswordScreenState extends State<Changepassword> {
|
class _ResetPasswordScreenState extends State<Changepassword> {
|
||||||
|
final _pb = PocketBase('https://pb.venbait.in');
|
||||||
|
// final pb = PocketBase('http://127.0.0.1:8090');
|
||||||
final _formKey = GlobalKey<FormState>();
|
final _formKey = GlobalKey<FormState>();
|
||||||
final _emailController = TextEditingController();
|
final _emailController = TextEditingController();
|
||||||
bool _isLoading = false;
|
bool _isLoading = false;
|
||||||
|
String? _otpId;
|
||||||
|
|
||||||
void _handleSubmit() {
|
String? _verificationCode;
|
||||||
|
Future<void> sendVerificationCode(String email) async {
|
||||||
if (_formKey.currentState!.validate()) {
|
if (_formKey.currentState!.validate()) {
|
||||||
setState(() {
|
setState(() {
|
||||||
_isLoading = true;
|
_isLoading = true;
|
||||||
});
|
});
|
||||||
Navigator.push(
|
|
||||||
context,
|
|
||||||
MaterialPageRoute(builder: (context) => EmailVerificationScreen()),
|
|
||||||
);
|
|
||||||
|
|
||||||
// TODO: Implement your password reset logic here
|
final email = _emailController.text.trim();
|
||||||
// After API call, set _isLoading back to false
|
|
||||||
|
//print('Email - $email');
|
||||||
|
|
||||||
|
try {
|
||||||
|
// Authenticate admin to get the token
|
||||||
|
final adminAuth = await _pb.admins
|
||||||
|
.authWithPassword('pb@venbainfotech.com', 'pb@venbainfotech.com');
|
||||||
|
final authToken = adminAuth.token;
|
||||||
|
|
||||||
|
//print('Admin Token: $authToken');
|
||||||
|
|
||||||
|
// Check if email exists in the user collection
|
||||||
|
final userCheckResponse = await http.get(
|
||||||
|
Uri.parse(
|
||||||
|
'https://pb.venbait.in/api/collections/users/records?filter=email="$email"'),
|
||||||
|
headers: {
|
||||||
|
'Authorization': 'Bearer $authToken',
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
if (userCheckResponse.statusCode == 200) {
|
||||||
|
final userData = jsonDecode(userCheckResponse.body);
|
||||||
|
|
||||||
|
//print('userDetails ->: $userData');
|
||||||
|
if (userData['items'].isNotEmpty) {
|
||||||
|
// Email exists; retrieve user ID
|
||||||
|
final userId = userData['items'][0]['id'];
|
||||||
|
|
||||||
|
// Proceed with OTP request
|
||||||
|
final otpResponse = await http.post(
|
||||||
|
Uri.parse(
|
||||||
|
'https://pb.venbait.in/api/collections/otp_requests/records'),
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
'Authorization': 'Bearer $authToken',
|
||||||
|
},
|
||||||
|
body: jsonEncode({
|
||||||
|
'email': email,
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
|
||||||
|
if (otpResponse.statusCode == 200) {
|
||||||
|
//print('otpResponse- ${otpResponse.body} ');
|
||||||
|
|
||||||
|
final otpData = jsonDecode(otpResponse.body);
|
||||||
|
final otp = otpData['otp']; // OTP value from the response
|
||||||
|
final otpId = otpData['id'];
|
||||||
|
_otpId = otpData['id'];
|
||||||
|
//print('OTP: $otp, ID: $otpId, ');
|
||||||
|
|
||||||
|
ScaffoldMessenger.of(context).showSnackBar(
|
||||||
|
SnackBar(content: Text("Verification code sent to $email")),
|
||||||
|
);
|
||||||
|
|
||||||
|
// Navigate to the Email Verification screen
|
||||||
|
Navigator.push(
|
||||||
|
context,
|
||||||
|
MaterialPageRoute(
|
||||||
|
builder: (context) => EmailVerificationScreen(
|
||||||
|
email: email,
|
||||||
|
userId: userId, // Pass user ID to the next screen
|
||||||
|
otp: otp,
|
||||||
|
otpId: otpId,
|
||||||
|
sendVerificationCode: sendVerificationCode,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
final error =
|
||||||
|
jsonDecode(otpResponse.body)['error'] ?? "Unknown error";
|
||||||
|
ScaffoldMessenger.of(context).showSnackBar(
|
||||||
|
SnackBar(content: Text("Error: $error")),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// Email not found
|
||||||
|
ScaffoldMessenger.of(context).showSnackBar(
|
||||||
|
SnackBar(content: Text("Please Enter Registered Mail ID")),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// Error in user collection request
|
||||||
|
final error =
|
||||||
|
jsonDecode(userCheckResponse.body)['error'] ?? "Unknown error";
|
||||||
|
ScaffoldMessenger.of(context).showSnackBar(
|
||||||
|
SnackBar(content: Text("Error: $error")),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
ScaffoldMessenger.of(context).showSnackBar(
|
||||||
|
SnackBar(content: Text("Failed to process request: $e")),
|
||||||
|
);
|
||||||
|
} finally {
|
||||||
|
setState(() {
|
||||||
|
_isLoading = false;
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -134,7 +232,12 @@ class _ResetPasswordScreenState extends State<Changepassword> {
|
|||||||
width: double.infinity,
|
width: double.infinity,
|
||||||
height: 48,
|
height: 48,
|
||||||
child: ElevatedButton(
|
child: ElevatedButton(
|
||||||
onPressed: _isLoading ? null : _handleSubmit,
|
onPressed: _isLoading
|
||||||
|
? null
|
||||||
|
: () {
|
||||||
|
final email = _emailController.text.trim();
|
||||||
|
sendVerificationCode(email);
|
||||||
|
},
|
||||||
style: ElevatedButton.styleFrom(
|
style: ElevatedButton.styleFrom(
|
||||||
backgroundColor: const Color(0xFF8B7355),
|
backgroundColor: const Color(0xFF8B7355),
|
||||||
foregroundColor: Colors.white,
|
foregroundColor: Colors.white,
|
||||||
@ -163,9 +266,14 @@ class _ResetPasswordScreenState extends State<Changepassword> {
|
|||||||
fontWeight: FontWeight.w500,
|
fontWeight: FontWeight.w500,
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
SizedBox(width: 2,),
|
SizedBox(
|
||||||
Icon(Icons.arrow_forward_ios,color: Colors.white38,size: 18,),
|
width: 2,
|
||||||
|
),
|
||||||
|
Icon(
|
||||||
|
Icons.arrow_forward_ios,
|
||||||
|
color: Colors.white38,
|
||||||
|
size: 18,
|
||||||
|
),
|
||||||
],
|
],
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
@ -173,19 +281,21 @@ class _ResetPasswordScreenState extends State<Changepassword> {
|
|||||||
),
|
),
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
SizedBox(height: screenheight/3,),
|
SizedBox(
|
||||||
|
height: screenheight / 3,
|
||||||
|
),
|
||||||
Center(
|
Center(
|
||||||
child: Container(
|
child: Container(
|
||||||
height: screenheight/8,
|
height: screenheight / 8,
|
||||||
width: screenwidth/2,
|
width: screenwidth / 2,
|
||||||
decoration: BoxDecoration(
|
decoration: BoxDecoration(
|
||||||
image: DecorationImage(
|
image: DecorationImage(
|
||||||
image: AssetImage("assets/splash_screen/logo.png"), // Background image asset
|
image: AssetImage(
|
||||||
fit: BoxFit.fill,
|
"assets/splash_screen/logo.png"), // Background image asset
|
||||||
),
|
fit: BoxFit.fill,
|
||||||
),
|
),
|
||||||
))
|
),
|
||||||
|
))
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
@ -195,8 +305,3 @@ class _ResetPasswordScreenState extends State<Changepassword> {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -1,13 +1,19 @@
|
|||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:pocketbase/pocketbase.dart';
|
||||||
|
import 'package:uae_stat/presentation/Screens/profilepage.dart';
|
||||||
|
|
||||||
class ConfirmPassword extends StatefulWidget {
|
class ConfirmPassword extends StatefulWidget {
|
||||||
ConfirmPassword({super.key});
|
final String email;
|
||||||
|
final String userId;
|
||||||
|
ConfirmPassword({required this.email, required this.userId});
|
||||||
|
|
||||||
@override
|
@override
|
||||||
State<ConfirmPassword> createState() => _ConfirmPasswordState();
|
State<ConfirmPassword> createState() => _ConfirmPasswordState();
|
||||||
}
|
}
|
||||||
|
|
||||||
class _ConfirmPasswordState extends State<ConfirmPassword> {
|
class _ConfirmPasswordState extends State<ConfirmPassword> {
|
||||||
|
final _pb = PocketBase('https://pb.venbait.in');
|
||||||
|
// final pb = PocketBase('http://127.0.0.1:8090');
|
||||||
final _formKey = GlobalKey<FormState>();
|
final _formKey = GlobalKey<FormState>();
|
||||||
bool _obscurePassword = true;
|
bool _obscurePassword = true;
|
||||||
bool _obscureConfirmPassword = true;
|
bool _obscureConfirmPassword = true;
|
||||||
@ -32,6 +38,68 @@ class _ConfirmPasswordState extends State<ConfirmPassword> {
|
|||||||
return null;
|
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}, // Updated password
|
||||||
|
);
|
||||||
|
|
||||||
|
// 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
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
double screenheight = MediaQuery.of(context).size.height;
|
double screenheight = MediaQuery.of(context).size.height;
|
||||||
@ -49,7 +117,9 @@ class _ConfirmPasswordState extends State<ConfirmPassword> {
|
|||||||
padding: const EdgeInsets.all(24.0),
|
padding: const EdgeInsets.all(24.0),
|
||||||
child: Column(
|
child: Column(
|
||||||
children: [
|
children: [
|
||||||
SizedBox(height: screenheight/6,),
|
SizedBox(
|
||||||
|
height: screenheight / 6,
|
||||||
|
),
|
||||||
Text(
|
Text(
|
||||||
"Create New Password",
|
"Create New Password",
|
||||||
style: TextStyle(
|
style: TextStyle(
|
||||||
@ -57,7 +127,9 @@ class _ConfirmPasswordState extends State<ConfirmPassword> {
|
|||||||
fontWeight: FontWeight.bold,
|
fontWeight: FontWeight.bold,
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
SizedBox(height: screenheight/35,),
|
SizedBox(
|
||||||
|
height: screenheight / 35,
|
||||||
|
),
|
||||||
Text(
|
Text(
|
||||||
"Your new password must de different\nform previously used password",
|
"Your new password must de different\nform previously used password",
|
||||||
textAlign: TextAlign.center,
|
textAlign: TextAlign.center,
|
||||||
@ -66,7 +138,9 @@ class _ConfirmPasswordState extends State<ConfirmPassword> {
|
|||||||
color: Colors.grey[600],
|
color: Colors.grey[600],
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
SizedBox(height: screenheight/35,),
|
SizedBox(
|
||||||
|
height: screenheight / 35,
|
||||||
|
),
|
||||||
TextFormField(
|
TextFormField(
|
||||||
obscureText: _obscurePassword,
|
obscureText: _obscurePassword,
|
||||||
decoration: InputDecoration(
|
decoration: InputDecoration(
|
||||||
@ -92,7 +166,9 @@ class _ConfirmPasswordState extends State<ConfirmPassword> {
|
|||||||
),
|
),
|
||||||
validator: _validatePassword,
|
validator: _validatePassword,
|
||||||
),
|
),
|
||||||
SizedBox(height: screenheight/35,),
|
SizedBox(
|
||||||
|
height: screenheight / 35,
|
||||||
|
),
|
||||||
TextFormField(
|
TextFormField(
|
||||||
obscureText: _obscureConfirmPassword,
|
obscureText: _obscureConfirmPassword,
|
||||||
decoration: InputDecoration(
|
decoration: InputDecoration(
|
||||||
@ -110,7 +186,8 @@ class _ConfirmPasswordState extends State<ConfirmPassword> {
|
|||||||
),
|
),
|
||||||
onPressed: () {
|
onPressed: () {
|
||||||
setState(() {
|
setState(() {
|
||||||
_obscureConfirmPassword = !_obscureConfirmPassword;
|
_obscureConfirmPassword =
|
||||||
|
!_obscureConfirmPassword;
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
@ -118,20 +195,28 @@ class _ConfirmPasswordState extends State<ConfirmPassword> {
|
|||||||
),
|
),
|
||||||
validator: _validateConfirmPassword,
|
validator: _validateConfirmPassword,
|
||||||
),
|
),
|
||||||
SizedBox(height: screenheight/35,),
|
|
||||||
SizedBox(
|
SizedBox(
|
||||||
width: screenwidth/1.1,
|
height: screenheight / 35,
|
||||||
|
),
|
||||||
|
SizedBox(
|
||||||
|
width: screenwidth / 1.1,
|
||||||
child: ElevatedButton(
|
child: ElevatedButton(
|
||||||
onPressed: () {
|
onPressed: () async {
|
||||||
if ((_formKey.currentState?.validate() ?? false)) {
|
if ((_formKey.currentState?.validate() ?? false)) {
|
||||||
// Proceed with registration if form is valid and checkbox is checked
|
//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
|
// Add verification logic here
|
||||||
},
|
},
|
||||||
style: ElevatedButton.styleFrom(
|
style: ElevatedButton.styleFrom(
|
||||||
backgroundColor: Colors.brown[300],
|
backgroundColor: Colors.brown[300],
|
||||||
padding: EdgeInsets.symmetric(horizontal: 80, vertical: 16),
|
padding: EdgeInsets.symmetric(
|
||||||
|
horizontal: 80, vertical: 16),
|
||||||
shape: RoundedRectangleBorder(
|
shape: RoundedRectangleBorder(
|
||||||
borderRadius: BorderRadius.circular(10),
|
borderRadius: BorderRadius.circular(10),
|
||||||
),
|
),
|
||||||
@ -141,10 +226,17 @@ class _ConfirmPasswordState extends State<ConfirmPassword> {
|
|||||||
children: [
|
children: [
|
||||||
Text(
|
Text(
|
||||||
"Save",
|
"Save",
|
||||||
style: TextStyle(fontSize: 18,color: Colors.white),
|
style: TextStyle(
|
||||||
|
fontSize: 18, color: Colors.white),
|
||||||
|
),
|
||||||
|
SizedBox(
|
||||||
|
width: 2,
|
||||||
|
),
|
||||||
|
Icon(
|
||||||
|
Icons.arrow_forward_ios,
|
||||||
|
color: Colors.white38,
|
||||||
|
size: 18,
|
||||||
),
|
),
|
||||||
SizedBox(width: 2,),
|
|
||||||
Icon(Icons.arrow_forward_ios,color: Colors.white38,size: 18,),
|
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
@ -152,18 +244,21 @@ class _ConfirmPasswordState extends State<ConfirmPassword> {
|
|||||||
],
|
],
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
SizedBox(height: screenheight/15,),
|
SizedBox(
|
||||||
|
height: screenheight / 15,
|
||||||
|
),
|
||||||
Center(
|
Center(
|
||||||
child: Container(
|
child: Container(
|
||||||
height: screenheight/10,
|
height: screenheight / 10,
|
||||||
width: screenwidth/2.5,
|
width: screenwidth / 2.5,
|
||||||
decoration: BoxDecoration(
|
decoration: BoxDecoration(
|
||||||
image: DecorationImage(
|
image: DecorationImage(
|
||||||
image: AssetImage("assets/splash_screen/logo.png"), // Background image asset
|
image: AssetImage(
|
||||||
fit: BoxFit.fill,
|
"assets/splash_screen/logo.png"), // Background image asset
|
||||||
),
|
fit: BoxFit.fill,
|
||||||
),
|
),
|
||||||
))
|
),
|
||||||
|
))
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
|
|||||||
@ -1,20 +1,35 @@
|
|||||||
import 'dart:async';
|
import 'dart:async';
|
||||||
|
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:pocketbase/pocketbase.dart';
|
||||||
|
|
||||||
import 'confirm_password.dart';
|
import 'confirm_password.dart';
|
||||||
|
|
||||||
class EmailVerificationScreen extends StatefulWidget {
|
class EmailVerificationScreen extends StatefulWidget {
|
||||||
|
final String email;
|
||||||
|
final String userId, otp, otpId;
|
||||||
|
final Function(String) sendVerificationCode;
|
||||||
|
|
||||||
|
EmailVerificationScreen(
|
||||||
|
{required this.email,
|
||||||
|
required this.userId,
|
||||||
|
required this.otp,
|
||||||
|
required this.otpId,
|
||||||
|
required this.sendVerificationCode});
|
||||||
|
|
||||||
@override
|
@override
|
||||||
_EmailVerificationScreenState createState() =>
|
_EmailVerificationScreenState createState() =>
|
||||||
_EmailVerificationScreenState();
|
_EmailVerificationScreenState();
|
||||||
}
|
}
|
||||||
|
|
||||||
class _EmailVerificationScreenState extends State<EmailVerificationScreen> {
|
class _EmailVerificationScreenState extends State<EmailVerificationScreen> {
|
||||||
|
// final pb = PocketBase('http://127.0.0.1:8090');
|
||||||
|
final _pb = PocketBase('https://pb.venbait.in');
|
||||||
final List<TextEditingController> _otpControllers =
|
final List<TextEditingController> _otpControllers =
|
||||||
List.generate(4, (_) => TextEditingController());
|
List.generate(4, (_) => TextEditingController());
|
||||||
int _secondsRemaining = 120; // 2 minutes timer
|
int _secondsRemaining = 120; // 2 minutes timer
|
||||||
late Timer _timer;
|
late Timer _timer;
|
||||||
|
bool _canResend = false;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void initState() {
|
void initState() {
|
||||||
@ -23,12 +38,16 @@ class _EmailVerificationScreenState extends State<EmailVerificationScreen> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void _startTimer() {
|
void _startTimer() {
|
||||||
|
_canResend = false;
|
||||||
_timer = Timer.periodic(Duration(seconds: 1), (timer) {
|
_timer = Timer.periodic(Duration(seconds: 1), (timer) {
|
||||||
if (_secondsRemaining > 0) {
|
if (_secondsRemaining > 0) {
|
||||||
setState(() {
|
setState(() {
|
||||||
_secondsRemaining--;
|
_secondsRemaining--;
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
|
setState(() {
|
||||||
|
_canResend = true; // Enable "Resend Code" when the timer ends
|
||||||
|
});
|
||||||
timer.cancel();
|
timer.cancel();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@ -49,6 +68,42 @@ class _EmailVerificationScreenState extends State<EmailVerificationScreen> {
|
|||||||
super.dispose();
|
super.dispose();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void verify(BuildContext context, String email, String enteredOTP,
|
||||||
|
String userId) async {
|
||||||
|
// Validate OTP length
|
||||||
|
if (enteredOTP.length != 4) {
|
||||||
|
ScaffoldMessenger.of(context).showSnackBar(
|
||||||
|
SnackBar(content: Text("Please enter a valid 4-digit code!")),
|
||||||
|
);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
if (enteredOTP == widget.otp && email == widget.email) {
|
||||||
|
// Parse the response
|
||||||
|
|
||||||
|
//print('VERIFIED OTF');
|
||||||
|
|
||||||
|
Navigator.push(
|
||||||
|
context,
|
||||||
|
MaterialPageRoute(
|
||||||
|
builder: (context) => ConfirmPassword(
|
||||||
|
email: widget.email, // Pass the email
|
||||||
|
userId: widget.userId)),
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
// No matching record found
|
||||||
|
ScaffoldMessenger.of(context).showSnackBar(
|
||||||
|
SnackBar(content: Text("Invalid OTP. Please try again.")),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
// Catch network or other unexpected errors
|
||||||
|
ScaffoldMessenger.of(context).showSnackBar(
|
||||||
|
SnackBar(content: Text("Something went wrong. Please try again.")),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
@ -63,7 +118,9 @@ class _EmailVerificationScreenState extends State<EmailVerificationScreen> {
|
|||||||
children: [
|
children: [
|
||||||
Column(
|
Column(
|
||||||
children: [
|
children: [
|
||||||
SizedBox(height: screenheight/6,),
|
SizedBox(
|
||||||
|
height: screenheight / 6,
|
||||||
|
),
|
||||||
Text(
|
Text(
|
||||||
"Verify Your Email",
|
"Verify Your Email",
|
||||||
style: TextStyle(
|
style: TextStyle(
|
||||||
@ -90,11 +147,11 @@ class _EmailVerificationScreenState extends State<EmailVerificationScreen> {
|
|||||||
height: 50,
|
height: 50,
|
||||||
alignment: Alignment.center,
|
alignment: Alignment.center,
|
||||||
decoration: BoxDecoration(
|
decoration: BoxDecoration(
|
||||||
border: Border.all(color: Colors.grey.shade400, width: 1),
|
border:
|
||||||
|
Border.all(color: Colors.grey.shade400, width: 1),
|
||||||
borderRadius: BorderRadius.circular(8),
|
borderRadius: BorderRadius.circular(8),
|
||||||
),
|
),
|
||||||
child:
|
child: TextField(
|
||||||
TextField(
|
|
||||||
controller: _otpControllers[index],
|
controller: _otpControllers[index],
|
||||||
keyboardType: TextInputType.number,
|
keyboardType: TextInputType.number,
|
||||||
textAlign: TextAlign.center,
|
textAlign: TextAlign.center,
|
||||||
@ -103,11 +160,13 @@ class _EmailVerificationScreenState extends State<EmailVerificationScreen> {
|
|||||||
counterText: "",
|
counterText: "",
|
||||||
border: OutlineInputBorder(
|
border: OutlineInputBorder(
|
||||||
borderRadius: BorderRadius.circular(8),
|
borderRadius: BorderRadius.circular(8),
|
||||||
borderSide: BorderSide(color: Colors.blue, width: 2),
|
borderSide:
|
||||||
|
BorderSide(color: Colors.blue, width: 2),
|
||||||
),
|
),
|
||||||
focusedBorder: OutlineInputBorder(
|
focusedBorder: OutlineInputBorder(
|
||||||
borderRadius: BorderRadius.circular(8),
|
borderRadius: BorderRadius.circular(8),
|
||||||
borderSide: BorderSide(color: Colors.blue, width: 2),
|
borderSide:
|
||||||
|
BorderSide(color: Colors.blue, width: 2),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
onChanged: (value) {
|
onChanged: (value) {
|
||||||
@ -127,36 +186,49 @@ class _EmailVerificationScreenState extends State<EmailVerificationScreen> {
|
|||||||
children: [
|
children: [
|
||||||
TextSpan(
|
TextSpan(
|
||||||
text: _formattedTime,
|
text: _formattedTime,
|
||||||
style: TextStyle(fontWeight: FontWeight.w900,color: Colors.black87),
|
style: TextStyle(
|
||||||
|
fontWeight: FontWeight.w900,
|
||||||
|
color: Colors.black87),
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
SizedBox(height: 5),
|
SizedBox(height: 5),
|
||||||
TextButton(
|
TextButton(
|
||||||
onPressed: () {
|
onPressed: _canResend
|
||||||
setState(() {
|
? () {
|
||||||
_secondsRemaining = 120;
|
setState(() {
|
||||||
_startTimer();
|
_secondsRemaining = 120;
|
||||||
});
|
_startTimer();
|
||||||
},
|
});
|
||||||
child: Text("Resend Code",style: TextStyle(color: Colors.brown,fontWeight: FontWeight.bold),),
|
widget.sendVerificationCode(widget.email);
|
||||||
|
}
|
||||||
|
: null,
|
||||||
|
child: Text(
|
||||||
|
"Resend Code",
|
||||||
|
style: TextStyle(
|
||||||
|
color: Colors.brown, fontWeight: FontWeight.bold),
|
||||||
|
),
|
||||||
),
|
),
|
||||||
SizedBox(height: 5),
|
SizedBox(height: 5),
|
||||||
SizedBox(
|
SizedBox(
|
||||||
width: screenwidth/1.2,
|
width: screenwidth / 1.2,
|
||||||
child: ElevatedButton(
|
child: ElevatedButton(
|
||||||
onPressed: () {
|
onPressed: () async {
|
||||||
Navigator.push(
|
// Combine the entered OTP
|
||||||
context,
|
String enteredOTP = _otpControllers
|
||||||
MaterialPageRoute(builder: (context) => ConfirmPassword()),
|
.map((controller) => controller.text)
|
||||||
);
|
.join();
|
||||||
|
|
||||||
|
verify(
|
||||||
|
context, widget.email, enteredOTP, widget.userId);
|
||||||
|
|
||||||
// Add verification logic here
|
// Add verification logic here
|
||||||
},
|
},
|
||||||
style: ElevatedButton.styleFrom(
|
style: ElevatedButton.styleFrom(
|
||||||
backgroundColor: Colors.brown[300],
|
backgroundColor: Colors.brown[300],
|
||||||
padding: EdgeInsets.symmetric(horizontal: 80, vertical: 16),
|
padding:
|
||||||
|
EdgeInsets.symmetric(horizontal: 80, vertical: 16),
|
||||||
shape: RoundedRectangleBorder(
|
shape: RoundedRectangleBorder(
|
||||||
borderRadius: BorderRadius.circular(8),
|
borderRadius: BorderRadius.circular(8),
|
||||||
),
|
),
|
||||||
@ -166,32 +238,41 @@ class _EmailVerificationScreenState extends State<EmailVerificationScreen> {
|
|||||||
children: [
|
children: [
|
||||||
Text(
|
Text(
|
||||||
"Verify",
|
"Verify",
|
||||||
style: TextStyle(fontSize: 18,color: Colors.white),
|
style: TextStyle(fontSize: 18, color: Colors.white),
|
||||||
|
),
|
||||||
|
SizedBox(
|
||||||
|
width: 3,
|
||||||
|
),
|
||||||
|
Icon(
|
||||||
|
Icons.arrow_forward_ios,
|
||||||
|
color: Colors.white38,
|
||||||
|
size: 18,
|
||||||
),
|
),
|
||||||
SizedBox(width: 3,),
|
|
||||||
Icon(Icons.arrow_forward_ios,color: Colors.white38,size: 18,),
|
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
SizedBox(height: screenheight/6,),
|
SizedBox(
|
||||||
|
height: screenheight / 6,
|
||||||
|
),
|
||||||
Center(
|
Center(
|
||||||
child: Container(
|
child: Container(
|
||||||
height: screenheight/8,
|
height: screenheight / 8,
|
||||||
width: screenwidth/2,
|
width: screenwidth / 2,
|
||||||
decoration: BoxDecoration(
|
decoration: BoxDecoration(
|
||||||
image: DecorationImage(
|
image: DecorationImage(
|
||||||
image: AssetImage("assets/splash_screen/logo.png"), // Background image asset
|
image: AssetImage(
|
||||||
fit: BoxFit.fill,
|
"assets/splash_screen/logo.png"), // Background image asset
|
||||||
),
|
fit: BoxFit.fill,
|
||||||
),
|
),
|
||||||
))
|
),
|
||||||
|
))
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -172,24 +172,31 @@ class _ProfileScreenState extends State<ProfileScreen> {
|
|||||||
DateTime dob = DateFormat('dd/MM/yyyy').parse(dateOfBirth);
|
DateTime dob = DateFormat('dd/MM/yyyy').parse(dateOfBirth);
|
||||||
String formattedDob = DateFormat('yyyy-MM-dd').format(dob);
|
String formattedDob = DateFormat('yyyy-MM-dd').format(dob);
|
||||||
|
|
||||||
// Prepare the data to be updated
|
// Create a multipart request
|
||||||
final Map<String, dynamic> userData = {
|
final uri =
|
||||||
'full_name': fullName,
|
Uri.parse('${_pb.baseUrl}/api/collections/users/records/$userID');
|
||||||
'dob': formattedDob,
|
final request = http.MultipartRequest('PATCH', uri);
|
||||||
'country_region': countryRegion,
|
|
||||||
'is_profile_completed': 'True'
|
|
||||||
};
|
|
||||||
|
|
||||||
// If there's a profile image, upload it separately or include in userData if PocketBase supports files in the update
|
// Add other fields
|
||||||
|
request.fields['full_name'] = fullName;
|
||||||
|
request.fields['dob'] = formattedDob;
|
||||||
|
request.fields['country_region'] = countryRegion;
|
||||||
|
request.fields['is_profile_completed'] = 'True';
|
||||||
|
|
||||||
|
// Add the file, if available
|
||||||
if (_profileImage != null) {
|
if (_profileImage != null) {
|
||||||
userData['avatar'] = await http.MultipartFile.fromPath(
|
request.files.add(await http.MultipartFile.fromPath(
|
||||||
'avatar', _profileImage!.path);
|
'avatar',
|
||||||
|
_profileImage!.path,
|
||||||
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update the user profile in PocketBase
|
// Add headers (if required, e.g., authorization)
|
||||||
final updatedUser =
|
request.headers['Authorization'] = 'Bearer ${_pb.authStore.token}';
|
||||||
await _pb.collection('users').update(userID, body: userData);
|
|
||||||
print(updatedUser);
|
// Send the request
|
||||||
|
final response = await request.send();
|
||||||
|
print(response);
|
||||||
_resetFormFields();
|
_resetFormFields();
|
||||||
ScaffoldMessenger.of(context).showSnackBar(
|
ScaffoldMessenger.of(context).showSnackBar(
|
||||||
SnackBar(content: Text("Profile updated successfully!")));
|
SnackBar(content: Text("Profile updated successfully!")));
|
||||||
|
|||||||
@ -24,7 +24,9 @@ class _RegisterScreenState extends State<RegisterScreen> {
|
|||||||
String? _password;
|
String? _password;
|
||||||
bool registrationSuccess = false;
|
bool registrationSuccess = false;
|
||||||
bool registrationFailed = false;
|
bool registrationFailed = false;
|
||||||
|
dynamic userID;
|
||||||
final pb = PocketBase('https://pb.venbait.in');
|
final pb = PocketBase('https://pb.venbait.in');
|
||||||
|
// final pb = PocketBase('http://127.0.0.1:8090');
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void initState() {
|
void initState() {
|
||||||
@ -80,6 +82,9 @@ class _RegisterScreenState extends State<RegisterScreen> {
|
|||||||
if (_formKey.currentState?.validate() ?? false) {
|
if (_formKey.currentState?.validate() ?? false) {
|
||||||
if (isChecked) {
|
if (isChecked) {
|
||||||
try {
|
try {
|
||||||
|
final adminAuth = await pb.admins
|
||||||
|
.authWithPassword('pb@venbainfotech.com', 'pb@venbainfotech.com');
|
||||||
|
final adminToken = adminAuth.token;
|
||||||
// Create user in PocketBase
|
// Create user in PocketBase
|
||||||
final response = await pb.collection('users').create(body: {
|
final response = await pb.collection('users').create(body: {
|
||||||
'username': _usernameController.text,
|
'username': _usernameController.text,
|
||||||
@ -87,12 +92,16 @@ class _RegisterScreenState extends State<RegisterScreen> {
|
|||||||
'password': _passwordController.text,
|
'password': _passwordController.text,
|
||||||
'passwordConfirm': _passwordController
|
'passwordConfirm': _passwordController
|
||||||
.text, // PocketBase requires password confirmation
|
.text, // PocketBase requires password confirmation
|
||||||
|
}, headers: {
|
||||||
|
'Authorization': adminToken
|
||||||
});
|
});
|
||||||
if (response.id != null) {
|
if (response.id != null) {
|
||||||
|
userID = response.id;
|
||||||
// Request email verification
|
// Request email verification
|
||||||
PocketBaseService.users.requestVerification(_emailController.text);
|
// PocketBaseService.users.requestVerification(_emailController.text);
|
||||||
// Show success message instead of navigating away
|
// Show success message instead of navigating away
|
||||||
setState(() {
|
setState(() {
|
||||||
|
userID = response.id;
|
||||||
registrationSuccess = true;
|
registrationSuccess = true;
|
||||||
registrationFailed = false; // Show success message on success
|
registrationFailed = false; // Show success message on success
|
||||||
});
|
});
|
||||||
@ -182,9 +191,14 @@ class _RegisterScreenState extends State<RegisterScreen> {
|
|||||||
),
|
),
|
||||||
SizedBox(height: 10),
|
SizedBox(height: 10),
|
||||||
ElevatedButton(
|
ElevatedButton(
|
||||||
onPressed: () => context.go(
|
onPressed: () => {
|
||||||
'/${context.language}/login',
|
Navigator.pushReplacement(
|
||||||
),
|
context,
|
||||||
|
MaterialPageRoute(
|
||||||
|
builder: (context) =>
|
||||||
|
ProfileScreen(userId: userID)),
|
||||||
|
),
|
||||||
|
},
|
||||||
child: Text(
|
child: Text(
|
||||||
'Go to Login',
|
'Go to Login',
|
||||||
),
|
),
|
||||||
|
|||||||
@ -29,7 +29,8 @@ class _FeedbackFormState extends State<FeedbackForm>
|
|||||||
with WidgetsBindingObserver {
|
with WidgetsBindingObserver {
|
||||||
final _pb =
|
final _pb =
|
||||||
PocketBase('https://pb.venbait.in'); // Initialize PocketBase client
|
PocketBase('https://pb.venbait.in'); // Initialize PocketBase client
|
||||||
// final _pb = PocketBase('http://127.0.0.1:8090'); // Initialize PocketBase client
|
// final _pb =
|
||||||
|
// PocketBase('http://127.0.0.1:8090'); // Initialize PocketBase client
|
||||||
|
|
||||||
final TextEditingController _feedbackController = TextEditingController();
|
final TextEditingController _feedbackController = TextEditingController();
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user