minor login fixes

This commit is contained in:
2026-06-01 14:45:42 +02:00
parent b61d81d84b
commit c86c7fca49
4 changed files with 106 additions and 57 deletions

View File

@@ -7,7 +7,7 @@ RUN apt-get install -y curl git unzip
# define variables
ARG FLUTTER_SDK=/usr/local/flutter
ARG FLUTTER_VERSION=3.35.6
ARG FLUTTER_VERSION=3.44.0
ARG APP=/app/
#clone flutter

View File

@@ -7,7 +7,7 @@ RUN apt-get install -y curl git unzip
# define variables
ARG FLUTTER_SDK=/usr/local/flutter
ARG FLUTTER_VERSION=3.35.6
ARG FLUTTER_VERSION=3.44.0
ARG APP=/app/
#clone flutter

View File

@@ -18,13 +18,29 @@ class LoginController extends ChangeNotifier {
final model = LoginScreenModel();
late SharedPreferences prefs;
BuildContext context;
bool _disposed = false;
LoginController(this.context);
Future<ApiResponse<LoginScreenModel>> login(String username, String password) async {
@override
void dispose() {
_disposed = true;
super.dispose();
}
void _safeNotifyListeners() {
if (!_disposed) {
notifyListeners();
}
}
Future<ApiResponse<LoginScreenModel>> login(
String username,
String password,
) async {
try {
model.isLoading = true;
notifyListeners();
_safeNotifyListeners();
prefs = await SharedPreferences.getInstance();
@@ -35,7 +51,7 @@ class LoginController extends ChangeNotifier {
logger.i(response.toString());
model.isLoading = false;
notifyListeners();
_safeNotifyListeners();
if (response.containsKey('token')) {
model.status = 'SUCCESS';
@@ -47,26 +63,32 @@ class LoginController extends ChangeNotifier {
prefs.setString('lastName', response['lastName']);
prefs.setString('phone', response['phone']);
prefs.setString('userId', response['uuid']);
prefs.setString('initials', response['firstName'].substring(0, 1) + response['lastName'].substring(0, 1));
prefs.setString(
'initials',
response['firstName'].substring(0, 1) +
response['lastName'].substring(0, 1),
);
return ApiResponse.success(model);
} else {
model.errorMessage =
response['errorMessage'] ??
"Problem with the transaction. Please try again or contact support";
"Problem with the operation. Please try again or contact support";
return ApiResponse.failure(model.errorMessage!);
}
} on DioException catch (e, s) {
logger.e(s);
model.isLoading = false;
notifyListeners();
return ApiResponse.failure(e.response?.data['errors'][0] ?? "Unknown error");
_safeNotifyListeners();
return ApiResponse.failure(
e.response?.data['errors'][0] ?? "Unknown error",
);
} catch (e, s) {
logger.e(s);
model.isLoading = false;
notifyListeners();
_safeNotifyListeners();
return ApiResponse.failure(
"Problem processing that transaction. Please try again or contact support",
"Problem processing that operation. Please try again or contact support",
);
}
}

View File

@@ -31,6 +31,7 @@ class _LoginScreenState extends State<LoginScreen> {
];
late LoginController _loginController;
StreamSubscription<GoogleSignInAuthenticationEvent>? _authSubscription;
@override
void initState() {
@@ -38,26 +39,28 @@ class _LoginScreenState extends State<LoginScreen> {
_loginController = LoginController(context);
googleSignIn = GoogleSignIn.instance;
if(kIsWeb){
if (kIsWeb) {
googleSignIn.initialize();
googleSignIn.authenticationEvents
.listen(_handleAuthenticationEvent)
.onError(_handleAuthenticationError);
}else {
unawaited(googleSignIn.initialize(serverClientId: dotenv.env['CLIENTID']));
_authSubscription = googleSignIn.authenticationEvents
.handleError(_handleAuthenticationError)
.listen(_handleAuthenticationEvent);
} else {
unawaited(
googleSignIn.initialize(serverClientId: dotenv.env['CLIENTID']),
);
}
}
Future<void> signInWithGoogle() async {
if (GoogleSignIn.instance.supportsAuthenticate()){
unawaited(GoogleSignIn.instance.authenticate().then((value) async {
googleSignIn.authenticationEvents
.listen(_handleAuthenticationEvent)
.onError(_handleAuthenticationError);
}));
if (GoogleSignIn.instance.supportsAuthenticate()) {
_authSubscription?.cancel();
unawaited(
GoogleSignIn.instance.authenticate().then((value) async {
_authSubscription = googleSignIn.authenticationEvents
.handleError(_handleAuthenticationError)
.listen(_handleAuthenticationEvent);
}),
);
}
}
@@ -78,6 +81,8 @@ class _LoginScreenState extends State<LoginScreen> {
Future<void> _handleAuthenticationEvent(
GoogleSignInAuthenticationEvent event,
) async {
if (!mounted) return;
// #docregion CheckAuthorization
final GoogleSignInAccount? user = // ...
// #enddocregion CheckAuthorization
@@ -86,6 +91,8 @@ class _LoginScreenState extends State<LoginScreen> {
GoogleSignInAuthenticationEventSignOut() => null,
};
if (user == null || !mounted) return;
// Check for existing authorization.
// #docregion CheckAuthorization
final GoogleSignInClientAuthorization? authorization = await user
@@ -94,9 +101,29 @@ class _LoginScreenState extends State<LoginScreen> {
// #enddocregion CheckAuthorization
print(user);
saveUser(user as GoogleSignInAccount);
saveUser(user);
await _loginController.login(user.email, user.id);
if (!mounted) return;
final response = await _loginController.login(user.email, user.id);
if (response.isSuccess) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text("Login successful!"),
behavior: SnackBarBehavior.floating,
backgroundColor: Colors.green,
),
);
} else {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(response.error ?? "Login failed"),
behavior: SnackBarBehavior.floating,
backgroundColor: Colors.deepOrange,
),
);
}
if (!mounted) return;
context.go('/');
}
@@ -106,6 +133,7 @@ class _LoginScreenState extends State<LoginScreen> {
@override
void dispose() {
_authSubscription?.cancel();
_loginController.dispose();
super.dispose();
}
@@ -158,7 +186,9 @@ class _LoginScreenState extends State<LoginScreen> {
// Divider with "or" text
Row(
children: [
Expanded(child: Divider(color: Colors.grey.shade300)),
Expanded(
child: Divider(color: Colors.grey.shade300),
),
Padding(
padding: EdgeInsets.symmetric(horizontal: 16),
child: Text(
@@ -170,28 +200,30 @@ class _LoginScreenState extends State<LoginScreen> {
textAlign: TextAlign.center,
),
),
Expanded(child: Divider(color: Colors.grey.shade300)),
Expanded(
child: Divider(color: Colors.grey.shade300),
),
],
),
SizedBox(height: 30),
// Social Login Buttons
if(GoogleSignIn.instance.supportsAuthenticate())
if (GoogleSignIn.instance.supportsAuthenticate())
_buildGoogleButton()
else ...<Widget>[
if (kIsWeb)
web.renderButton()
],
else ...<Widget>[if (kIsWeb) web.renderButton()],
SizedBox(height: 40),
TextButton(
onPressed: () {
context.go('/');
},
child: Text('Continue as guest?', style: TextStyle(fontSize: 14)),
child: Text(
'Continue as guest?',
style: TextStyle(fontSize: 14),
),
),
],
),
);
}
},
),
),
),
@@ -200,7 +232,7 @@ class _LoginScreenState extends State<LoginScreen> {
);
}
Widget _buildGoogleButton(){
Widget _buildGoogleButton() {
return Skeletonizer(
enabled: _loading,
child: Row(
@@ -210,11 +242,7 @@ class _LoginScreenState extends State<LoginScreen> {
onPressed: () {
signInWithGoogle();
},
icon: Image.asset(
'assets/google.png',
width: 20,
height: 20,
),
icon: Image.asset('assets/google.png', width: 20, height: 20),
label: Text('Google', style: TextStyle(fontSize: 16)),
style: OutlinedButton.styleFrom(
padding: EdgeInsets.symmetric(vertical: 16),
@@ -230,5 +258,4 @@ class _LoginScreenState extends State<LoginScreen> {
),
);
}
}