Skip to content

Commit

Permalink
Merge pull request #37 from sebastian-rahlf/bugfix/update-access-token
Browse files Browse the repository at this point in the history
Improve access token handling
  • Loading branch information
silverhairs authored Feb 12, 2024
2 parents cfd7997 + cbad39c commit 1d9b9c4
Showing 1 changed file with 11 additions and 10 deletions.
21 changes: 11 additions & 10 deletions homeconnect/lib/src/client/client_dart.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class HomeConnectApi {
throw Exception('No authenticator provided');
}
final token = await authenticator!.authorize(baseUrl, credentials);
_accessToken = token.accessToken;
storage.setCredentials(token);
}

Expand All @@ -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<http.Response> 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) {
Expand All @@ -71,8 +74,7 @@ class HomeConnectApi {
}

Future<http.Response> 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,
Expand All @@ -86,8 +88,7 @@ class HomeConnectApi {
}

Future<http.Response> 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,
Expand Down Expand Up @@ -130,9 +131,8 @@ class HomeConnectApi {

Future<void> 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(
Expand Down Expand Up @@ -170,6 +170,7 @@ class HomeConnectApi {
}

Future<void> logout() async {
_accessToken = '';
await storage.clearCredentials();
}
}

0 comments on commit 1d9b9c4

Please sign in to comment.