Fix authentication process. App register in background
This commit is contained in:
parent
ad6355503b
commit
ef15026203
@ -105,23 +105,27 @@ class Connection {
|
|||||||
_disconnect().then((_) {
|
_disconnect().then((_) {
|
||||||
completer?.completeError(HAError("Connection timeout"));
|
completer?.completeError(HAError("Connection timeout"));
|
||||||
});
|
});
|
||||||
}).then((_) => completer?.complete()).catchError((e) {
|
}).then((_) {
|
||||||
|
Logger.d("doConnect is finished 1");
|
||||||
|
completer?.complete();
|
||||||
|
}).catchError((e) {
|
||||||
completer?.completeError(e);
|
completer?.completeError(e);
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
|
Logger.d("doConnect is finished 2");
|
||||||
completer?.complete();
|
completer?.complete();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Completer connecting;
|
Completer connecting;
|
||||||
|
|
||||||
Future _connect() async {
|
Future _connect() {
|
||||||
if (connecting != null && !connecting.isCompleted) {
|
if (connecting != null && !connecting.isCompleted) {
|
||||||
Logger.w("Previous connection attempt pending...");
|
Logger.w("Previous connection attempt pending...");
|
||||||
return connecting.future;
|
return connecting.future;
|
||||||
} else {
|
} else {
|
||||||
connecting = Completer();
|
connecting = Completer();
|
||||||
await _disconnect();
|
_disconnect().then((_) {
|
||||||
Logger.d("Socket connecting: $_webSocketAPIEndpoint...");
|
Logger.d("Socket connecting: $_webSocketAPIEndpoint...");
|
||||||
_socket = IOWebSocketChannel.connect(
|
_socket = IOWebSocketChannel.connect(
|
||||||
_webSocketAPIEndpoint, pingInterval: Duration(seconds: 15));
|
_webSocketAPIEndpoint, pingInterval: Duration(seconds: 15));
|
||||||
@ -131,15 +135,19 @@ class Connection {
|
|||||||
var data = json.decode(message);
|
var data = json.decode(message);
|
||||||
if (data["type"] == "auth_required") {
|
if (data["type"] == "auth_required") {
|
||||||
Logger.d("[Received] <== ${data.toString()}");
|
Logger.d("[Received] <== ${data.toString()}");
|
||||||
_authenticate().then((_) => connecting.complete()).catchError((
|
_authenticate().then((_) {
|
||||||
e) {
|
Logger.d('Authentication complete');
|
||||||
|
connecting.complete();
|
||||||
|
}).catchError((e) {
|
||||||
if (!connecting.isCompleted) connecting.completeError(e);
|
if (!connecting.isCompleted) connecting.completeError(e);
|
||||||
});
|
});
|
||||||
} else if (data["type"] == "auth_ok") {
|
} else if (data["type"] == "auth_ok") {
|
||||||
Logger.d("[Received] <== ${data.toString()}");
|
Logger.d("[Received] <== ${data.toString()}");
|
||||||
_messageResolver["auth"]?.complete();
|
_messageResolver["auth"]?.complete();
|
||||||
_messageResolver.remove("auth");
|
_messageResolver.remove("auth");
|
||||||
|
if (_token != null) {
|
||||||
if (!connecting.isCompleted) connecting.complete();
|
if (!connecting.isCompleted) connecting.complete();
|
||||||
|
}
|
||||||
} else if (data["type"] == "auth_invalid") {
|
} else if (data["type"] == "auth_invalid") {
|
||||||
Logger.d("[Received] <== ${data.toString()}");
|
Logger.d("[Received] <== ${data.toString()}");
|
||||||
_messageResolver["auth"]?.completeError(HAError("${data["message"]}", actions: [HAErrorAction.loginAgain()]));
|
_messageResolver["auth"]?.completeError(HAError("${data["message"]}", actions: [HAErrorAction.loginAgain()]));
|
||||||
@ -155,6 +163,7 @@ class Connection {
|
|||||||
onDone: () => _handleSocketClose(connecting),
|
onDone: () => _handleSocketClose(connecting),
|
||||||
onError: (e) => _handleSocketError(e, connecting)
|
onError: (e) => _handleSocketError(e, connecting)
|
||||||
);
|
);
|
||||||
|
});
|
||||||
return connecting.future;
|
return connecting.future;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -262,6 +271,7 @@ class Connection {
|
|||||||
).then((_) {
|
).then((_) {
|
||||||
Logger.d("Requesting long-lived token...");
|
Logger.d("Requesting long-lived token...");
|
||||||
_getLongLivedToken().then((_) {
|
_getLongLivedToken().then((_) {
|
||||||
|
Logger.d("getLongLivedToken finished");
|
||||||
completer.complete();
|
completer.complete();
|
||||||
}).catchError((e) {
|
}).catchError((e) {
|
||||||
Logger.e("Can't get long-lived token: $e");
|
Logger.e("Can't get long-lived token: $e");
|
||||||
|
@ -58,7 +58,6 @@ class HomeAssistant {
|
|||||||
futures.add(_getServices());
|
futures.add(_getServices());
|
||||||
futures.add(_getUserInfo());
|
futures.add(_getUserInfo());
|
||||||
futures.add(_getPanels());
|
futures.add(_getPanels());
|
||||||
futures.add(checkAppRegistration());
|
|
||||||
futures.add(Connection().sendSocketMessage(
|
futures.add(Connection().sendSocketMessage(
|
||||||
type: "subscribe_events",
|
type: "subscribe_events",
|
||||||
additionalData: {"event_type": "state_changed"},
|
additionalData: {"event_type": "state_changed"},
|
||||||
@ -67,6 +66,7 @@ class HomeAssistant {
|
|||||||
if (isMobileAppEnabled) {
|
if (isMobileAppEnabled) {
|
||||||
_createUI();
|
_createUI();
|
||||||
_fetchCompleter.complete();
|
_fetchCompleter.complete();
|
||||||
|
checkAppRegistration();
|
||||||
} else {
|
} else {
|
||||||
_fetchCompleter.completeError(HAError("Mobile app component not found", actions: [HAErrorAction.tryAgain(), HAErrorAction(type: HAErrorActionType.URL ,title: "Help",url: "http://ha-client.homemade.systems/docs#mobile-app")]));
|
_fetchCompleter.completeError(HAError("Mobile app component not found", actions: [HAErrorAction.tryAgain(), HAErrorAction(type: HAErrorActionType.URL ,title: "Help",url: "http://ha-client.homemade.systems/docs#mobile-app")]));
|
||||||
}
|
}
|
||||||
|
@ -347,6 +347,7 @@ class _MainPageState extends State<MainPage> with WidgetsBindingObserver, Ticker
|
|||||||
void _showDialog({String title, String body, var onPositive, var onNegative, String positiveText, String negativeText}) {
|
void _showDialog({String title, String body, var onPositive, var onNegative, String positiveText, String negativeText}) {
|
||||||
// flutter defined function
|
// flutter defined function
|
||||||
showDialog(
|
showDialog(
|
||||||
|
barrierDismissible: false,
|
||||||
context: context,
|
context: context,
|
||||||
builder: (BuildContext context) {
|
builder: (BuildContext context) {
|
||||||
// return object of type Dialog
|
// return object of type Dialog
|
||||||
|
23
pubspec.lock
23
pubspec.lock
@ -21,7 +21,7 @@ packages:
|
|||||||
name: async
|
name: async
|
||||||
url: "https://pub.dartlang.org"
|
url: "https://pub.dartlang.org"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "2.0.8"
|
version: "2.1.0"
|
||||||
boolean_selector:
|
boolean_selector:
|
||||||
dependency: transitive
|
dependency: transitive
|
||||||
description:
|
description:
|
||||||
@ -141,6 +141,13 @@ packages:
|
|||||||
url: "https://pub.dartlang.org"
|
url: "https://pub.dartlang.org"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "0.7.0"
|
version: "0.7.0"
|
||||||
|
flutter_local_notifications:
|
||||||
|
dependency: "direct main"
|
||||||
|
description:
|
||||||
|
name: flutter_local_notifications
|
||||||
|
url: "https://pub.dartlang.org"
|
||||||
|
source: hosted
|
||||||
|
version: "0.7.1+3"
|
||||||
flutter_markdown:
|
flutter_markdown:
|
||||||
dependency: "direct main"
|
dependency: "direct main"
|
||||||
description:
|
description:
|
||||||
@ -222,7 +229,7 @@ packages:
|
|||||||
name: matcher
|
name: matcher
|
||||||
url: "https://pub.dartlang.org"
|
url: "https://pub.dartlang.org"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "0.12.3+1"
|
version: "0.12.5"
|
||||||
meta:
|
meta:
|
||||||
dependency: transitive
|
dependency: transitive
|
||||||
description:
|
description:
|
||||||
@ -264,7 +271,7 @@ packages:
|
|||||||
name: pedantic
|
name: pedantic
|
||||||
url: "https://pub.dartlang.org"
|
url: "https://pub.dartlang.org"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "1.4.0"
|
version: "1.5.0"
|
||||||
petitparser:
|
petitparser:
|
||||||
dependency: transitive
|
dependency: transitive
|
||||||
description:
|
description:
|
||||||
@ -292,7 +299,7 @@ packages:
|
|||||||
name: quiver
|
name: quiver
|
||||||
url: "https://pub.dartlang.org"
|
url: "https://pub.dartlang.org"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "2.0.1"
|
version: "2.0.2"
|
||||||
shared_preferences:
|
shared_preferences:
|
||||||
dependency: "direct main"
|
dependency: "direct main"
|
||||||
description:
|
description:
|
||||||
@ -311,7 +318,7 @@ packages:
|
|||||||
name: source_span
|
name: source_span
|
||||||
url: "https://pub.dartlang.org"
|
url: "https://pub.dartlang.org"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "1.5.4"
|
version: "1.5.5"
|
||||||
sqflite:
|
sqflite:
|
||||||
dependency: transitive
|
dependency: transitive
|
||||||
description:
|
description:
|
||||||
@ -332,7 +339,7 @@ packages:
|
|||||||
name: stream_channel
|
name: stream_channel
|
||||||
url: "https://pub.dartlang.org"
|
url: "https://pub.dartlang.org"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "1.6.8"
|
version: "2.0.0"
|
||||||
string_scanner:
|
string_scanner:
|
||||||
dependency: transitive
|
dependency: transitive
|
||||||
description:
|
description:
|
||||||
@ -360,7 +367,7 @@ packages:
|
|||||||
name: test_api
|
name: test_api
|
||||||
url: "https://pub.dartlang.org"
|
url: "https://pub.dartlang.org"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "0.2.2"
|
version: "0.2.4"
|
||||||
typed_data:
|
typed_data:
|
||||||
dependency: transitive
|
dependency: transitive
|
||||||
description:
|
description:
|
||||||
@ -411,5 +418,5 @@ packages:
|
|||||||
source: hosted
|
source: hosted
|
||||||
version: "2.1.15"
|
version: "2.1.15"
|
||||||
sdks:
|
sdks:
|
||||||
dart: ">=2.1.0 <3.0.0"
|
dart: ">=2.2.0 <3.0.0"
|
||||||
flutter: ">=1.2.1 <2.0.0"
|
flutter: ">=1.2.1 <2.0.0"
|
||||||
|
@ -24,6 +24,7 @@ dependencies:
|
|||||||
flutter_webview_plugin: ^0.3.1
|
flutter_webview_plugin: ^0.3.1
|
||||||
flutter_secure_storage: ^3.2.0
|
flutter_secure_storage: ^3.2.0
|
||||||
device_info: ^0.4.0+1
|
device_info: ^0.4.0+1
|
||||||
|
flutter_local_notifications: ^0.7.1+3
|
||||||
|
|
||||||
dev_dependencies:
|
dev_dependencies:
|
||||||
flutter_test:
|
flutter_test:
|
||||||
|
Reference in New Issue
Block a user