Merge pull request #26 from SyncrowIOT/forgot_pass

forget password added
This commit is contained in:
mohammadnemer1
2024-06-25 15:53:16 +03:00
committed by GitHub
10 changed files with 453 additions and 20 deletions

View File

@ -22,16 +22,19 @@ class AuthCubit extends Cubit<AuthState> {
String fullName = '';
String email = '';
String forgetPasswordEmail = '';
String signUpPassword = '';
String newPassword = '';
String maskedEmail = '';
String otpCode = '';
final loginFormKey = GlobalKey<FormState>();
final signUpFormKey = GlobalKey<FormState>();
final checkEmailFormKey = GlobalKey<FormState>();
final createNewPasswordKey = GlobalKey<FormState>();
bool isPasswordVisible = false;
bool showValidationMessage = false;
static GlobalKey<FormState> formKey = GlobalKey<FormState>();
void changePasswordVisibility() {
isPasswordVisible = !isPasswordVisible;
emit(AuthPasswordVisibilityChanged());
@ -98,9 +101,15 @@ class AuthCubit extends Cubit<AuthState> {
String? reEnterPasswordCheck(String? value) {
passwordValidator(value);
if (signUpPassword == value) {
// if (signUpFormKey.currentState != null) {
// signUpFormKey.currentState!.save();
// }
return null;
} else {
return 'Passwords do not match';
}
}
String? reEnterPasswordCheckForgetPass(String? value) {
passwordValidator(value);
if (newPassword == value) {
return null;
} else {
return 'Passwords do not match';
@ -228,24 +237,21 @@ class AuthCubit extends Cubit<AuthState> {
}
}
verifyOtp() async {
verifyOtp(bool isForgotPass) async {
emit(AuthLoginLoading());
try {
final response = await AuthenticationAPI.verifyPassCode(
body: {'email': email, 'type': 'VERIFICATION', 'otpCode': otpCode});
if (response['statusCode'] == 200) {
emailController.text = email;
passwordController.text = signUpPassword;
await login();
if (!isForgotPass) {
emailController.text = email;
passwordController.text = signUpPassword;
await login();
}
emit(AuthOtpSuccess());
} else {
emit(AuthLoginError(message: 'Something went wrong'));
}
// if (otpCode == '654321') {
// emit(AuthOtpSuccess());
// } else {
// emit(AuthLoginError(message: 'Otp is not correct'));
// }
} catch (failure) {
emit(AuthLoginError(message: failure.toString()));
return;
@ -303,4 +309,14 @@ class AuthCubit extends Cubit<AuthState> {
emit(AuthTokenError(message: "Something went wrong"));
}
}
sendToForgetPassword({required String password}) async {
try {
emit(AuthForgetPassLoading());
await AuthenticationAPI.forgetPassword(email: email, password: password);
emit(AuthForgetPassSuccess());
} catch (_) {
emit(AuthForgetPassError(message: 'Something went wrong'));
}
}
}

View File

@ -51,3 +51,16 @@ class AuthTokenSuccess extends AuthSuccess {}
class AuthTokenError extends AuthError {
AuthTokenError({required super.message, super.code});
}
//ForgetPassword log states
class AuthForgetPassLoading extends AuthLoading {}
class AuthForgetPassSuccess extends AuthSuccess {}
class AuthForgetPassError extends AuthError {
AuthForgetPassError({required super.message, super.code});
}

View File

@ -0,0 +1,202 @@
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_svg/svg.dart';
import 'package:syncrow_app/features/auth/bloc/auth_cubit.dart';
import 'package:syncrow_app/features/auth/view/otp_view.dart';
import 'package:syncrow_app/features/shared_widgets/default_button.dart';
import 'package:syncrow_app/features/shared_widgets/text_widgets/body_large.dart';
import 'package:syncrow_app/features/shared_widgets/text_widgets/body_medium.dart';
import 'package:syncrow_app/features/shared_widgets/text_widgets/title_medium.dart';
import 'package:syncrow_app/generated/assets.dart';
import 'package:syncrow_app/utils/context_extension.dart';
import 'package:syncrow_app/utils/resource_manager/constants.dart';
import 'package:syncrow_app/utils/resource_manager/font_manager.dart';
import 'package:syncrow_app/utils/resource_manager/styles_manager.dart';
class checkEmailPage extends StatelessWidget {
const checkEmailPage({super.key});
@override
Widget build(BuildContext context) {
final formKey = AuthCubit.get(context).checkEmailFormKey;
SystemChrome.setSystemUIOverlayStyle(const SystemUiOverlayStyle(
statusBarBrightness: Brightness.light, statusBarIconBrightness: Brightness.light));
return BlocConsumer<AuthCubit, AuthState>(
listener: (context, state) {
if (state is AuthError) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(state.message),
),
);
} else if (state is AuthSignUpSuccess) {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => const OtpView(
isForgetPage: true,
),
));
}
},
builder: (context, state) {
return Scaffold(
body: Stack(
children: [
Container(
width: MediaQuery.sizeOf(context).width,
height: MediaQuery.sizeOf(context).height,
decoration: const BoxDecoration(
image: DecorationImage(
image: AssetImage(
Assets.assetsImagesBackground,
),
fit: BoxFit.cover,
),
),
),
Container(
width: MediaQuery.sizeOf(context).width,
height: MediaQuery.sizeOf(context).height,
decoration: const BoxDecoration(
image: DecorationImage(
image: AssetImage(Assets.assetsImagesVector),
fit: BoxFit.cover,
opacity: 0.9,
),
),
),
SafeArea(
child: Padding(
padding: const EdgeInsets.only(
right: Constants.defaultPadding,
left: Constants.defaultPadding,
top: Constants.defaultPadding,
),
child: Form(
key: formKey,
child: SingleChildScrollView(
child: Center(
child: Column(
children: [
Center(
child: SvgPicture.asset(
Assets.assetsImagesLogo,
width: 160,
),
),
Center(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
SizedBox(
height: MediaQuery.sizeOf(context).height / 5.5,
),
TitleMedium(
text: 'Forgot password?',
style: context.titleMedium.copyWith(
fontWeight: FontsManager.extraBold,
color: Colors.white,
),
),
const SizedBox(
height: 20,
),
const BodyMedium(
text: "Enter email address",
fontColor: Colors.white,
),
TextFormField(
textInputAction: TextInputAction.done,
keyboardType: TextInputType.text,
scrollPadding: EdgeInsets.zero,
autocorrect: false,
enableSuggestions: false,
autofillHints: const [AutofillHints.email],
validator: AuthCubit.get(context).emailAddressValidator,
onTapOutside: (event) {
FocusScope.of(context).unfocus();
},
onChanged: (value) {
AuthCubit.get(context).email = value;
},
decoration: defaultInputDecoration(context,
hint: "Example@email.com"),
),
const SizedBox(
height: 20,
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Expanded(
child: DefaultButton(
isDone: state is AuthLoginSuccess,
isLoading: state is AuthLoading,
customButtonStyle: ButtonStyle(
backgroundColor: MaterialStateProperty.all(
Colors.black.withOpacity(.25),
),
foregroundColor: MaterialStateProperty.all(
Colors.white,
),
),
child: const Text(
'Send Code',
),
onPressed: () {
AuthCubit.get(context).showValidationMessage = true;
if (formKey.currentState!.validate()) {
if ((state is! AuthLoading)) {
AuthCubit.get(context).sendOtp();
FocusScope.of(context).unfocus();
}
}
},
),
),
],
),
Padding(
padding: EdgeInsets.only(
top: MediaQuery.sizeOf(context).height / 5.5),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
BodyLarge(
text: "Do you have an account? ",
style:
context.displaySmall.copyWith(color: Colors.white),
),
TextButton(
onPressed: () {
Navigator.of(context).pop();
},
child: BodyLarge(
text: "Sign in",
style: context.displaySmall.copyWith(
color: Colors.black,
fontWeight: FontsManager.bold,
),
),
),
],
),
),
],
),
),
],
),
),
),
)),
)
],
),
);
},
);
}
}

View File

@ -0,0 +1,186 @@
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_svg/svg.dart';
import 'package:syncrow_app/features/auth/bloc/auth_cubit.dart';
import 'package:syncrow_app/features/shared_widgets/default_button.dart';
import 'package:syncrow_app/features/shared_widgets/text_widgets/body_medium.dart';
import 'package:syncrow_app/features/shared_widgets/text_widgets/title_medium.dart';
import 'package:syncrow_app/generated/assets.dart';
import 'package:syncrow_app/utils/context_extension.dart';
import 'package:syncrow_app/utils/resource_manager/constants.dart';
import 'package:syncrow_app/utils/resource_manager/font_manager.dart';
import 'package:syncrow_app/utils/resource_manager/styles_manager.dart';
class CreateNewPasswordPage extends StatelessWidget {
const CreateNewPasswordPage({super.key,});
@override
Widget build(BuildContext context) {
final formKey = AuthCubit.get(context).createNewPasswordKey;
return BlocConsumer<AuthCubit, AuthState>(
listener: (context, state) {
if (state is AuthForgetPassSuccess) {
Navigator.of(context).pop();
}
},
builder: (context, state) {
return Scaffold(
body: Stack(
children: [
Container(
width: MediaQuery.sizeOf(context).width,
height: MediaQuery.sizeOf(context).height,
padding: const EdgeInsets.symmetric(vertical: 24),
decoration: const BoxDecoration(
image: DecorationImage(
image: AssetImage(
Assets.assetsImagesBackground,
),
fit: BoxFit.cover,
),
),
),
Container(
width: MediaQuery.sizeOf(context).width,
height: MediaQuery.sizeOf(context).height,
decoration: const BoxDecoration(
image: DecorationImage(
image: AssetImage(Assets.assetsImagesVector),
fit: BoxFit.cover,
opacity: 0.9,
),
),
),
SafeArea(
child: Padding(
padding: const EdgeInsets.only(
right: Constants.defaultPadding,
left: Constants.defaultPadding,
top: Constants.defaultPadding,
),
child: SingleChildScrollView(
child: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Center(
child: SvgPicture.asset(
Assets.assetsImagesLogo,
width: 160,
),
),
const SizedBox(
height: 40,
),
TitleMedium(
text: 'Create new password',
style: context.titleMedium.copyWith(
fontWeight: FontsManager.extraBold,
color: Colors.white,
),
),
const SizedBox(
height: 20,
),
Form(
key: formKey,
child: SingleChildScrollView(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const SizedBox(height: 16),
const BodyMedium(
text: "Password",
fontColor: Colors.white,
),
TextFormField(
textInputAction: TextInputAction.done,
keyboardType: TextInputType.text,
scrollPadding: EdgeInsets.zero,
autocorrect: false,
autofillHints: const [AutofillHints.password],
validator: AuthCubit.get(context).passwordValidator,
onChanged: (value) {
AuthCubit.get(context).newPassword = value;
},
onTapOutside: (event) {
FocusScope.of(context).unfocus();
},
obscureText: !AuthCubit.get(context).isPasswordVisible,
decoration: defaultInputDecoration(context,
hint: "At least 8 characters"),
),
const SizedBox(height: 16),
const BodyMedium(
text: "Re-enter Password",
fontColor: Colors.white,
),
TextFormField(
autovalidateMode: AutovalidateMode.disabled,
textInputAction: TextInputAction.done,
keyboardType: TextInputType.text,
scrollPadding: EdgeInsets.zero,
autocorrect: false,
enableSuggestions: false,
autofillHints: const [AutofillHints.password],
onChanged: (value) {},
validator: AuthCubit.get(context).reEnterPasswordCheckForgetPass,
onTapOutside: (event) {
FocusScope.of(context).unfocus();
},
obscureText: !AuthCubit.get(context).isPasswordVisible,
decoration: defaultInputDecoration(context,
hint: "At least 8 characters"),
),
const SizedBox(height: 40),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Expanded(
child: DefaultButton(
isDone: state is AuthLoginSuccess,
isLoading: state is AuthLoading,
customButtonStyle: ButtonStyle(
backgroundColor: MaterialStateProperty.all(
Colors.black.withOpacity(.25),
),
foregroundColor: MaterialStateProperty.all(
Colors.white,
),
),
child: const Text(
'Confirm',
),
onPressed: () {
AuthCubit.get(context).showValidationMessage = true;
if (formKey.currentState!.validate()) {
if ((state is! AuthForgetPassLoading)) {
AuthCubit.get(context).sendToForgetPassword(password:AuthCubit.get(context).newPassword);
FocusScope.of(context).unfocus();
}
}
},
),
),
],
)
],
),
),
),
],
),
),
),
),
)
],
),
);
},
);
}
}

View File

@ -31,7 +31,8 @@ class LoginView extends StatelessWidget {
// content: Text(state.message),
// ),
// );
} else if (state is AuthLoginSuccess) {
}
else if (state is AuthLoginSuccess) {
Navigator.popAndPushNamed(context, Routes.homeRoute);
}
},

View File

@ -6,6 +6,7 @@ import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_svg/svg.dart';
import 'package:pin_code_fields/pin_code_fields.dart';
import 'package:syncrow_app/features/auth/bloc/auth_cubit.dart';
import 'package:syncrow_app/features/auth/view/create_new_password.dart';
import 'package:syncrow_app/features/shared_widgets/default_button.dart';
import 'package:syncrow_app/features/shared_widgets/text_widgets/title_medium.dart';
import 'package:syncrow_app/generated/assets.dart';
@ -17,7 +18,8 @@ import 'package:syncrow_app/utils/resource_manager/constants.dart';
import 'package:syncrow_app/utils/resource_manager/font_manager.dart';
class OtpView extends StatefulWidget {
const OtpView({super.key});
final bool isForgetPage;
const OtpView({super.key,this.isForgetPage=false});
@override
State<OtpView> createState() => _OtpViewState();
@ -119,6 +121,8 @@ class _OtpViewState extends State<OtpView> {
if (state is AuthOtpSuccess) {
Navigator.of(context).pop();
Navigator.of(context).pop();
widget.isForgetPage?
Navigator.push(context, MaterialPageRoute(builder: (context) => const CreateNewPasswordPage(),)):
Navigator.popAndPushNamed(context, Routes.homeRoute);
}
if (state is ResendOtpSuccess) {
@ -300,7 +304,7 @@ class _OtpViewState extends State<OtpView> {
),
onPressed: () {
if ((state is! AuthLoading)) {
AuthCubit.get(context).verifyOtp();
AuthCubit.get(context).verifyOtp(widget.isForgetPage);
FocusScope.of(context).unfocus();
}
},

View File

@ -1,6 +1,6 @@
import 'package:flutter/material.dart';
import 'package:syncrow_app/features/auth/view/check_email_page.dart';
import 'package:syncrow_app/features/shared_widgets/text_widgets/body_medium.dart';
import 'package:syncrow_app/navigation/routing_constants.dart';
import 'package:syncrow_app/utils/context_extension.dart';
class ForgetPassword extends StatelessWidget {
@ -15,7 +15,7 @@ class ForgetPassword extends StatelessWidget {
const Spacer(),
TextButton(
onPressed: () {
Navigator.popAndPushNamed(context, Routes.otpRoute);
Navigator.push(context, MaterialPageRoute(builder: (context) => const checkEmailPage(),));
},
child: BodyMedium(
text: "Forgot Password?",