forget password added

This commit is contained in:
mohammad
2024-06-25 10:54:42 +03:00
parent bc3cd66096
commit 08d3ff3e0f
9 changed files with 445 additions and 4 deletions

View File

@ -22,11 +22,14 @@ 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>();
bool isPasswordVisible = false;
bool showValidationMessage = false;
@ -107,6 +110,18 @@ class AuthCubit extends Cubit<AuthState> {
}
}
String? reEnterPasswordCheckForgetPass(String? value) {
passwordValidator(value);
if (newPassword == value) {
// if (signUpFormKey.currentState != null) {
// signUpFormKey.currentState!.save();
// }
return null;
} else {
return 'Passwords do not match';
}
}
String? emailAddressValidator(String? value) {
if (value != null && value.isNotEmpty && value != "") {
if (checkValidityOfEmail(value)) {
@ -303,4 +318,18 @@ 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,180 @@
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/view/otp_view.dart';
import 'package:syncrow_app/utils/context_extension.dart';
import '../../../generated/assets.dart';
import '../../../navigation/routing_constants.dart';
import '../../../utils/resource_manager/constants.dart';
import '../../../utils/resource_manager/font_manager.dart';
import '../../../utils/resource_manager/styles_manager.dart';
import '../../shared_widgets/default_button.dart';
import '../../shared_widgets/text_widgets/body_medium.dart';
import '../../shared_widgets/text_widgets/title_medium.dart';
import '../bloc/auth_cubit.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,),
));
// Navigator.popAndPushNamed(context, Routes.otpRoute);
}
},
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(
// autovalidateMode: AutovalidateMode.disabled,
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"),
),
SizedBox(height: 50,),
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();
}
}
},
),
),
],
)
// const LoginForm(),
// const LoginDivider(),
// const LoginWithGoogleFacebook(),
// const DontHaveAnAccount(),
],
),
),
],
),
),
),)
),
)
],
),
);
},
);
}
}

View File

@ -0,0 +1,201 @@
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_svg/svg.dart';
import 'package:syncrow_app/features/auth/view/login_view.dart';
import 'package:syncrow_app/utils/context_extension.dart';
import '../../../generated/assets.dart';
import '../../../utils/helpers/custom_page_route.dart';
import '../../../utils/resource_manager/constants.dart';
import '../../../utils/resource_manager/font_manager.dart';
import '../../../utils/resource_manager/styles_manager.dart';
import '../../shared_widgets/default_button.dart';
import '../../shared_widgets/text_widgets/body_medium.dart';
import '../../shared_widgets/text_widgets/title_medium.dart';
import '../bloc/auth_cubit.dart';
class CreateNewPasswordPage extends StatefulWidget {
const CreateNewPasswordPage({super.key,});
@override
State<CreateNewPasswordPage> createState() => _CreateNewPasswordPageState();
}
class _CreateNewPasswordPageState extends State<CreateNewPasswordPage> {
final createNewPasswordKey = GlobalKey<FormState>();
@override
Widget build(BuildContext context) {
return BlocConsumer<AuthCubit, AuthState>(
listener: (context, state) {
if (state is AuthForgetPassSuccess) {
Future.delayed(const Duration(seconds: 2), () {
Navigator.pushReplacement(
context,
CustomPageRoute(builder: (context) => const LoginView()),
);
});
}
},
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 account',
style: context.titleMedium.copyWith(
fontWeight: FontsManager.extraBold,
color: Colors.white,
),
),
const SizedBox(
height: 20,
),
Form(
key: createNewPasswordKey,
// autovalidateMode: AutovalidateMode.disabled,
child: SingleChildScrollView(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const SizedBox(height: 16),
const BodyMedium(
text: "Password",
fontColor: Colors.white,
),
TextFormField(
// autovalidateMode: AutovalidateMode.disabled,
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 (createNewPasswordKey.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});
@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==true?
Navigator.push(context, MaterialPageRoute(builder: (context) => const CreateNewPasswordPage(),)):
Navigator.popAndPushNamed(context, Routes.homeRoute);
}
if (state is ResendOtpSuccess) {

View File

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

View File

@ -40,4 +40,14 @@ class AuthenticationAPI {
expectedResponseModel: (json) => json['data']);
return response;
}
static Future<Map<String, dynamic>> forgetPassword({required String email,required String password ,}) async {
Map<String, dynamic> params = {"email": email, "password": password,};
final response = await HTTPService().post(
path: ApiEndpoints.forgetPassword,
body: params,
showServerMessage: false,
expectedResponseModel: (json) => json['data']);
return response;
}
}