Login error handling improvements

This commit is contained in:
estevez-dev
2019-09-04 23:40:37 +03:00
parent 56cd8963d7
commit 996fbf7bba
3 changed files with 22 additions and 15 deletions

View File

@ -768,6 +768,16 @@ class _MainPageState extends State<MainPage> with WidgetsBindingObserver, Ticker
break; break;
} }
case HAErrorActionType.RELOGIN: {
actions.add(FlatButton(
child: Text("${action.title}", style: textStyle),
onPressed: () {
ConnectionManager().logout().then((_) => _fullLoad());
},
));
break;
}
case HAErrorActionType.URL: { case HAErrorActionType.URL: {
actions.add(FlatButton( actions.add(FlatButton(
child: Text("${action.title}", style: textStyle), child: Text("${action.title}", style: textStyle),

View File

@ -54,21 +54,20 @@ class ConnectionManager {
completer.completeError(HAError.checkConnectionSettings()); completer.completeError(HAError.checkConnectionSettings());
stopInit = true; stopInit = true;
} else { } else {
//_token = prefs.getString('hassio-token');
final storage = new FlutterSecureStorage(); final storage = new FlutterSecureStorage();
try { try {
_token = await storage.read(key: "hacl_llt"); _token = await storage.read(key: "hacl_llt");
Logger.e("Long-lived token read successful"); Logger.e("Long-lived token read successful");
} catch (e) {
Logger.e("Cannt read secure storage. Need to relogin.");
_token = null;
await storage.delete(key: "hacl_llt");
}
oauthUrl = "$httpWebHost/auth/authorize?client_id=${Uri.encodeComponent( oauthUrl = "$httpWebHost/auth/authorize?client_id=${Uri.encodeComponent(
'http://ha-client.homemade.systems/')}&redirect_uri=${Uri 'http://ha-client.homemade.systems/')}&redirect_uri=${Uri
.encodeComponent( .encodeComponent(
'http://ha-client.homemade.systems/service/auth_callback.html')}"; 'http://ha-client.homemade.systems/service/auth_callback.html')}";
settingsLoaded = true; settingsLoaded = true;
} catch (e) {
completer.completeError(HAError("Error reading login details", actions: [HAErrorAction.tryAgain(type: HAErrorActionType.FULL_RELOAD), HAErrorAction.loginAgain()]));
Logger.e("Cannt read secure storage. Need to relogin.");
stopInit = true;
}
} }
} else { } else {
if ((_domain == null) || (_port == null) || if ((_domain == null) || (_port == null) ||
@ -148,9 +147,7 @@ class ConnectionManager {
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()]));
_messageResolver.remove("auth"); _messageResolver.remove("auth");
logout().then((_) { if (!connecting.isCompleted) connecting.completeError(HAError("${data["message"]}", actions: [HAErrorAction.tryAgain(title: "Retry"), HAErrorAction.loginAgain(title: "Relogin")]));
if (!connecting.isCompleted) connecting.completeError(HAError("${data["message"]}", actions: [HAErrorAction.loginAgain()]));
});
} else { } else {
_handleMessage(data); _handleMessage(data);
} }
@ -281,6 +278,7 @@ class ConnectionManager {
} }
Future logout() { Future logout() {
Logger.d("Logging out");
Completer completer = Completer(); Completer completer = Completer();
_disconnect().whenComplete(() { _disconnect().whenComplete(() {
_token = null; _token = null;
@ -309,8 +307,7 @@ class ConnectionManager {
throw e; throw e;
}); });
}).catchError((e) { }).catchError((e) {
logout(); completer.completeError(HAError("Authentication error: $e", actions: [HAErrorAction.reload(title: "Retry"), HAErrorAction.loginAgain(title: "Relogin")]));
completer.completeError(HAError("Authentication error: $e", actions: [HAErrorAction.loginAgain()]));
}); });
return completer.future; return completer.future;
} }

View File

@ -77,16 +77,16 @@ class HAErrorAction {
const HAErrorAction.reload({this.title = "Reload", this.type = HAErrorActionType.FULL_RELOAD, this.url}); const HAErrorAction.reload({this.title = "Reload", this.type = HAErrorActionType.FULL_RELOAD, this.url});
const HAErrorAction.loginAgain({this.title = "Login again", this.type = HAErrorActionType.FULL_RELOAD, this.url}); const HAErrorAction.loginAgain({this.title = "Login again", this.type = HAErrorActionType.RELOGIN, this.url});
} }
class HAErrorActionType { class HAErrorActionType {
static const FULL_RELOAD = 0; static const FULL_RELOAD = 0;
static const QUICK_RELOAD = 1; static const QUICK_RELOAD = 1;
static const LOGOUT = 2;
static const URL = 3; static const URL = 3;
static const OPEN_CONNECTION_SETTINGS = 4; static const OPEN_CONNECTION_SETTINGS = 4;
static const RELOGIN = 5;
} }
class StateChangedEvent { class StateChangedEvent {