2020-04-30 15:09:41 +00:00
|
|
|
import 'package:dio/dio.dart';
|
2020-05-02 16:28:54 +00:00
|
|
|
import 'package:logging/logging.dart';
|
2020-05-04 20:08:20 +00:00
|
|
|
import 'package:photos/core/configuration.dart';
|
|
|
|
import 'package:photos/core/event_bus.dart';
|
2020-04-30 15:09:41 +00:00
|
|
|
|
2020-05-04 20:08:20 +00:00
|
|
|
import 'package:photos/events/user_authenticated_event.dart';
|
2020-04-30 15:09:41 +00:00
|
|
|
|
|
|
|
class UserAuthenticator {
|
|
|
|
final _dio = Dio();
|
2020-05-02 16:28:54 +00:00
|
|
|
final _logger = Logger("UserAuthenticator");
|
2020-04-30 15:09:41 +00:00
|
|
|
|
|
|
|
UserAuthenticator._privateConstructor();
|
|
|
|
|
|
|
|
static final UserAuthenticator instance =
|
|
|
|
UserAuthenticator._privateConstructor();
|
|
|
|
|
|
|
|
Future<bool> login(String username, String password) {
|
|
|
|
return _dio.post(
|
2020-05-17 12:39:38 +00:00
|
|
|
Configuration.instance.getHttpEndpoint() + "/users/authenticate",
|
|
|
|
data: {
|
|
|
|
"username": username,
|
|
|
|
"password": password,
|
|
|
|
}).then((response) {
|
2020-04-30 15:09:41 +00:00
|
|
|
if (response.statusCode == 200 && response.data != null) {
|
2020-07-31 22:58:30 +00:00
|
|
|
_saveConfiguration(username, password, response);
|
2020-04-30 15:09:41 +00:00
|
|
|
Bus.instance.fire(UserAuthenticatedEvent());
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}).catchError((e) {
|
2020-05-02 16:28:54 +00:00
|
|
|
_logger.severe(e.toString());
|
2020-04-30 15:09:41 +00:00
|
|
|
return false;
|
|
|
|
});
|
|
|
|
}
|
2020-04-30 21:51:42 +00:00
|
|
|
|
|
|
|
Future<bool> create(String username, String password) {
|
2020-05-17 12:39:38 +00:00
|
|
|
return _dio
|
|
|
|
.post(Configuration.instance.getHttpEndpoint() + "/users", data: {
|
|
|
|
"username": username,
|
|
|
|
"password": password,
|
|
|
|
}).then((response) {
|
2020-04-30 21:51:42 +00:00
|
|
|
if (response.statusCode == 200 && response.data != null) {
|
2020-07-31 22:58:30 +00:00
|
|
|
_saveConfiguration(username, password, response);
|
2020-04-30 21:51:42 +00:00
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
if (response.data != null && response.data["message"] != null) {
|
|
|
|
throw Exception(response.data["message"]);
|
|
|
|
} else {
|
|
|
|
throw Exception("Something went wrong");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}).catchError((e) {
|
2020-05-02 16:28:54 +00:00
|
|
|
_logger.severe(e.toString());
|
2020-04-30 21:51:42 +00:00
|
|
|
throw e;
|
|
|
|
});
|
|
|
|
}
|
2020-07-31 22:58:30 +00:00
|
|
|
|
2020-08-15 01:22:14 +00:00
|
|
|
Future<void> setEncryptedKeyOnServer() {
|
|
|
|
return _dio.put(
|
|
|
|
Configuration.instance.getHttpEndpoint() + "/users/encrypted-key",
|
|
|
|
data: {
|
|
|
|
"encryptedKey": Configuration.instance.getEncryptedKey(),
|
|
|
|
},
|
|
|
|
options: Options(headers: {
|
|
|
|
"X-Auth-Token": Configuration.instance.getToken(),
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-07-31 22:58:30 +00:00
|
|
|
void _saveConfiguration(String username, String password, Response response) {
|
|
|
|
Configuration.instance.setUsername(username);
|
|
|
|
Configuration.instance.setPassword(password);
|
|
|
|
Configuration.instance.setUserID(response.data["id"]);
|
|
|
|
Configuration.instance.setToken(response.data["token"]);
|
2020-08-15 01:22:14 +00:00
|
|
|
final String encryptedKey = response.data["encryptedKey"];
|
|
|
|
if (encryptedKey != null && encryptedKey.isNotEmpty) {
|
|
|
|
Configuration.instance.setEncryptedKey(encryptedKey);
|
|
|
|
}
|
2020-07-31 22:58:30 +00:00
|
|
|
}
|
2020-04-30 15:09:41 +00:00
|
|
|
}
|