-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a3e2348
commit dcb30fb
Showing
13 changed files
with
435 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import 'package:manga_nih/blocs/event_states/user_state.dart'; | ||
|
||
class UserForgotPasswordSend extends UserState {} | ||
|
||
class UserForgotPasswordSuccess extends UserState {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import 'dart:developer'; | ||
|
||
import 'package:firebase_auth/firebase_auth.dart'; | ||
import 'package:flutter_bloc/flutter_bloc.dart'; | ||
import 'package:manga_nih/blocs/event_states/event_states.dart'; | ||
import 'package:manga_nih/core/core.dart'; | ||
import 'package:manga_nih/models/models.dart'; | ||
|
||
class ForgotPasswordCubit extends Cubit<UserState> { | ||
final FirebaseAuth _firebaseAuth = FirebaseAuth.instance; | ||
|
||
ForgotPasswordCubit() : super(UserUninitialized()); | ||
|
||
Future<void> sendForgotPassword(String email) async { | ||
try { | ||
emit(UserLoading()); | ||
|
||
Uri uri = Uri.https(Constants.webDomain, '', { | ||
'time': DateTime.now().millisecondsSinceEpoch.toString(), | ||
}); | ||
|
||
await _firebaseAuth.sendPasswordResetEmail( | ||
email: email, | ||
actionCodeSettings: ActionCodeSettings( | ||
url: uri.toString(), | ||
androidPackageName: Constants.androidPackage, | ||
dynamicLinkDomain: Constants.dynamicLink, | ||
handleCodeInApp: true, | ||
androidInstallApp: true, | ||
iOSBundleId: Constants.iosPackage, | ||
), | ||
); | ||
|
||
emit(UserForgotPasswordSend()); | ||
} on FirebaseAuthException catch (e) { | ||
log(e.toString(), name: 'ForgotPasswordCubit - sendForgotPassword'); | ||
|
||
if (e.code == 'user-not-found') { | ||
SnackbarModel.custom(true, 'User not found, try again'); | ||
} | ||
|
||
emit(UserError()); | ||
} catch (e) { | ||
log(e.toString(), name: 'ForgotPasswordCubit - sendForgotPassword'); | ||
|
||
SnackbarModel.globalError(); | ||
|
||
emit(UserError()); | ||
} | ||
} | ||
|
||
Future<void> verifyCode(String code, String password) async { | ||
try { | ||
emit(UserLoading()); | ||
|
||
await _firebaseAuth.confirmPasswordReset( | ||
code: code, | ||
newPassword: password, | ||
); | ||
|
||
emit(UserForgotPasswordSuccess()); | ||
} on FirebaseAuthException catch (e) { | ||
log(e.toString(), name: 'ForgotPasswordCubit - verifyCode'); | ||
|
||
if (e.code == 'invalid-action-code') { | ||
SnackbarModel.custom( | ||
true, 'Invalid code, please resend verification email'); | ||
} | ||
|
||
emit(UserError()); | ||
} catch (e) { | ||
log(e.toString(), name: 'ForgotPasswordCubit - verifyCode'); | ||
|
||
SnackbarModel.globalError(); | ||
|
||
emit(UserError()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
import 'package:email_validator/email_validator.dart'; | ||
import 'package:flutter/material.dart'; | ||
import 'package:flutter_bloc/flutter_bloc.dart'; | ||
import 'package:manga_nih/blocs/blocs.dart'; | ||
import 'package:manga_nih/models/models.dart'; | ||
import 'package:manga_nih/ui/configs/pallette.dart'; | ||
import 'package:manga_nih/blocs/event_states/event_states.dart'; | ||
import 'package:manga_nih/ui/widgets/widgets.dart'; | ||
|
||
class ForgotPasswordScreen extends StatefulWidget { | ||
const ForgotPasswordScreen({Key? key}) : super(key: key); | ||
|
||
@override | ||
_ForgotPasswordScreenState createState() => _ForgotPasswordScreenState(); | ||
} | ||
|
||
class _ForgotPasswordScreenState extends State<ForgotPasswordScreen> { | ||
final GlobalKey<FormState> _key = GlobalKey(); | ||
final TextEditingController _emailController = TextEditingController(); | ||
final ForgotPasswordCubit _forgotPasswordCubit = ForgotPasswordCubit(); | ||
String? _emailErrorText; | ||
|
||
@override | ||
void initState() { | ||
// init bloc | ||
|
||
super.initState(); | ||
} | ||
|
||
@override | ||
void dispose() { | ||
_emailController.dispose(); | ||
|
||
super.dispose(); | ||
} | ||
|
||
void _sendCodeAction() { | ||
if (_key.currentState!.validate()) { | ||
// re-init error | ||
setState(() { | ||
_emailErrorText = null; | ||
}); | ||
|
||
String email = _emailController.text.trim(); | ||
|
||
if (!EmailValidator.validate(email)) { | ||
setState(() => _emailErrorText = 'Email invalid'); | ||
} else { | ||
_forgotPasswordCubit.sendForgotPassword(email); | ||
} | ||
} | ||
} | ||
|
||
void _blocListener(BuildContext context, UserState userState) { | ||
if (userState is UserForgotPasswordSend) { | ||
SnackbarModel.custom(false, 'Check your email'); | ||
} | ||
} | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
final Size screenSize = MediaQuery.of(context).size; | ||
|
||
return BlocProvider<ForgotPasswordCubit>( | ||
create: (_) => _forgotPasswordCubit, | ||
child: SafeArea( | ||
child: Scaffold( | ||
body: Center( | ||
child: ListView( | ||
physics: const BouncingScrollPhysics(), | ||
shrinkWrap: true, | ||
children: [ | ||
Center( | ||
child: Container( | ||
padding: const EdgeInsets.symmetric( | ||
vertical: 20.0, horizontal: 10.0), | ||
width: screenSize.width * 0.9, | ||
child: Form( | ||
key: _key, | ||
child: Column( | ||
children: [ | ||
Align( | ||
alignment: Alignment.topLeft, | ||
child: Text( | ||
'Forgot Password', | ||
style: Theme.of(context) | ||
.textTheme | ||
.headline5! | ||
.copyWith(color: Pallette.gradientEndColor), | ||
), | ||
), | ||
const SizedBox(height: 5.0), | ||
Align( | ||
alignment: Alignment.topLeft, | ||
child: Text( | ||
'Input your valid email', | ||
style: TextStyle(color: Colors.grey.shade700), | ||
), | ||
), | ||
const SizedBox(height: 25.0), | ||
InputField( | ||
icon: Icons.email, | ||
hintText: 'Email', | ||
controller: _emailController, | ||
errorText: _emailErrorText, | ||
), | ||
const SizedBox(height: 25.0), | ||
BlocConsumer<ForgotPasswordCubit, UserState>( | ||
listener: _blocListener, | ||
builder: (context, state) { | ||
if (state is UserLoading) { | ||
return const PrimaryButton.loading(); | ||
} | ||
|
||
return PrimaryButton( | ||
label: 'Send code', | ||
onTap: _sendCodeAction, | ||
); | ||
}, | ||
), | ||
], | ||
), | ||
), | ||
), | ||
), | ||
], | ||
), | ||
), | ||
), | ||
), | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.