furman_now/lib/src/services/get_app/user/user_service.dart

78 lines
2.1 KiB
Dart

import 'dart:convert';
import 'package:flutter_inappwebview/flutter_inappwebview.dart';
import 'package:furman_now/src/services/get_app/user/in_app_browser.dart';
import 'package:http/http.dart' as http;
class UserService {
final String servicesUrl = "https://services.get.cbord.com/GETServices/services/json";
final GetLoginInAppBrowser browser = GetLoginInAppBrowser();
void login() {
var options = InAppBrowserClassOptions(
crossPlatform: InAppBrowserOptions(
hideUrlBar: true,
),
);
browser.loadStart.subscribe((args) async {
if (args!.url != null) {
bool success = await _getAuthSessionFromUrl(args.url!);
print(success);
if (success) {
browser.close();
}
}
});
browser.openUrlRequest(
urlRequest: URLRequest(
url: Uri.parse("https://get.cbord.com/furman/full/login.php?mobileapp=1"),
),
options: options,
);
}
Future<bool> _getAuthSessionFromUrl(Uri url) async {
print(url);
if (
url.origin == "https://get.cbord.com" &&
url.path.contains("mobileapp_login_validator.php")
) {
var sessionId = url.queryParameters["sessionId"];
if (sessionId != null) {
return _validateSession(sessionId);
}
}
return false;
}
Future<bool> _validateSession(String sessionId) async {
final response = await http.post(
Uri.parse("$servicesUrl/user"),
headers: {
"Content-Type": "application/json",
},
body: """{
"method": "retrieve",
"params": {
"sessionId": "$sessionId"
}
}"""
);
if (response.statusCode == 200) {
// If the server did return a 200 OK response,
// then parse the JSON.
final json = jsonDecode(response.body);
print(json);
// make sure there wasn't an exception and the data was actually loaded
return json["exception"] == null;
} else {
// If the server did not return a 200 OK response,
// then throw an exception.
print(response.statusCode);
throw Exception('Failed to load user info.');
}
}
}