57 lines
1.7 KiB
Dart
57 lines
1.7 KiB
Dart
import "package:http/http.dart";
|
|
import 'package:http/browser_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) => BrowserAuthClient(storage);
|
|
|
|
class BrowserAuthClient extends BrowserClient implements BaseAuthClient {
|
|
BaseStorageClient _storage;
|
|
String _cachedServer;
|
|
String _cachedAccessToken;
|
|
String _cachedRefreshToken;
|
|
|
|
BrowserAuthClient(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<StreamedResponse> 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;
|
|
});
|
|
}
|
|
}
|