Skip to content

Commit

Permalink
fix android login problem and stabilize UI
Browse files Browse the repository at this point in the history
  • Loading branch information
Wrth1 committed Mar 20, 2024
1 parent de3c4ca commit eeb488b
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 72 deletions.
4 changes: 2 additions & 2 deletions lib/email_verification_gate.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class EmailVerificationGate extends StatelessWidget {
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
const Text(
'Please verify your email to access this page.',
textAlign: TextAlign.center,
),
Expand All @@ -35,7 +35,7 @@ class EmailVerificationGate extends StatelessWidget {
MaterialPageRoute(builder: (context) => const LoginPage()),
);
},
child: Text('Go to Login'),
child: const Text('Go to Login'),
),
],
),
Expand Down
4 changes: 3 additions & 1 deletion lib/home.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'dart:math';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
import 'package:google_sign_in/google_sign_in.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:testnote/list.dart';
import 'package:testnote/login.dart';
Expand Down Expand Up @@ -64,7 +65,7 @@ class _NotepadHomePageState extends State<NotepadHomePage> {
},
child: const MouseRegion(
cursor: SystemMouseCursors.click,
child: Text('Notease - v0.3 | 20 Maret 2024'),
child: Text('Notease - v0.3.1 | 20 Maret 2024'),
),
),
backgroundColor: const Color.fromARGB(255, 227, 179, 235),
Expand All @@ -87,6 +88,7 @@ class _NotepadHomePageState extends State<NotepadHomePage> {
),
);
} else {
await GoogleSignIn().disconnect();
await _auth.signOut();
}
},
Expand Down
142 changes: 73 additions & 69 deletions lib/login.dart
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,17 @@ class _LoginPageState extends State<LoginPage> {
accessToken: authenticationAccessToken,
);

await _auth.signInWithCredential(credential);
try {
await _auth.signInWithCredential(credential);
} on Exception {
setState(() {
_isError = true;
_loggedInEmail = "Something went wrong!";
});
}
if (_auth.currentUser == null) {
setState(() {
_isError = true;
_loggedInEmail = "Something went wrong!";
});
}
Expand All @@ -102,33 +110,28 @@ class _LoginPageState extends State<LoginPage> {
MaterialPageRoute(builder: (context) => const NotepadHomePage()),
);
} else {
// Display a message indicating that the email is not verified
// Display a message indicating that the email is not verified
await showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: const Text('Email Not Verified'),
content: const Text('Please verify your email to login.'),
actions: [
TextButton(
onPressed: () {
Navigator.of(context).pop();
},
child: const Text('OK'),
),
],
);
},
);
// Log out the user
await _auth.signOut();
}
if (defaultTargetPlatform == TargetPlatform.macOS ||
defaultTargetPlatform == TargetPlatform.linux ||
defaultTargetPlatform == TargetPlatform.windows) {
listener.cancel();
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: const Text('Email Not Verified'),
content: const Text(
'Please verify your email to login. Check your email'),
actions: [
TextButton(
onPressed: () {
Navigator.of(context).pop();
},
child: const Text('OK'),
),
],
);
},
);
// Log out the user
await _auth.signOut();
}
Navigator.pop(context);
}
});
}
Expand Down Expand Up @@ -164,50 +167,51 @@ class _LoginPageState extends State<LoginPage> {
ElevatedButton(
onPressed: () async {
try {
if (_auth.currentUser == null) {
_auth.signInWithEmailAndPassword(
email: _emailController.text,
password: _passwordController.text,
);
} else {
if (_auth.currentUser!.emailVerified) {
// Allow login if email is verified
_auth.signInWithEmailAndPassword(
email: _emailController.text,
password: _passwordController.text,
);
} else {
// Display message if email is not verified
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: const Text('Email Not Verified'),
content: const Text('Please verify your email to login.'),
actions: [
TextButton(
onPressed: () {
Navigator.of(context).pop();
},
child: const Text('OK'),
),
],
);
if (_auth.currentUser == null) {
_auth.signInWithEmailAndPassword(
email: _emailController.text,
password: _passwordController.text,
);
} else {
if (_auth.currentUser!.emailVerified) {
// Allow login if email is verified
_auth.signInWithEmailAndPassword(
email: _emailController.text,
password: _passwordController.text,
);
} else {
// Display message if email is not verified
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: const Text('Register Success'),
content: const Text(
'Please verify your email to login. Check your email'),
actions: [
TextButton(
onPressed: () {
Navigator.of(context).pop();
},
child: const Text('OK'),
),
],
);
},
);
}
}
} on Exception catch (e) {
if (mounted) {
setState(() {
_isError = true;
_loggedInEmail = 'Error: ${e.toString()}';
});
}
}
},
);
}
}
} on Exception catch (e) {
if (mounted) {
setState(() {
_isError = true;
_loggedInEmail = 'Error: ${e.toString()}';
});
}
}
},
child: const Text('Login'),
),
child: const Text('Login'),
),
if (_isError) Text(_loggedInEmail) else const Text('Not logged in'),
Padding(
padding: const EdgeInsets.symmetric(vertical: 8.0),
Expand Down

0 comments on commit eeb488b

Please sign in to comment.