diff --git a/lib/helpers/account_helper.dart b/lib/helpers/account_helper.dart index a4c58e2..935b49c 100644 --- a/lib/helpers/account_helper.dart +++ b/lib/helpers/account_helper.dart @@ -144,6 +144,20 @@ class AccountHelper { .getObject()["questions"]) .cast(); + /// Validate given security answers + /// + /// Throws an [Exception] in case of failure + /// + /// Returns a password reset token in case of success + static Future checkAnswers( + String email, List answers) async => + (await APIRequest.withoutLogin("account/check_security_answers") + .addString("email", email) + .addString("answers", + answers.map((f) => Uri.encodeComponent(f)).join("&")) + .execWithThrow()) + .getObject()["reset_token"]; + /// Get current user ID from the server Future _downloadCurrentUserID() async { final response = await APIRequest( diff --git a/lib/ui/routes/reset_password_route.dart b/lib/ui/routes/reset_password_route.dart index 6eab5f8..944d646 100644 --- a/lib/ui/routes/reset_password_route.dart +++ b/lib/ui/routes/reset_password_route.dart @@ -213,14 +213,26 @@ class _ResetPasswordBodyState extends SafeState<_ResetPasswordBody> { _canSubmitAnswers ? (s) => _submitSecurityAnswers() : null, decoration: InputDecoration( alignLabelWithHint: false, - labelText: tr("Answer %num%", args: {"num": id.toString()})), + labelText: + tr("Answer %num%", args: {"num": (id + 1).toString()})), ), _Spacer() ], ); /// Submit security answers - Future _submitSecurityAnswers() async {} + Future _submitSecurityAnswers() async { + _setLoading(true); + try { + final token = await AccountHelper.checkAnswers(_emailAddress, _answers); + print(token); + } catch (e, s) { + print("Could not submit security answers! $e\n$s"); + showSimpleSnack( + context, tr("Could not validate these security answers!")); + } + _setLoading(false); + } } class _Spacer extends StatelessWidget {