|
@@ -1,9 +1,13 @@
|
|
import 'package:dio/dio.dart';
|
|
import 'package:dio/dio.dart';
|
|
|
|
+import 'package:flutter/material.dart';
|
|
|
|
+import 'package:flutter/widgets.dart';
|
|
import 'package:logging/logging.dart';
|
|
import 'package:logging/logging.dart';
|
|
import 'package:photos/core/configuration.dart';
|
|
import 'package:photos/core/configuration.dart';
|
|
import 'package:photos/core/event_bus.dart';
|
|
import 'package:photos/core/event_bus.dart';
|
|
|
|
|
|
import 'package:photos/events/user_authenticated_event.dart';
|
|
import 'package:photos/events/user_authenticated_event.dart';
|
|
|
|
+import 'package:photos/ui/ott_verification_page.dart';
|
|
|
|
+import 'package:photos/utils/dialog_util.dart';
|
|
|
|
|
|
class UserAuthenticator {
|
|
class UserAuthenticator {
|
|
final _dio = Dio();
|
|
final _dio = Dio();
|
|
@@ -14,6 +18,60 @@ class UserAuthenticator {
|
|
static final UserAuthenticator instance =
|
|
static final UserAuthenticator instance =
|
|
UserAuthenticator._privateConstructor();
|
|
UserAuthenticator._privateConstructor();
|
|
|
|
|
|
|
|
+ Future<void> getOtt(BuildContext context, String email) async {
|
|
|
|
+ final dialog = createProgressDialog(context, "Please wait...");
|
|
|
|
+ await dialog.show();
|
|
|
|
+ await Dio().get(
|
|
|
|
+ Configuration.instance.getHttpEndpoint() + "/users/ott",
|
|
|
|
+ queryParameters: {
|
|
|
|
+ "email": email,
|
|
|
|
+ },
|
|
|
|
+ ).catchError((e) async {
|
|
|
|
+ _logger.severe(e);
|
|
|
|
+ await dialog.hide();
|
|
|
|
+ showGenericErrorDialog(context);
|
|
|
|
+ }).then((response) async {
|
|
|
|
+ await dialog.hide();
|
|
|
|
+ if (response.statusCode == 200) {
|
|
|
|
+ Navigator.of(context).push(
|
|
|
|
+ MaterialPageRoute(
|
|
|
|
+ builder: (BuildContext context) {
|
|
|
|
+ return OTTVerificationPage(email);
|
|
|
|
+ },
|
|
|
|
+ ),
|
|
|
|
+ );
|
|
|
|
+ } else {
|
|
|
|
+ showGenericErrorDialog(context);
|
|
|
|
+ }
|
|
|
|
+ });
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ Future<void> getCredentials(
|
|
|
|
+ BuildContext context, String email, String ott) async {
|
|
|
|
+ final dialog = createProgressDialog(context, "Please wait...");
|
|
|
|
+ await dialog.show();
|
|
|
|
+ await Dio().get(
|
|
|
|
+ Configuration.instance.getHttpEndpoint() + "/users/credentials",
|
|
|
|
+ queryParameters: {
|
|
|
|
+ "email": email,
|
|
|
|
+ "ott": ott,
|
|
|
|
+ },
|
|
|
|
+ ).catchError((e) async {
|
|
|
|
+ _logger.severe(e);
|
|
|
|
+ await dialog.hide();
|
|
|
|
+ showGenericErrorDialog(context);
|
|
|
|
+ }).then((response) async {
|
|
|
|
+ await dialog.hide();
|
|
|
|
+ if (response.statusCode == 200) {
|
|
|
|
+ _saveConfiguration(email, response);
|
|
|
|
+ Navigator.of(context).pop();
|
|
|
|
+ } else {
|
|
|
|
+ showErrorDialog(
|
|
|
|
+ context, "Oops.", "Verification failed, please try again.");
|
|
|
|
+ }
|
|
|
|
+ });
|
|
|
|
+ }
|
|
|
|
+
|
|
Future<bool> login(String username, String password) {
|
|
Future<bool> login(String username, String password) {
|
|
return _dio.post(
|
|
return _dio.post(
|
|
Configuration.instance.getHttpEndpoint() + "/users/authenticate",
|
|
Configuration.instance.getHttpEndpoint() + "/users/authenticate",
|
|
@@ -22,7 +80,7 @@ class UserAuthenticator {
|
|
"password": password,
|
|
"password": password,
|
|
}).then((response) {
|
|
}).then((response) {
|
|
if (response.statusCode == 200 && response.data != null) {
|
|
if (response.statusCode == 200 && response.data != null) {
|
|
- _saveConfiguration(username, password, response);
|
|
|
|
|
|
+ _saveConfiguration(username, response);
|
|
Bus.instance.fire(UserAuthenticatedEvent());
|
|
Bus.instance.fire(UserAuthenticatedEvent());
|
|
return true;
|
|
return true;
|
|
} else {
|
|
} else {
|
|
@@ -41,7 +99,7 @@ class UserAuthenticator {
|
|
"password": password,
|
|
"password": password,
|
|
}).then((response) {
|
|
}).then((response) {
|
|
if (response.statusCode == 200 && response.data != null) {
|
|
if (response.statusCode == 200 && response.data != null) {
|
|
- _saveConfiguration(username, password, response);
|
|
|
|
|
|
+ _saveConfiguration(username, response);
|
|
return true;
|
|
return true;
|
|
} else {
|
|
} else {
|
|
if (response.data != null && response.data["message"] != null) {
|
|
if (response.data != null && response.data["message"] != null) {
|
|
@@ -68,9 +126,9 @@ class UserAuthenticator {
|
|
);
|
|
);
|
|
}
|
|
}
|
|
|
|
|
|
- void _saveConfiguration(String username, String password, Response response) {
|
|
|
|
- Configuration.instance.setUsername(username);
|
|
|
|
- Configuration.instance.setPassword(password);
|
|
|
|
|
|
+ void _saveConfiguration(String email, Response response) {
|
|
|
|
+ _logger.info("Saving configuration " + response.data.toString());
|
|
|
|
+ Configuration.instance.setEmail(email);
|
|
Configuration.instance.setUserID(response.data["id"]);
|
|
Configuration.instance.setUserID(response.data["id"]);
|
|
Configuration.instance.setToken(response.data["token"]);
|
|
Configuration.instance.setToken(response.data["token"]);
|
|
final String encryptedKey = response.data["encryptedKey"];
|
|
final String encryptedKey = response.data["encryptedKey"];
|