Login errors handling improvements

This commit is contained in:
estevez-dev 2019-09-03 23:44:03 +03:00
parent 0d2f1cf9aa
commit 6a8cee2cc2
4 changed files with 26 additions and 14 deletions

View File

@ -120,4 +120,5 @@ class ErrorCode {
static const GENERAL_AUTH_ERROR = 11;
static const AUTH_ERROR = 12;
static const NOT_LOGGED_IN = 13;
static const SECURE_STORAGE_READ_ERROR = 14;
}

View File

@ -515,7 +515,6 @@ class _MainPageState extends State<MainPage> with WidgetsBindingObserver, Ticker
);
}
//TODO remove this shit
void _callService(String domain, String service, String entityId, Map additionalParams) {
_showInfoBottomBar(
message: "Calling $domain.$service",

View File

@ -58,16 +58,16 @@ class ConnectionManager {
final storage = new FlutterSecureStorage();
try {
_token = await storage.read(key: "hacl_llt");
oauthUrl = "$httpWebHost/auth/authorize?client_id=${Uri.encodeComponent(
'http://ha-client.homemade.systems/')}&redirect_uri=${Uri
.encodeComponent(
'http://ha-client.homemade.systems/service/auth_callback.html')}";
settingsLoaded = true;
} catch (e) {
Logger.e("Cannt read secure storage. Need to relogin.");
_token = null;
await storage.delete(key: "hacl_llt");
completer.completeError(UserError(code: ErrorCode.SECURE_STORAGE_READ_ERROR));
stopInit = true;
}
oauthUrl = "$httpWebHost/auth/authorize?client_id=${Uri.encodeComponent(
'http://ha-client.homemade.systems/')}&redirect_uri=${Uri
.encodeComponent(
'http://ha-client.homemade.systems/service/auth_callback.html')}";
settingsLoaded = true;
}
} else {
if ((_domain == null) || (_port == null) ||
@ -147,10 +147,7 @@ class ConnectionManager {
Logger.d("[Received] <== ${data.toString()}");
_messageResolver["auth"]?.completeError(UserError(code: ErrorCode.AUTH_INVALID, message: "${data["message"]}"));
_messageResolver.remove("auth");
//TODO dont logout, show variants to User
logout().then((_) {
if (!connecting.isCompleted) connecting.completeError(UserError(code: ErrorCode.AUTH_INVALID, message: "${data["message"]}"));
});
if (!connecting.isCompleted) connecting.completeError(UserError(code: ErrorCode.AUTH_INVALID, message: "${data["message"]}"));
} else {
_handleMessage(data);
}
@ -309,8 +306,6 @@ class ConnectionManager {
throw e;
});
}).catchError((e) {
//TODO dont logout, show variants
logout();
completer.completeError(UserError(code: ErrorCode.AUTH_ERROR, message: "$e"));
});
return completer.future;

View File

@ -75,6 +75,7 @@ class UserErrorScreen extends StatelessWidget {
break;
}
case ErrorCode.GENERAL_AUTH_ERROR: {
errorText = "There was some error logging in. ${this.error.message ?? ""}";
buttons.addAll(<Widget>[
RaisedButton(
onPressed: () => _reload(),
@ -89,6 +90,22 @@ class UserErrorScreen extends StatelessWidget {
);
break;
}
case ErrorCode.SECURE_STORAGE_READ_ERROR: {
errorText = "There was an error reading secure storage. You can try again or clear saved auth data and login again.";
buttons.addAll(<Widget>[
RaisedButton(
onPressed: () => _reload(),
child: Text("Retry")
),
Container(width: 15.0,),
RaisedButton(
onPressed: () => _reLogin(),
child: Text("Clear and login again"),
)
]
);
break;
}
case ErrorCode.DISCONNECTED: {
errorText = "Disconnected";
buttons.addAll(<Widget>[