D1APP-111 reset password

This commit is contained in:
MohammadTaha Basiri 2022-03-09 18:05:08 +03:30
parent d7eff24d29
commit 71a5bda8c9
5 changed files with 124 additions and 46 deletions

View File

@ -10,6 +10,7 @@ import 'package:didvan/utils/action_sheet.dart';
class UserProvider extends CoreProvier { class UserProvider extends CoreProvier {
late User user; late User user;
bool isAuthenticated = false;
static final List<MapEntry> _radarMarkQueue = []; static final List<MapEntry> _radarMarkQueue = [];
static final List<MapEntry> _newsMarkQueue = []; static final List<MapEntry> _newsMarkQueue = [];
@ -24,6 +25,7 @@ class UserProvider extends CoreProvier {
} }
Future<void> getUserInfo() async { Future<void> getUserInfo() async {
isAuthenticated = true;
final RequestService service = RequestService(RequestHelper.userInfo); final RequestService service = RequestService(RequestHelper.userInfo);
await service.httpGet(); await service.httpGet();
if (service.isSuccess) { if (service.isSuccess) {

View File

@ -11,6 +11,7 @@ class RequestHelper {
static const String _baseDirectUrl = _baseUserUrl + '/direct'; static const String _baseDirectUrl = _baseUserUrl + '/direct';
static const String confirmUsername = _baseUserUrl + '/confirmUsername'; static const String confirmUsername = _baseUserUrl + '/confirmUsername';
static const String changePassword = _baseUserUrl + '/changePassword';
static const String login = _baseUserUrl + '/login'; static const String login = _baseUserUrl + '/login';
static const String directs = _baseUserUrl + '/direct'; static const String directs = _baseUserUrl + '/direct';
static const String userInfo = _baseUserUrl + '/info'; static const String userInfo = _baseUserUrl + '/info';

View File

@ -10,6 +10,7 @@ class AuthenticationState extends CoreProvier {
int _currentPageIndex = 0; int _currentPageIndex = 0;
String username = ''; String username = '';
String password = ''; String password = '';
String _verificationCode = '';
set currentPageIndex(int value) { set currentPageIndex(int value) {
_currentPageIndex = value; _currentPageIndex = value;
@ -58,28 +59,62 @@ class AuthenticationState extends CoreProvier {
} }
Future<void> sendOtpToken() async { Future<void> sendOtpToken() async {
final service = RequestService(RequestHelper.otp + '?username=$username'); final service = RequestService(
RequestHelper.otp + '?username=$username',
useAutherization: false,
);
await service.httpGet(); await service.httpGet();
} }
Future<bool> verifyOtpToken(String token) async { Future<bool> verifyOtpToken(String token) async {
appState = AppState.isolatedBusy; appState = AppState.isolatedBusy;
final service = RequestService(
final service = RequestService(RequestHelper.otp, body: { RequestHelper.otp,
'code': token, body: {
'username': username, 'code': token,
}); 'username': username,
},
useAutherization: false,
);
await service.post(); await service.post();
appState = AppState.idle; appState = AppState.idle;
if (service.isSuccess) { if (service.isSuccess) {
_verificationCode = token;
currentPageIndex++; currentPageIndex++;
return true; return true;
} }
ActionSheetUtils.showAlert(AlertData( ActionSheetUtils.showAlert(
message: service.isSuccess AlertData(
? service.result['message'] message: service.isSuccess
: 'کد وارد شده صحیح نمی‌باشد', ? service.result['message']
)); : 'کد وارد شده صحیح نمی‌باشد',
),
);
return false;
}
Future<bool> resetPassword(String password) async {
appState = AppState.isolatedBusy;
final service = RequestService(
RequestHelper.changePassword,
body: {
'code': _verificationCode,
'username': username,
'password': password,
},
useAutherization: false,
);
await service.put();
if (service.isSuccess) {
ActionSheetUtils.showAlert(
AlertData(
message: 'کلمه عبور با موفقیت تغییر کرد',
aLertType: ALertType.success,
),
);
return true;
}
ActionSheetUtils.showAlert(AlertData(message: service.errorMessage));
return false; return false;
} }
} }

View File

@ -1,39 +1,75 @@
import 'package:didvan/providers/user_provider.dart';
import 'package:didvan/routes/routes.dart';
import 'package:didvan/views/authentication/authentication_state.dart';
import 'package:didvan/views/authentication/widgets/authentication_layout.dart'; import 'package:didvan/views/authentication/widgets/authentication_layout.dart';
import 'package:didvan/views/widgets/didvan/button.dart'; import 'package:didvan/views/widgets/didvan/button.dart';
import 'package:didvan/views/widgets/didvan/text_field.dart'; import 'package:didvan/views/widgets/didvan/text_field.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
class ResetPassword extends StatelessWidget { class ResetPassword extends StatefulWidget {
const ResetPassword({Key? key}) : super(key: key); const ResetPassword({Key? key}) : super(key: key);
@override
State<ResetPassword> createState() => _ResetPasswordState();
}
class _ResetPasswordState extends State<ResetPassword> {
String _password = '';
String _passwordConfirmation = '';
final _formKey = GlobalKey<FormState>();
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return AuthenticationLayout( return Form(
children: [ key: _formKey,
DidvanTextField( child: AuthenticationLayout(
title: 'کلمه عبور جدید', children: [
onChanged: (value) {}, DidvanTextField(
hintText: 'کلمه عبور جدید', title: 'کلمه عبور جدید',
obsecureText: true, onChanged: (value) => _password = value,
), validator: (value) => value.length < 8
const SizedBox( ? 'کلمه عبور نمی‌تواند کمتر از 8 کاراکتر داشته باشد'
height: 16, : null,
), hintText: 'کلمه عبور جدید',
DidvanTextField( obsecureText: true,
title: 'کلمه عبور جدید', ),
onChanged: (value) {}, const SizedBox(
hintText: 'کلمه عبور جدید', height: 16,
obsecureText: true, ),
), DidvanTextField(
const Spacer(), title: 'تکرار کلمه عبور جدید',
DidvanButton( onChanged: (value) => _passwordConfirmation = value,
onPressed: () {}, validator: (value) => _passwordConfirmation != _password
title: 'تغییر رمز عبور', ? 'تکرار کلمه عبور با کلمه عبور یکسان نیست'
), : null,
const SizedBox( hintText: 'تکرار کلمه عبور جدید',
height: 48, obsecureText: true,
), ),
], const Spacer(),
DidvanButton(
onPressed: () async {
if (!_formKey.currentState!.validate()) return;
final result = await context
.read<AuthenticationState>()
.resetPassword(_password);
if (result && context.read<UserProvider>().isAuthenticated) {
Navigator.of(context).pop();
Navigator.of(context).pop();
return;
}
Navigator.of(context).pushNamed(
Routes.authenticaion,
arguments: false,
);
},
title: 'تغییر رمز عبور',
),
const SizedBox(
height: 48,
),
],
),
); );
} }
} }

View File

@ -124,14 +124,18 @@ class _VerificationState extends State<Verification> {
_timer?.cancel(); _timer?.cancel();
_isResendButtonEnabled = false; _isResendButtonEnabled = false;
_timer = Timer.periodic(const Duration(seconds: 1), (timer) { _timer = Timer.periodic(const Duration(seconds: 1), (timer) {
setState(() { try {
_secondsRemaining -= 1; setState(() {
if (_secondsRemaining == 0) { _secondsRemaining -= 1;
_isResendButtonEnabled = true; if (_secondsRemaining == 0) {
_secondsRemaining = 129; _isResendButtonEnabled = true;
_timer?.cancel(); _secondsRemaining = 129;
} _timer?.cancel();
}); }
});
} catch (e) {
_timer?.cancel();
}
}); });
setState(() {}); setState(() {});
} }