Skip to content

Commit

Permalink
refractor auth popup
Browse files Browse the repository at this point in the history
  • Loading branch information
youngbryanyu committed Feb 22, 2024
1 parent 7b380d4 commit adbd4d9
Show file tree
Hide file tree
Showing 5 changed files with 151 additions and 82 deletions.
10 changes: 10 additions & 0 deletions frontend/lib/components/auth/auth_popup.dart
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,13 @@ class AuthPopup extends StatelessWidget {
);
}
}

/* Function which shows an auth popup */
void showAuthPopup(final context, String title, String message) {
showDialog(
context: context,
builder: (context) {
return AuthPopup(title: title, message: message);
},
);
}
2 changes: 2 additions & 0 deletions frontend/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ final ThemeData lightTheme = ThemeData(
fillColor: Colors.grey.shade300,
hintStyle: TextStyle(
color: Colors.grey.shade500,
fontSize: 16,
),
enabledBorder: OutlineInputBorder(
borderSide: BorderSide(
Expand Down Expand Up @@ -140,6 +141,7 @@ final ThemeData darkTheme = ThemeData(
fillColor: Colors.grey.shade700,
hintStyle: TextStyle(
color: Colors.grey.shade500,
fontSize: 16,
),
enabledBorder: OutlineInputBorder(
borderSide: BorderSide(
Expand Down
74 changes: 45 additions & 29 deletions frontend/lib/pages/auth/forgot_password_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,12 @@ class ResetPasswordPage extends ConsumerStatefulWidget {
class _ResetPasswordPageState extends ConsumerState<ResetPasswordPage> {
/* Text box controllers */
final emailController = TextEditingController();
final passwordController = TextEditingController();

/* whether to show password */
bool passwordVisible = false;

/* Sign in function */
Future<void> sendResetPasswordEmail() async {
// TODO: update
/* Show loading circle */
showDialog(
context: context,
Expand All @@ -37,16 +35,23 @@ class _ResetPasswordPageState extends ConsumerState<ResetPasswordPage> {
);

try {
/* Await sign in */
await FirebaseAuth.instance.signInWithEmailAndPassword(
/* Await send email */
await FirebaseAuth.instance.sendPasswordResetEmail(
email: emailController.text,
password: passwordController.text,
);

/* Pop loading circle */
if (mounted) {
/* Pop loading circle */
Navigator.pop(context);

/* Show popup and clear controller */
showAuthPopup(
context,
"Email sent successfully",
"Please check your email for steps on how to reset your password",
);
}
emailController.text = '';
} on FirebaseAuthException catch (error) {
/* Pop loading circle */
if (mounted) {
Expand All @@ -55,36 +60,47 @@ class _ResetPasswordPageState extends ConsumerState<ResetPasswordPage> {

/* Handle error codes */
if (error.code == 'too-many-requests') {
showErrorPopup(
'Account temporarily locked',
'Too many failed login attempts. Try again later or reset your password.',
);
} else if (error.code == 'user-disabled') {
showErrorPopup('Account locked', 'Your account is disabled.');
if (mounted) {
showAuthPopup(
context,
'Password reset failed',
'Too many recent password resets. Please try again later.',
);
}
} else if (error.code == 'operation-not-allowed') {
showErrorPopup(
'Login failed',
'Email-based login is currently disabled',
);
if (mounted) {
showAuthPopup(
context,
'Password reset failed',
'Password reset is currently disabled',
);
}
} else if (error.code == 'invalid-email' ||
error.code == 'missing-email') {
if (mounted) {
showAuthPopup(
context,
'Invalid email',
'Please enter a valid email address',
);
}
} else {
/* Group other errors together (e.g. invalid password, no user found, invalid email)*/
showErrorPopup('Login failed', 'Invalid login credentials');
if (mounted) {
showAuthPopup(
context, 'Unknown error occurred', 'Please try again later.');
}
}
} catch (error) {
showErrorPopup('Login failed', 'Server error occurred');
if (mounted) {
showAuthPopup(
context,
'Server error occurred',
'Please try again later.',
);
}
}
}

/* Shows an error message popup */
void showErrorPopup(String title, String message) {
showDialog(
context: context,
builder: (context) {
return AuthPopup(title: title, message: message);
},
);
}

@override
Widget build(BuildContext context) {
/* Get the screen height */
Expand Down
68 changes: 46 additions & 22 deletions frontend/lib/pages/auth/login_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -59,36 +59,60 @@ class _LoginPageState extends ConsumerState<LoginPage> {

/* Handle error codes */
if (error.code == 'too-many-requests') {
showErrorPopup(
'Account temporarily locked',
'Too many failed login attempts. Try again later or reset your password.',
);
if (mounted) {
showAuthPopup(
context,
'Account temporarily locked',
'Too many failed login attempts. Please try again later or reset your password.',
);
}
} else if (error.code == 'user-disabled') {
showErrorPopup('Account locked', 'Your account is disabled.');
if (mounted) {
showAuthPopup(
context,
'Account locked',
'Your account is disabled.',
);
}
} else if (error.code == 'operation-not-allowed') {
showErrorPopup(
'Login failed',
'Email-based login is currently disabled',
);
if (mounted) {
showAuthPopup(
context,
'Login failed',
'Email-based login is currently disabled',
);
}
} else if (error.code == 'wrong-password' ||
error.code == 'invalid-email' ||
error.code == 'user-not-found' ||
error.code == 'invalid-credential') {
if (mounted) {
showAuthPopup(
context,
'Login failed',
'Invalid login credentials',
);
}
} else {
/* Group other errors together (e.g. invalid password, no user found, invalid email)*/
showErrorPopup('Login failed', 'Invalid login credentials');
if (mounted) {
showAuthPopup(
context,
'Unknown error occurred',
'Please try again later',
);
}
}
} catch (error) {
showErrorPopup('Login failed', 'Server error occurred');
if (mounted) {
showAuthPopup(
context,
'Server error occurred',
'Please try again later',
);
}
}
}

/* Shows an error message popup */
void showErrorPopup(String title, String message) {
showDialog(
context: context,
builder: (context) {
return AuthPopup(title: title, message: message);
},
);
}

@override
Widget build(BuildContext context) {
/* Get the screen height */
Expand Down
79 changes: 48 additions & 31 deletions frontend/lib/pages/auth/register_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -65,49 +65,66 @@ class _RegisterPageState extends ConsumerState<RegisterPage> {
}

if (error.code == 'email-already-in-use') {
showPopup(
'Registration failed',
'The email is already taken. Try logging in. If you forgot your password you can reset it.',
);
if (mounted) {
showAuthPopup(
context,
'Email already taken',
'The email is already taken. Try logging in. If you forgot your password, you can reset it.',
);
}
} else if (error.code == 'invalid-email') {
showPopup('Registration failed', 'The email is invalid');
if (mounted) {
showAuthPopup(
context,
'Invalid email',
'Please enter a valid email address',
);
}
} else if (error.code == 'weak-password') {
showPopup(
'Registration failed',
'Your password must be at least 6 characters long',
);
if (mounted) {
showAuthPopup(
context,
'Invalid password',
'Your password must be at least 6 characters long',
);
}
} else if (error.code == 'operation-not-allowed') {
showPopup(
'Registration failed',
'Email-based sign up is currently disabled.',
);
if (mounted) {
showAuthPopup(
context,
'Registration failed',
'Email-based sign up is currently disabled.',
);
}
} else if (error.code == 'too-many-requests') {
showPopup(
'Registration failed',
'Too many sign up requests from this IP. Try again later.',
);
if (mounted) {
showAuthPopup(
context,
'Registration failed',
'Too many recent sign up requests. Please try again later.',
);
}
} else {
/* Group all other errors together */
showPopup(
if (mounted) {
showAuthPopup(
context,
'Registration failed',
'Unknown error occurred. Please try again later.',
);
}
}
} catch (error) {
if (mounted) {
showAuthPopup(
context,
'Registration failed',
'Unknown error occurred during sign up',
'Server error occurred. Please try again later.',
);
}
} catch (error) {
showPopup('Registration failed', 'Server error occurred');
}
}

/* Shows an error message popup */
void showPopup(String title, String message) {
showDialog(
context: context,
builder: (context) {
return AuthPopup(title: title, message: message);
},
);
}

@override
Widget build(BuildContext context) {
/* Get the screen height */
Expand Down

0 comments on commit adbd4d9

Please sign in to comment.