fluffychat/lib/pangea/user/utils/password_forgotten.dart
ggurdin 49e586a7ad
Fluffychat merge (#1685)
chore: Merge upstream changes

---------

Signed-off-by: Krille <c.kussowski@famedly.com>
Co-authored-by: krille-chan <christian-kussowski@posteo.de>
Co-authored-by: Krille <c.kussowski@famedly.com>
Co-authored-by: Linerly <linerly@proton.me>
Co-authored-by: Priit Jõerüüt <hwlate@joeruut.com>
Co-authored-by: 大王叫我来巡山 <hamburger2048@users.noreply.hosted.weblate.org>
Co-authored-by: fadelkon <fadelkon@posteo.net>
Co-authored-by: Aindriú Mac Giolla Eoin <aindriu80@gmail.com>
Co-authored-by: Edgars Andersons <Edgars+Weblate@gaitenis.id.lv>
Co-authored-by: josé m <correoxm@disroot.org>
Co-authored-by: Bezruchenko Simon <worcposj44@gmail.com>
Co-authored-by: Christian <christian-pauly@posteo.de>
Co-authored-by: - <hitekex@yandex.ru>
Co-authored-by: Angelo Schirinzi <Odi-3@users.noreply.hosted.weblate.org>
Co-authored-by: xabirequejo <xabi.rn@gmail.com>
Co-authored-by: Piotr Orzechowski <piotr@orzechowski.tech>
Co-authored-by: Rex_sa <rex.sa@pm.me>
Co-authored-by: Tewuzij <tenajeza@outlook.com>
Co-authored-by: goknarbahceli <goknarbahceli@proton.me>
Co-authored-by: தமிழ்நேரம் <anishprabu.t@gmail.com>
Co-authored-by: Erin <erin@erindesu.cz>
Co-authored-by: EpicKiwi <me@epickiwi.fr>
Co-authored-by: Christian Tietze <me@christiantietze.de>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
2025-02-03 12:36:46 -05:00

150 lines
6.2 KiB
Dart

import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/l10n.dart';
import 'package:matrix/matrix.dart';
import 'package:fluffychat/pages/login/login.dart';
import 'package:fluffychat/widgets/adaptive_dialogs/show_ok_cancel_alert_dialog.dart';
import 'package:fluffychat/widgets/future_loading_dialog.dart';
import '../../../widgets/matrix.dart';
extension PangeaPasswordForgotten on LoginController {
void pangeaPasswordForgotten() async {
final TextEditingController emailController = TextEditingController();
final TextEditingController newPasswordController = TextEditingController();
showDialog(
context: context,
useRootNavigator: false,
builder: (BuildContext context) => Scaffold(
backgroundColor: Colors.transparent,
body: AlertDialog(
title: Text(L10n.of(context).passwordForgotten),
content: Column(
mainAxisSize: MainAxisSize.min,
children: [
Text(L10n.of(context).enterAnEmailAddress),
const SizedBox(height: 12),
TextField(
controller: emailController,
decoration: InputDecoration(
hintText: L10n.of(context).enterAnEmailAddress,
),
),
],
),
actions: [
TextButton(
child: Text(L10n.of(context).cancel),
onPressed: () {
Navigator.of(context).pop();
return;
},
),
TextButton(
child: Text(L10n.of(context).ok),
onPressed: () async {
if (emailController.text == "") return;
final clientSecret =
DateTime.now().millisecondsSinceEpoch.toString();
final response = await showFutureLoadingDialog(
context: context,
future: () => Matrix.of(context)
.getLoginClient()
.requestTokenToResetPasswordEmail(
clientSecret,
emailController.text,
LoginController.sendAttempt++,
),
);
if (response.error != null) {
return;
}
Navigator.of(context).pop();
showDialog(
context: context,
useRootNavigator: false,
builder: (BuildContext context) => Scaffold(
backgroundColor: Colors.transparent,
body: AlertDialog(
title: Text(L10n.of(context).passwordForgotten),
content: Column(
mainAxisSize: MainAxisSize.min,
children: [
Text(L10n.of(context).chooseAStrongPassword),
const SizedBox(height: 12),
TextField(
obscureText: true,
controller: newPasswordController,
decoration: const InputDecoration(
hintText: "******",
),
),
],
),
actions: [
TextButton(
child: Text(L10n.of(context).cancel),
onPressed: () {
Navigator.of(context).pop();
return;
},
),
TextButton(
child: Text(L10n.of(context).ok),
onPressed: () async {
if (newPasswordController.text == "") return;
final ok = await showOkAlertDialog(
useRootNavigator: false,
context: context,
title: L10n.of(context).weSentYouAnEmail,
message: L10n.of(context).pleaseClickOnLink,
okLabel: L10n.of(context).iHaveClickedOnLink,
);
if (ok != OkCancelResult.ok) return;
final data = <String, dynamic>{
'new_password': newPasswordController.text,
'logout_devices': false,
"auth": AuthenticationThreePidCreds(
type: AuthenticationTypes.emailIdentity,
threepidCreds: ThreepidCreds(
sid: response.result!.sid,
clientSecret: clientSecret,
),
).toJson(),
};
final success = await showFutureLoadingDialog(
context: context,
future: () =>
Matrix.of(context).getLoginClient().request(
RequestType.POST,
'/client/r0/account/password',
data: data,
),
);
if (success.error == null) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(
L10n.of(context).passwordHasBeenChanged,
),
),
);
usernameController.text = emailController.text;
passwordController.text =
newPasswordController.text;
login();
}
},
),
],
),
),
);
},
),
],
),
),
);
}
}