Skip to content

Commit

Permalink
Add validator when changing login info
Browse files Browse the repository at this point in the history
  • Loading branch information
Serious-senpai committed Dec 22, 2024
1 parent 939b4d6 commit 9c24c48
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 39 deletions.
8 changes: 8 additions & 0 deletions app/resident_manager/lib/src/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -445,3 +445,11 @@ String? feePerCarValidator(BuildContext context, {required bool required, requir

return double.tryParse(value) == null ? AppLocale.InvalidValue.getString(context) : null;
}

String? requiredValidator(BuildContext context, {required String? value}) {
if (value == null || value.isEmpty) {
return AppLocale.MissingRequiredValue.getString(context);
}

return null;
}
80 changes: 41 additions & 39 deletions app/resident_manager/lib/src/widgets/personal_info.dart
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,8 @@ class _PersonalInfoPageState extends AbstractCommonState<PersonalInfoPage> with
),
),
obscureText: true,
// No need to validate old password
// validator: (value) => passwordValidator(context, required: false, value: value),
// Must provide old password for authorization
validator: (value) => requiredValidator(context, value: value),
),
),
_InfoCard(
Expand Down Expand Up @@ -276,53 +276,55 @@ class _PersonalInfoPageState extends AbstractCommonState<PersonalInfoPage> with
: () async {
await _authLock.run(
() async {
_authNotification = Builder(
builder: (context) => Text(
AppLocale.Loading.getString(context),
style: const TextStyle(color: Colors.blue),
),
);
refresh();
if (_authFormKey.currentState?.validate() ?? false) {
_authNotification = Builder(
builder: (context) => Text(
AppLocale.Loading.getString(context),
style: const TextStyle(color: Colors.blue),
),
);
refresh();

try {
final result = await state.resident?.updateAuthorization(
state: state,
newUsername: _username.text,
oldPassword: _oldPassword.text,
try {
final result = await state.resident?.updateAuthorization(
state: state,
newUsername: _username.text,
oldPassword: _oldPassword.text,

// If the user doesn't fill out the new password, keep the old one
newPassword: _newPassword.text.isEmpty ? _oldPassword.text : _newPassword.text,
);
// If the user doesn't fill out the new password, keep the old one
newPassword: _newPassword.text.isEmpty ? _oldPassword.text : _newPassword.text,
);

if (result == null || result.code != 0) {
if (result == null || result.code != 0) {
_authNotification = Builder(
builder: (context) => Text(
AppLocale.errorMessage(result?.code ?? -1).getString(context),
style: const TextStyle(color: Colors.red),
),
);
} else {
// Authorization info updated. Logout.
await state.deauthorize();
if (context.mounted) {
Navigator.popUntil(context, (route) => route.isFirst);
await Navigator.pushReplacementNamed(context, ApplicationRoute.login);
}
}
} catch (e) {
await showToastSafe(msg: context.mounted ? AppLocale.ConnectionError.getString(context) : AppLocale.ConnectionError);
_authNotification = Builder(
builder: (context) => Text(
AppLocale.errorMessage(result?.code ?? -1).getString(context),
AppLocale.ConnectionError.getString(context),
style: const TextStyle(color: Colors.red),
),
);
} else {
// Authorization info updated. Logout.
await state.deauthorize();
if (context.mounted) {
Navigator.popUntil(context, (route) => route.isFirst);
await Navigator.pushReplacementNamed(context, ApplicationRoute.login);
}
}
} catch (e) {
await showToastSafe(msg: context.mounted ? AppLocale.ConnectionError.getString(context) : AppLocale.ConnectionError);
_authNotification = Builder(
builder: (context) => Text(
AppLocale.ConnectionError.getString(context),
style: const TextStyle(color: Colors.red),
),
);

if (!(e is SocketException || e is TimeoutException)) {
rethrow;
if (!(e is SocketException || e is TimeoutException)) {
rethrow;
}
} finally {
refresh();
}
} finally {
refresh();
}
},
);
Expand Down

0 comments on commit 9c24c48

Please sign in to comment.