diff --git a/homeconnect/lib/src/client/client_dart.dart b/homeconnect/lib/src/client/client_dart.dart index d68d2b4..907bfd4 100644 --- a/homeconnect/lib/src/client/client_dart.dart +++ b/homeconnect/lib/src/client/client_dart.dart @@ -34,6 +34,7 @@ class HomeConnectApi { throw Exception('No authenticator provided'); } final token = await authenticator!.authorize(baseUrl, credentials); + _accessToken = token.accessToken; storage.setCredentials(token); } @@ -50,17 +51,19 @@ class HomeConnectApi { throw Exception('No authenticator provided'); } final userCredentials = await storage.getCredentials(); - final tokens = await authenticator?.refresh(baseUrl, userCredentials!.refreshToken); + if (userCredentials == null) { + throw Exception('Failed to refresh token'); + } + final tokens = await authenticator?.refresh(baseUrl, userCredentials.refreshToken); if (tokens == null) { throw Exception('Failed to refresh token'); } - // set token in storage + _accessToken = tokens.accessToken; await storage.setCredentials(tokens); } Future put({required String resource, required String body}) async { - HomeConnectAuthCredentials? userCredentials = await checkTokenIntegrity(); - _accessToken = userCredentials!.accessToken; + await checkTokenIntegrity(); final uri = baseUrl.join('/api/homeappliances/$resource'); final response = await client.put(uri, headers: commonHeaders, body: body); if (response.statusCode >= 200 && response.statusCode < 300) { @@ -71,8 +74,7 @@ class HomeConnectApi { } Future get(String resource) async { - HomeConnectAuthCredentials? userCredentials = await checkTokenIntegrity(); - _accessToken = userCredentials!.accessToken; + await checkTokenIntegrity(); final uri = baseUrl.join('/api/homeappliances/$resource'); final response = await client.get( uri, @@ -86,8 +88,7 @@ class HomeConnectApi { } Future delete(String resource) async { - HomeConnectAuthCredentials? userCredentials = await checkTokenIntegrity(); - _accessToken = userCredentials!.accessToken; + await checkTokenIntegrity(); final uri = baseUrl.join('/api/homeappliances/$resource'); final response = await client.delete( uri, @@ -130,9 +131,8 @@ class HomeConnectApi { Future openEventListenerChannel({required HomeDevice source}) async { final uri = baseUrl.join("/api/homeappliances/${source.info.haId}/events"); - HomeConnectAuthCredentials? userCredentials = await checkTokenIntegrity(); + await checkTokenIntegrity(); EventController eventController = EventController(); - _accessToken = userCredentials!.accessToken; try { EventSource eventSource = await EventSource.connect( @@ -170,6 +170,7 @@ class HomeConnectApi { } Future logout() async { + _accessToken = ''; await storage.clearCredentials(); } }