Skip to content

Commit

Permalink
feat(JordyHers-org#226) : Implement the possibility to `reset passwor…
Browse files Browse the repository at this point in the history
…d` in `forgot password` section
  • Loading branch information
bhoomikshetty committed Oct 31, 2023
1 parent 45b41ca commit d4f8596
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 0 deletions.
9 changes: 9 additions & 0 deletions lib/app/features/sign_in/email_sign_in_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,15 @@ class EmailSignInBloc {
}
}


Future<bool> forgotPassword(String email) async{
try {
return await auth.forgotPassword(email);
} catch (e) {
rethrow;
}
}

void toggleFormType() {
final formType = _model.formType == EmailSignInFormType.signIn
? EmailSignInFormType.register
Expand Down
46 changes: 46 additions & 0 deletions lib/app/features/sign_in/email_sign_in_form_bloc_based.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import 'package:times_up_flutter/app/features/sign_in/email_sign_in_model.dart';
import 'package:times_up_flutter/services/auth.dart';
import 'package:times_up_flutter/theme/theme.dart';
import 'package:times_up_flutter/widgets/jh_form_submit_button.dart';
import 'package:times_up_flutter/widgets/show_alert_dialog.dart';
import 'package:times_up_flutter/widgets/show_exeption_alert.dart';

class EmailSignInFormBlocBased extends StatefulWidget {
Expand Down Expand Up @@ -74,6 +75,37 @@ class _EmailSignInFormBlocBasedState extends State<EmailSignInFormBlocBased> {
}
}

Future<void>? _forgotPassword(EmailSignInModel model, String email) async {
try {
if (model.canResetPassword) {
final emailSent = await widget.bloc.forgotPassword(email);
if (emailSent) {
// ignore: use_build_context_synchronously
await showAlertDialog(
context,
title: 'Password reset email sent to $email',
content: 'Please check your email',
defaultActionText: 'OK',
);
} else {
// ignore: use_build_context_synchronously
await showAlertDialog(
context,
title: 'Password reset failed',
content: 'There was some issue',
defaultActionText: 'OK',
);
}
}
} on FirebaseAuthException catch (e) {
await showExceptionAlertDialog(
context,
title: model.signInFailedText,
exception: e,
);
}
}

void _emailEditingComplete(EmailSignInModel model) {
final newFocus = model.emailValidator.isValid(model.email)
? _passwordFocusNode
Expand Down Expand Up @@ -106,6 +138,8 @@ class _EmailSignInFormBlocBasedState extends State<EmailSignInFormBlocBased> {
const SizedBox(height: 8),
_buildPasswordTextField(model),
const SizedBox(height: 8),
_buildForgotPassword(model),
const SizedBox(height: 8),
FormSubmitButton(
onPressed: () => model.canSubmitRegister || model.canSubmitSignIn
? _submit(model)
Expand Down Expand Up @@ -203,6 +237,18 @@ class _EmailSignInFormBlocBasedState extends State<EmailSignInFormBlocBased> {
);
}

Widget _buildForgotPassword(EmailSignInModel model) {
return GestureDetector(
onTap: () => _forgotPassword(model, _emailController.text),
child: Container(
margin: const EdgeInsets.symmetric(vertical: 8),
width: double.infinity,
alignment: Alignment.centerRight,
child: const Text('Forgot Password?'),
),
);
}

@override
Widget build(BuildContext context) {
return SingleChildScrollView(
Expand Down
4 changes: 4 additions & 0 deletions lib/app/features/sign_in/email_sign_in_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ class EmailSignInModel with EmailAndPasswordValidators {
!isLoading;
}

bool get canResetPassword {
return emailValidator.isValid(email);
}

bool get canSubmitRegister {
return emailValidator.isValid(email) &&
passwordValidator.isValid(password) &&
Expand Down
12 changes: 12 additions & 0 deletions lib/services/auth.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ abstract class AuthBase {
String name,
String surname,
);

Future<bool> forgotPassword(String email);
}

class Auth implements AuthBase {
Expand Down Expand Up @@ -186,4 +188,14 @@ class Auth implements AuthBase {
_isFirstLogin = false;
await _firebaseAuth.signOut();
}

@override
Future<bool> forgotPassword(String email) async {
try {
await _firebaseAuth.sendPasswordResetEmail(email: email);
return true;
} catch (e) {
return false;
}
}
}

0 comments on commit d4f8596

Please sign in to comment.