ente/lib/services/user_service.dart

179 lines
5.7 KiB
Dart
Raw Normal View History

2020-04-30 15:09:41 +00:00
import 'package:dio/dio.dart';
2020-08-25 06:00:19 +00:00
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
2020-05-02 16:28:54 +00:00
import 'package:logging/logging.dart';
import 'package:photos/core/configuration.dart';
2020-11-19 18:22:30 +00:00
import 'package:photos/core/network.dart';
2020-10-17 18:16:30 +00:00
import 'package:photos/db/public_keys_db.dart';
import 'package:photos/models/key_attributes.dart';
2020-10-18 21:39:55 +00:00
import 'package:photos/models/public_key.dart';
import 'package:photos/models/subscription.dart';
import 'package:photos/services/billing_service.dart';
2020-08-25 06:00:19 +00:00
import 'package:photos/ui/ott_verification_page.dart';
2021-01-05 14:27:02 +00:00
import 'package:photos/ui/password_entry_page.dart';
import 'package:photos/ui/password_reentry_page.dart';
2021-01-06 16:09:42 +00:00
import 'package:photos/ui/subscription_page.dart';
2020-08-25 06:00:19 +00:00
import 'package:photos/utils/dialog_util.dart';
import 'package:photos/utils/toast_util.dart';
2020-04-30 15:09:41 +00:00
2020-10-03 17:56:18 +00:00
class UserService {
2020-11-19 18:22:30 +00:00
final _dio = Network.instance.getDio();
2020-05-02 16:28:54 +00:00
final _logger = Logger("UserAuthenticator");
final _config = Configuration.instance;
2020-04-30 15:09:41 +00:00
2020-10-03 17:56:18 +00:00
UserService._privateConstructor();
2020-04-30 15:09:41 +00:00
2020-10-03 17:56:18 +00:00
static final UserService instance = UserService._privateConstructor();
2020-04-30 15:09:41 +00:00
2020-08-25 06:00:19 +00:00
Future<void> getOtt(BuildContext context, String email) async {
2021-01-08 17:02:41 +00:00
final dialog = createProgressDialog(context, "please wait...");
2020-08-25 06:00:19 +00:00
await dialog.show();
2020-08-28 23:50:18 +00:00
await _dio.get(
_config.getHttpEndpoint() + "/users/ott",
2020-08-25 06:00:19 +00:00
queryParameters: {
"email": email,
},
).catchError((e) async {
_logger.severe(e);
}).then((response) async {
await dialog.hide();
if (response != null) {
if (response.statusCode == 200) {
Navigator.of(context).push(
MaterialPageRoute(
builder: (BuildContext context) {
return OTTVerificationPage();
},
),
);
} else if (response.statusCode == 403) {
showErrorDialog(
context,
2021-01-08 17:06:52 +00:00
"please wait...",
"we are currently not accepting new registrations. you have been added to the waitlist and we will let you know once we are ready for you.",
);
}
2020-08-25 06:00:19 +00:00
} else {
showGenericErrorDialog(context);
}
});
}
Future<String> getPublicKey(String email) async {
try {
final response = await _dio.get(
_config.getHttpEndpoint() + "/users/public-key",
queryParameters: {"email": email},
options: Options(
headers: {
"X-Auth-Token": _config.getToken(),
},
),
);
2020-10-18 21:39:55 +00:00
final publicKey = response.data["publicKey"];
await PublicKeysDB.instance.setKey(PublicKey(email, publicKey));
2020-10-17 18:16:30 +00:00
return publicKey;
} on DioError catch (e) {
_logger.info(e);
return null;
}
}
2020-08-25 23:02:43 +00:00
Future<void> getCredentials(BuildContext context, String ott) async {
2021-01-08 17:02:41 +00:00
final dialog = createProgressDialog(context, "please wait...");
2020-08-25 06:00:19 +00:00
await dialog.show();
try {
final response = await _dio.get(
_config.getHttpEndpoint() + "/users/credentials",
queryParameters: {
"email": _config.getEmail(),
"ott": ott,
},
);
2020-08-25 06:00:19 +00:00
await dialog.hide();
if (response != null && response.statusCode == 200) {
await _saveConfiguration(response);
2021-01-08 17:11:32 +00:00
showToast("email verification successful!");
var page;
if (Configuration.instance.getKeyAttributes() != null) {
2021-01-05 14:27:02 +00:00
page = PasswordReentryPage();
} else {
2021-01-05 14:27:02 +00:00
page = PasswordEntryPage();
}
Navigator.of(context).pushAndRemoveUntil(
MaterialPageRoute(
builder: (BuildContext context) {
return page;
},
),
(route) => route.isFirst,
);
2020-08-25 06:00:19 +00:00
} else {
showErrorDialog(
2021-01-08 17:06:52 +00:00
context, "oops", "verification failed, please try again");
2020-08-25 06:00:19 +00:00
}
} catch (e) {
await dialog.hide();
_logger.severe(e);
showErrorDialog(context, "oops", "verification failed, please try again");
}
2020-08-25 06:00:19 +00:00
}
2021-01-05 14:27:02 +00:00
Future<void> setupAttributes(BuildContext context, String password) async {
2021-01-08 17:02:41 +00:00
final dialog = createProgressDialog(context, "please wait...");
await dialog.show();
2021-01-05 14:27:02 +00:00
final result = await _config.generateKey(password);
2020-10-31 15:33:32 +00:00
final name = _config.getName();
await _dio
.put(
2020-10-31 15:33:32 +00:00
_config.getHttpEndpoint() + "/users/attributes",
data: {
"name": name,
"keyAttributes": result.keyAttributes.toMap(),
},
options: Options(
headers: {
"X-Auth-Token": _config.getToken(),
},
),
)
.catchError((e) async {
await dialog.hide();
_logger.severe(e);
showGenericErrorDialog(context);
}).then((response) async {
await dialog.hide();
if (response != null && response.statusCode == 200) {
await _config.setKey(result.privateKeyAttributes.key);
await _config.setSecretKey(result.privateKeyAttributes.secretKey);
await _config.setKeyAttributes(result.keyAttributes);
2021-01-06 16:09:42 +00:00
Navigator.of(context).pushAndRemoveUntil(
MaterialPageRoute(
builder: (BuildContext context) {
return SubscriptionPage(isOnboarding: true);
2021-01-06 16:09:42 +00:00
},
),
(route) => route.isFirst,
);
} else {
showGenericErrorDialog(context);
}
});
}
Future<void> _saveConfiguration(Response response) async {
await Configuration.instance.setUserID(response.data["id"]);
await Configuration.instance.setToken(response.data["token"]);
final keyAttributes = response.data["keyAttributes"];
if (keyAttributes != null) {
await Configuration.instance
.setKeyAttributes(KeyAttributes.fromMap(keyAttributes));
2020-08-15 01:22:14 +00:00
}
final subscription = response.data["subscription"];
if (subscription != null) {
await BillingService.instance
.setSubscription(Subscription.fromMap(subscription));
}
}
2020-04-30 15:09:41 +00:00
}