user_authenticator.dart 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. import 'package:dio/dio.dart';
  2. import 'package:flutter/material.dart';
  3. import 'package:flutter/widgets.dart';
  4. import 'package:logging/logging.dart';
  5. import 'package:photos/core/configuration.dart';
  6. import 'package:photos/core/event_bus.dart';
  7. import 'package:photos/events/user_authenticated_event.dart';
  8. import 'package:photos/ui/ott_verification_page.dart';
  9. import 'package:photos/utils/dialog_util.dart';
  10. class UserAuthenticator {
  11. final _dio = Dio();
  12. final _logger = Logger("UserAuthenticator");
  13. UserAuthenticator._privateConstructor();
  14. static final UserAuthenticator instance =
  15. UserAuthenticator._privateConstructor();
  16. Future<void> getOtt(BuildContext context, String email) async {
  17. final dialog = createProgressDialog(context, "Please wait...");
  18. await dialog.show();
  19. await Dio().get(
  20. Configuration.instance.getHttpEndpoint() + "/users/ott",
  21. queryParameters: {
  22. "email": email,
  23. },
  24. ).catchError((e) async {
  25. _logger.severe(e);
  26. await dialog.hide();
  27. showGenericErrorDialog(context);
  28. }).then((response) async {
  29. await dialog.hide();
  30. if (response.statusCode == 200) {
  31. Navigator.of(context).push(
  32. MaterialPageRoute(
  33. builder: (BuildContext context) {
  34. return OTTVerificationPage(email);
  35. },
  36. ),
  37. );
  38. } else {
  39. showGenericErrorDialog(context);
  40. }
  41. });
  42. }
  43. Future<void> getCredentials(
  44. BuildContext context, String email, String ott) async {
  45. final dialog = createProgressDialog(context, "Please wait...");
  46. await dialog.show();
  47. await Dio().get(
  48. Configuration.instance.getHttpEndpoint() + "/users/credentials",
  49. queryParameters: {
  50. "email": email,
  51. "ott": ott,
  52. },
  53. ).catchError((e) async {
  54. _logger.severe(e);
  55. await dialog.hide();
  56. showGenericErrorDialog(context);
  57. }).then((response) async {
  58. await dialog.hide();
  59. if (response.statusCode == 200) {
  60. _saveConfiguration(email, response);
  61. Navigator.of(context).pop();
  62. } else {
  63. showErrorDialog(
  64. context, "Oops.", "Verification failed, please try again.");
  65. }
  66. });
  67. }
  68. Future<bool> login(String username, String password) {
  69. return _dio.post(
  70. Configuration.instance.getHttpEndpoint() + "/users/authenticate",
  71. data: {
  72. "username": username,
  73. "password": password,
  74. }).then((response) {
  75. if (response.statusCode == 200 && response.data != null) {
  76. _saveConfiguration(username, response);
  77. Bus.instance.fire(UserAuthenticatedEvent());
  78. return true;
  79. } else {
  80. return false;
  81. }
  82. }).catchError((e) {
  83. _logger.severe(e.toString());
  84. return false;
  85. });
  86. }
  87. Future<bool> create(String username, String password) {
  88. return _dio
  89. .post(Configuration.instance.getHttpEndpoint() + "/users", data: {
  90. "username": username,
  91. "password": password,
  92. }).then((response) {
  93. if (response.statusCode == 200 && response.data != null) {
  94. _saveConfiguration(username, response);
  95. return true;
  96. } else {
  97. if (response.data != null && response.data["message"] != null) {
  98. throw Exception(response.data["message"]);
  99. } else {
  100. throw Exception("Something went wrong");
  101. }
  102. }
  103. }).catchError((e) {
  104. _logger.severe(e.toString());
  105. throw e;
  106. });
  107. }
  108. Future<void> setEncryptedKeyOnServer() {
  109. return _dio.put(
  110. Configuration.instance.getHttpEndpoint() + "/users/encrypted-key",
  111. data: {
  112. "encryptedKey": Configuration.instance.getEncryptedKey(),
  113. },
  114. options: Options(headers: {
  115. "X-Auth-Token": Configuration.instance.getToken(),
  116. }),
  117. );
  118. }
  119. void _saveConfiguration(String email, Response response) {
  120. Configuration.instance.setEmail(email);
  121. Configuration.instance.setUserID(response.data["id"]);
  122. Configuration.instance.setToken(response.data["token"]);
  123. final String encryptedKey = response.data["encryptedKey"];
  124. if (encryptedKey != null && encryptedKey.isNotEmpty) {
  125. Configuration.instance.setEncryptedKey(encryptedKey);
  126. }
  127. }
  128. }