mirror of
https://codeberg.org/shroff/phylum.git
synced 2026-05-09 05:39:35 -05:00
[client] Move password reset logic into fragment
This commit is contained in:
@@ -1,6 +1,5 @@
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:flutter/gestures.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:http/http.dart';
|
||||
import 'package:offtheline/offtheline.dart';
|
||||
@@ -73,51 +72,12 @@ class _LoginPageState extends State<LoginPage> {
|
||||
});
|
||||
}
|
||||
return PasswordLoginFragment(
|
||||
onSubmitted: (email, password) => _performLogin(context, instanceUrl, email, password),
|
||||
requestLogin: (email, password) => _performLogin(context, instanceUrl, email, password),
|
||||
onBackPressed: widget.fixedInstanceUrl == null ? () => setState(() => _instanceUrl = null) : null,
|
||||
onForgotPassword: (email) async {
|
||||
if (email.isEmpty) {
|
||||
showAlertDialog(
|
||||
context,
|
||||
message: 'Please enter email address to reset password',
|
||||
barrierDismissible: true,
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
final contents = RichText(
|
||||
text: TextSpan(
|
||||
children: [
|
||||
TextSpan(text: 'This will request a password reset for $email.\n\nDo you want to continue?\n\n'),
|
||||
TextSpan(
|
||||
text: 'Tap here if you already have a reset token',
|
||||
style: TextStyle(color: Theme.of(context).colorScheme.primary),
|
||||
recognizer: TapGestureRecognizer()
|
||||
..onTap = () async {
|
||||
final token = await showInputDialog(context, labelText: 'Reset Token', barrierDismissable: true);
|
||||
if (token == null || !context.mounted) return;
|
||||
Navigator.of(context).pop(false);
|
||||
context
|
||||
.read<PhylumRouterDelegate>()
|
||||
.go(ResetPasswordRoute(instanceUri: instanceUrl, email: email, token: token));
|
||||
},
|
||||
),
|
||||
],
|
||||
style: Theme.of(context).textTheme.bodyMedium,
|
||||
),
|
||||
);
|
||||
final confirm = await showAlertDialog(
|
||||
context,
|
||||
title: 'Reset Password',
|
||||
contents: contents,
|
||||
negativeText: 'No',
|
||||
positiveText: 'Yes',
|
||||
barrierDismissible: true,
|
||||
) ??
|
||||
false;
|
||||
if (!confirm || !context.mounted) return;
|
||||
_requestPasswordReset(context, instanceUrl, email);
|
||||
},
|
||||
requestPasswordReset: (email) => _requestPasswordReset(context, instanceUrl, email),
|
||||
resetPassword: (email, token) => context
|
||||
.read<PhylumRouterDelegate>()
|
||||
.go(ResetPasswordRoute(instanceUri: instanceUrl, email: email, token: token)),
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,15 +1,19 @@
|
||||
import 'package:flutter/gestures.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:phylum/util/dialogs.dart';
|
||||
|
||||
class PasswordLoginFragment extends StatefulWidget {
|
||||
final Function(String, String) onSubmitted;
|
||||
final Function(String, String) requestLogin;
|
||||
final Function()? onBackPressed;
|
||||
final Function(String) onForgotPassword;
|
||||
final Function(String) requestPasswordReset;
|
||||
final Function(String, String) resetPassword;
|
||||
|
||||
const PasswordLoginFragment({
|
||||
super.key,
|
||||
required this.onSubmitted,
|
||||
required this.requestLogin,
|
||||
required this.onBackPressed,
|
||||
required this.onForgotPassword,
|
||||
required this.requestPasswordReset,
|
||||
required this.resetPassword,
|
||||
});
|
||||
|
||||
@override
|
||||
@@ -39,10 +43,51 @@ class _PasswordLoginFragmentState extends State<PasswordLoginFragment> {
|
||||
obscureText: true,
|
||||
keyboardType: TextInputType.visiblePassword,
|
||||
onChanged: (value) => setState(() => _password = value),
|
||||
onSubmitted: _email.isEmpty ? null : (value) => widget.onSubmitted(_email, _password),
|
||||
onSubmitted: _email.isEmpty ? null : (value) => widget.requestLogin(_email, _password),
|
||||
),
|
||||
TextButton(
|
||||
onPressed: () => widget.onForgotPassword(_email),
|
||||
onPressed: () async {
|
||||
if (_email.isEmpty) {
|
||||
showAlertDialog(
|
||||
context,
|
||||
message: 'Please enter email address to reset password',
|
||||
barrierDismissible: true,
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
final contents = RichText(
|
||||
text: TextSpan(
|
||||
children: [
|
||||
TextSpan(text: 'This will request a password reset for $_email.\n\nDo you want to continue?\n\n'),
|
||||
TextSpan(
|
||||
text: 'Tap here if you already have a reset token',
|
||||
style: TextStyle(color: Theme.of(context).colorScheme.primary),
|
||||
recognizer: TapGestureRecognizer()
|
||||
..onTap = () async {
|
||||
final token =
|
||||
await showInputDialog(context, labelText: 'Reset Token', barrierDismissable: true);
|
||||
if (token == null || !context.mounted) return;
|
||||
Navigator.of(context).pop(false);
|
||||
widget.resetPassword(_email, token);
|
||||
},
|
||||
),
|
||||
],
|
||||
style: Theme.of(context).textTheme.bodyMedium,
|
||||
),
|
||||
);
|
||||
final confirm = await showAlertDialog(
|
||||
context,
|
||||
title: 'Reset Password',
|
||||
contents: contents,
|
||||
negativeText: 'No',
|
||||
positiveText: 'Yes',
|
||||
barrierDismissible: true,
|
||||
) ??
|
||||
false;
|
||||
if (!confirm || !context.mounted) return;
|
||||
widget.requestPasswordReset(_email);
|
||||
},
|
||||
child: Text(
|
||||
'Forgot Password',
|
||||
style: TextStyle(color: Theme.of(context).colorScheme.primary),
|
||||
@@ -61,7 +106,7 @@ class _PasswordLoginFragmentState extends State<PasswordLoginFragment> {
|
||||
),
|
||||
),
|
||||
ElevatedButton(
|
||||
onPressed: _email.isEmpty || _password.isEmpty ? null : () => widget.onSubmitted(_email, _password),
|
||||
onPressed: _email.isEmpty || _password.isEmpty ? null : () => widget.requestLogin(_email, _password),
|
||||
child: Text('Login'),
|
||||
),
|
||||
],
|
||||
|
||||
Reference in New Issue
Block a user