add email verification and account screen

This commit is contained in:
2026-01-08 11:20:13 -06:00
parent 6302018647
commit 0ffffdeee5
6 changed files with 582 additions and 15 deletions

View File

@@ -6,6 +6,8 @@ import 'package:flutter/material.dart';
import 'package:blind_master/BlindMasterResources/text_inputs.dart';
import 'package:blind_master/BlindMasterResources/title_text.dart';
import 'package:blind_master/BlindMasterScreens/Startup/verification_waiting_screen.dart';
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
class CreateUserScreen extends StatefulWidget {
const CreateUserScreen({super.key});
@@ -44,18 +46,25 @@ class _CreateUserScreenState extends State<CreateUserScreen> {
if (response.statusCode == 201) {
if(mounted) {
ScaffoldMessenger.of(context).clearSnackBars();
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
backgroundColor: Colors.green[800],
content: Text(
"Account Successfully Created!",
textAlign: TextAlign.center,
style: TextStyle(fontSize: 15),
// Extract token from response body
final body = json.decode(response.body);
final token = body['token'];
if (token != null && token.isNotEmpty) {
// Store token temporarily for verification checking
final storage = FlutterSecureStorage();
await storage.write(key: 'temp_token', value: token);
// Navigate to verification waiting screen
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => VerificationWaitingScreen(token: token),
),
)
);
Navigator.pop(context);
);
} else {
throw Exception('No token received from server');
}
}
} else {
if (response.statusCode == 409) throw Exception('Email Already In Use!');

View File

@@ -51,6 +51,23 @@ class _LoginScreenState extends State<LoginScreen> {
final response = await regularPost(payload, 'login');
if (response.statusCode != 200) {
if (response.statusCode == 400) {throw Exception('Email and Password Necessary');}
else if (response.statusCode == 403) {
// Email not verified
if (!mounted) return;
ScaffoldMessenger.of(context).clearSnackBars();
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
backgroundColor: Colors.orange[700],
duration: Duration(seconds: 4),
content: Text(
"Your account has not been verified. Please check your email from blindmasterapp@wahwa.com and verify your account.",
textAlign: TextAlign.center,
style: TextStyle(fontSize: 15),
),
)
);
return;
}
else if (response.statusCode == 429) {
final body = json.decode(response.body);
final retryAfter = body['retryAfter'] ?? 'some time';

View File

@@ -0,0 +1,217 @@
import 'dart:async';
import 'dart:convert';
import 'package:blind_master/BlindMasterResources/error_snackbar.dart';
import 'package:flutter/material.dart';
import 'package:blind_master/BlindMasterResources/title_text.dart';
import 'package:http/http.dart' as http;
class VerificationWaitingScreen extends StatefulWidget {
final String token;
const VerificationWaitingScreen({super.key, required this.token});
@override
State<VerificationWaitingScreen> createState() => _VerificationWaitingScreenState();
}
class _VerificationWaitingScreenState extends State<VerificationWaitingScreen> {
Timer? _pollingTimer;
bool _isChecking = false;
@override
void initState() {
super.initState();
_startPolling();
}
@override
void dispose() {
_pollingTimer?.cancel();
super.dispose();
}
void _startPolling() {
_pollingTimer = Timer.periodic(Duration(seconds: 5), (timer) {
_checkVerificationStatus();
});
}
Future<void> _checkVerificationStatus() async {
if (_isChecking) return;
setState(() {
_isChecking = true;
});
try {
final uri = Uri.parse('https://wahwa.com').replace(path: 'verification_status');
final response = await http.get(
uri,
headers: {
'Authorization': 'Bearer ${widget.token}',
'Content-Type': 'application/json',
},
).timeout(const Duration(seconds: 10));
if (response.statusCode == 200) {
final body = json.decode(response.body);
if (body['is_verified'] == true) {
_pollingTimer?.cancel();
if (!mounted) return;
Navigator.of(context).popUntil((route) => route.isFirst);
ScaffoldMessenger.of(context).clearSnackBars();
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
backgroundColor: Colors.green[800],
duration: Duration(seconds: 4),
content: Text(
"Account verified successfully! You can now log in.",
textAlign: TextAlign.center,
style: TextStyle(fontSize: 15),
),
)
);
}
}
} catch (e) {
// Silently fail for polling - don't show error to user
print('Verification status check failed: $e');
} finally {
if (mounted) {
setState(() {
_isChecking = false;
});
}
}
}
Future<void> _resendVerificationEmail() async {
try {
final uri = Uri.parse('https://wahwa.com').replace(path: 'resend_verification');
final response = await http.post(
uri,
headers: {
'Authorization': 'Bearer ${widget.token}',
'Content-Type': 'application/json',
},
body: json.encode({}),
).timeout(const Duration(seconds: 10));
if (!mounted) return;
if (response.statusCode == 200) {
ScaffoldMessenger.of(context).clearSnackBars();
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
backgroundColor: Colors.green[800],
content: Text(
"Verification email sent! Please check your inbox.",
textAlign: TextAlign.center,
style: TextStyle(fontSize: 15),
),
)
);
} else if (response.statusCode == 429) {
final body = json.decode(response.body);
final retryAfter = body['retryAfter'] ?? 20;
throw Exception('Please wait $retryAfter seconds before requesting another email.');
} else {
throw Exception('Failed to resend verification email');
}
} catch (e) {
if (!mounted) return;
ScaffoldMessenger.of(context).clearSnackBars();
ScaffoldMessenger.of(context).showSnackBar(
errorSnackbar(e)
);
}
}
Future<void> _onRefresh() async {
await _checkVerificationStatus();
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: RefreshIndicator(
onRefresh: _onRefresh,
child: Container(
color: Theme.of(context).primaryColorLight,
child: ListView(
physics: AlwaysScrollableScrollPhysics(),
children: [
Container(
height: MediaQuery.of(context).size.height,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
TitleText("Verify Your Email", txtClr: Colors.white),
SizedBox(height: 30),
Padding(
padding: EdgeInsets.symmetric(horizontal: 40),
child: Text(
"We've sent a verification link to your email from blindmasterapp@wahwa.com",
textAlign: TextAlign.center,
style: TextStyle(
color: Colors.white,
fontSize: 16,
),
),
),
SizedBox(height: 20),
if (_isChecking)
CircularProgressIndicator(
valueColor: AlwaysStoppedAnimation<Color>(Colors.white),
)
else
Icon(
Icons.email_outlined,
size: 80,
color: Colors.white70,
),
SizedBox(height: 30),
Padding(
padding: EdgeInsets.symmetric(horizontal: 40),
child: Text(
"Click the link in the email to verify your account. This page will automatically update once verified.",
textAlign: TextAlign.center,
style: TextStyle(
color: Colors.white70,
fontSize: 14,
),
),
),
SizedBox(height: 40),
ElevatedButton(
onPressed: _resendVerificationEmail,
child: Text("Resend Verification Email"),
),
SizedBox(height: 20),
Padding(
padding: EdgeInsets.symmetric(horizontal: 40),
child: Text(
"Pull down to check verification status manually",
textAlign: TextAlign.center,
style: TextStyle(
color: Colors.white54,
fontSize: 12,
fontStyle: FontStyle.italic,
),
),
),
],
),
),
],
),
),
),
);
}
}