1
0
mirror of https://gitlab.com/comunic/comunicmobile synced 2024-11-22 12:59:21 +00:00
comunicmobile/lib/ui/routes/password_reset_route.dart

141 lines
4.0 KiB
Dart
Raw Normal View History

2020-05-03 14:55:00 +00:00
import 'package:comunic/helpers/account_helper.dart';
2021-02-18 17:58:47 +00:00
import 'package:comunic/models/res_check_password_reset_token.dart';
2020-05-03 14:55:00 +00:00
import 'package:comunic/ui/dialogs/input_new_password_dialog.dart';
import 'package:comunic/ui/widgets/async_screen_widget.dart';
2021-04-17 09:46:57 +00:00
import 'package:comunic/ui/widgets/login_route_container.dart';
2021-02-18 17:58:47 +00:00
import 'package:comunic/ui/widgets/new_password_input_widget.dart';
2020-05-03 14:55:00 +00:00
import 'package:comunic/ui/widgets/safe_state.dart';
import 'package:comunic/utils/intl_utils.dart';
import 'package:comunic/utils/ui_utils.dart';
import 'package:flutter/material.dart';
/// Reset user password route
///
/// @author Pierre HUBERT
class PasswordResetRoute extends StatelessWidget {
final String token;
const PasswordResetRoute({
Key? key,
required this.token,
2022-03-11 16:02:06 +00:00
}) : super(key: key);
2020-05-03 14:55:00 +00:00
@override
Widget build(BuildContext context) {
2021-04-17 09:46:57 +00:00
return LoginRouteContainer(
2021-04-17 09:26:37 +00:00
child: Scaffold(
appBar: AppBar(
title: Text(tr("Change your password")!),
2021-04-17 09:26:37 +00:00
),
body: _PasswordResetBody(token: token),
2020-05-03 14:55:00 +00:00
),
);
}
}
class _PasswordResetBody extends StatefulWidget {
final String token;
const _PasswordResetBody({Key? key, required this.token})
2022-03-11 16:02:06 +00:00
: super(key: key);
2020-05-03 14:55:00 +00:00
@override
__PasswordResetBodyState createState() => __PasswordResetBodyState();
}
enum _Status { BEFORE_CHANGE, WHILE_CHANGE, AFTER_CHANGE }
class __PasswordResetBodyState extends SafeState<_PasswordResetBody> {
final _key = GlobalKey<AsyncScreenWidgetState>();
var _status = _Status.BEFORE_CHANGE;
late ResCheckPasswordToken _tokenInfo;
2020-05-03 14:55:00 +00:00
void _setStatus(_Status s) => setState(() => _status = s);
Future<void> _validateToken() async {
_status = _Status.BEFORE_CHANGE;
2021-02-18 17:58:47 +00:00
_tokenInfo = await AccountHelper.validatePasswordResetToken(widget.token);
2020-05-03 14:55:00 +00:00
}
@override
Widget build(BuildContext context) {
return AsyncScreenWidget(
key: _key,
onReload: _validateToken, // The first time, we validate the token
onBuild: _buildBody,
errorMessage: tr(
"Could not validate your password reset token! Maybe it has expired now...")!,
2020-05-03 14:55:00 +00:00
);
}
Widget _buildBody() {
// The user has not requested yet to change his password
if (_status == _Status.BEFORE_CHANGE)
return Center(
child: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Text(tr("You can choose a new password.")!),
2021-03-13 14:28:34 +00:00
OutlinedButton(
2020-05-03 14:55:00 +00:00
onPressed: _changePassword,
child: Text(tr("Choose a new password")!),
2020-05-03 14:55:00 +00:00
)
],
));
// Password is being changed
if (_status == _Status.WHILE_CHANGE) return buildCenteredProgressBar();
// Password was successfully changed!
if (_status == _Status.AFTER_CHANGE)
return Center(
child: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Text(
tr("Congratulations! Your password has now been successfully changed!")!,
2020-05-03 14:55:00 +00:00
textAlign: TextAlign.center,
),
2021-03-13 14:28:34 +00:00
OutlinedButton(
2020-05-03 14:55:00 +00:00
onPressed: _quitScreen,
child: Text(tr("Login")!),
2020-05-03 14:55:00 +00:00
)
],
));
throw Exception("Unreachable statement!");
}
/// Ask the user to specify his new password
void _changePassword() async {
try {
// Ask for new password
2021-02-18 17:58:47 +00:00
final newPass = await showInputNewPassword(
context: context,
userInfo: UserInfoForPassword(
firstName: _tokenInfo.firstName,
lastName: _tokenInfo.lastName,
email: _tokenInfo.email,
),
);
2020-05-03 14:55:00 +00:00
if (newPass == null) return;
_setStatus(_Status.WHILE_CHANGE);
// Apply it
await AccountHelper.changeAccountPassword(widget.token, newPass);
_setStatus(_Status.AFTER_CHANGE);
} catch (e, s) {
print("Could not change user password! $e\n$s");
showSimpleSnack(context, tr("Could not change your password!")!);
2020-05-03 14:55:00 +00:00
// We start everything again
_key.currentState!.refresh();
2020-05-03 14:55:00 +00:00
}
}
/// Go back to login screen
void _quitScreen() => Navigator.of(context).pop();
}