This repository has been archived on 2023-11-13. You can view files and clone it, but cannot push or open issues or pull requests.
imagini/web_native/lib/api/auth_client/io_auth_client.dart
2021-04-05 20:40:23 -04:00

57 lines
1.7 KiB
Dart

import "package:http/http.dart";
import 'package:http/io_client.dart';
import "package:imagini/api/auth_client/base_auth_client.dart";
import "package:imagini/core/storage_client/base_storage_client.dart";
BaseClient getAuthClient(storage) => IOAuthClient(storage);
class IOAuthClient extends IOClient implements BaseAuthClient {
BaseStorageClient _storage;
String _cachedServer;
String _cachedAccessToken;
String _cachedRefreshToken;
IOAuthClient(BaseStorageClient storage) {
_storage = storage;
}
@override
String getServer() {
return _cachedServer;
}
@override
Map<String, String> getHeaders() {
return {
'X-Imagini-AccessToken': _cachedAccessToken,
'X-Imagini-RefreshToken': _cachedRefreshToken,
};
}
@override
Future<IOStreamedResponse> send(BaseRequest request) async {
String _accessToken = await _storage.get("accessToken");
String _refreshToken = await _storage.get("refreshToken");
_cachedAccessToken = _accessToken;
_cachedRefreshToken = _refreshToken;
request.headers.addAll({
'X-Imagini-AccessToken': _accessToken,
'X-Imagini-RefreshToken': _refreshToken,
});
return super.send(request).then((response) async {
if (response.headers.containsKey("x-imagini-accesstoken")) {
_cachedAccessToken = response.headers["x-imagini-accesstoken"];
await _storage.set("accessToken", response.headers["x-imagini-accesstoken"]);
}
if (response.headers.containsKey("x-imagini-refreshtoken")) {
_cachedRefreshToken = response.headers["x-imagini-refreshtoken"];
await _storage.set("refreshToken", response.headers["x-imagini-refreshtoken"]);
}
return response;
});
}
}