Multiple improvements in authentication functionality
This commit is contained in:
1 parent
bd5c4e002f
commit
fbee31841d
11 files changed
+260
-17
No files matched your search
@@ -1,4 +1,5 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
|
||||
import 'backend.dart';
|
||||
import 'login_screen.dart';
|
||||
import 'chat_screen.dart';
|
||||
@@ -11,12 +12,26 @@ class UserChecker extends StatefulWidget {
|
||||
|
||||
class _UserCheckerState extends State<UserChecker> {
|
||||
String? username;
|
||||
bool isLoading = false;
|
||||
bool isLoading = true;
|
||||
final _storage = const FlutterSecureStorage();
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
username = null;
|
||||
_checkSavedCredentials();
|
||||
}
|
||||
|
||||
Future<void> _checkSavedCredentials() async {
|
||||
final savedUsername = await _storage.read(key: 'username');
|
||||
final savedPassword = await _storage.read(key: 'password');
|
||||
|
||||
if (savedUsername != null && savedPassword != null) {
|
||||
_handleLogin(savedUsername, savedPassword);
|
||||
} else {
|
||||
setState(() {
|
||||
isLoading = false;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
static void fetchChats(Function(List<int>) onSuccess) {
|
||||
@@ -28,13 +43,15 @@ class _UserCheckerState extends State<UserChecker> {
|
||||
});
|
||||
}
|
||||
|
||||
void _handleLogin(String user, String password) {
|
||||
Backend.auth(user, password);
|
||||
|
||||
void _handleLogin(String user, String password) async {
|
||||
setState(() {
|
||||
username = user;
|
||||
isLoading = true; // Show loading spinner
|
||||
isLoading = true;
|
||||
});
|
||||
|
||||
await Backend.auth(user, password);
|
||||
await _storage.write(key: 'username', value: user);
|
||||
await _storage.write(key: 'password', value: password);
|
||||
|
||||
fetchChats((chatIds) {
|
||||
if (chatIds.isEmpty) {
|
||||
@@ -49,14 +66,13 @@ class _UserCheckerState extends State<UserChecker> {
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
print("Build UserChecker: $username $isLoading");
|
||||
if (isLoading) {
|
||||
return Center(child: CircularProgressIndicator()); // Loading state UI
|
||||
return Center(child: CircularProgressIndicator());
|
||||
}
|
||||
|
||||
return LoginScreen(onLogin: _handleLogin);
|
||||
|
||||
Reference in new issue
Block a user