2021-02-20 19:10:25 +00:00
|
|
|
import "package:http/http.dart";
|
2021-04-06 00:40:23 +00:00
|
|
|
import 'package:http/browser_client.dart';
|
|
|
|
import "package:imagini/api/auth_client/base_auth_client.dart";
|
2021-02-20 19:10:25 +00:00
|
|
|
import "package:imagini/core/storage_client/base_storage_client.dart";
|
|
|
|
|
2021-04-06 00:40:23 +00:00
|
|
|
BaseClient getAuthClient(storage) => BrowserAuthClient(storage);
|
2021-02-20 19:10:25 +00:00
|
|
|
|
2021-04-06 00:40:23 +00:00
|
|
|
class BrowserAuthClient extends BrowserClient implements BaseAuthClient {
|
2021-02-20 19:10:25 +00:00
|
|
|
BaseStorageClient _storage;
|
2021-04-06 00:40:23 +00:00
|
|
|
String _cachedServer;
|
|
|
|
String _cachedAccessToken;
|
|
|
|
String _cachedRefreshToken;
|
2021-02-20 19:10:25 +00:00
|
|
|
|
2021-04-06 00:40:23 +00:00
|
|
|
BrowserAuthClient(BaseStorageClient storage) {
|
2021-02-20 19:10:25 +00:00
|
|
|
_storage = storage;
|
|
|
|
}
|
|
|
|
|
2021-04-06 00:40:23 +00:00
|
|
|
@override
|
|
|
|
String getServer() {
|
|
|
|
return _cachedServer;
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Map<String, String> getHeaders() {
|
|
|
|
return {
|
|
|
|
'X-Imagini-AccessToken': _cachedAccessToken,
|
|
|
|
'X-Imagini-RefreshToken': _cachedRefreshToken,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-02-20 19:10:25 +00:00
|
|
|
@override
|
|
|
|
Future<StreamedResponse> send(BaseRequest request) async {
|
|
|
|
String _accessToken = await _storage.get("accessToken");
|
|
|
|
String _refreshToken = await _storage.get("refreshToken");
|
|
|
|
|
2021-04-06 00:40:23 +00:00
|
|
|
_cachedAccessToken = _accessToken;
|
|
|
|
_cachedRefreshToken = _refreshToken;
|
|
|
|
|
2021-02-20 19:10:25 +00:00
|
|
|
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")) {
|
2021-04-06 00:40:23 +00:00
|
|
|
_cachedAccessToken = response.headers["x-imagini-accesstoken"];
|
2021-02-20 19:10:25 +00:00
|
|
|
await _storage.set("accessToken", response.headers["x-imagini-accesstoken"]);
|
|
|
|
}
|
|
|
|
if (response.headers.containsKey("x-imagini-refreshtoken")) {
|
2021-04-06 00:40:23 +00:00
|
|
|
_cachedRefreshToken = response.headers["x-imagini-refreshtoken"];
|
2021-02-20 19:10:25 +00:00
|
|
|
await _storage.set("refreshToken", response.headers["x-imagini-refreshtoken"]);
|
|
|
|
}
|
|
|
|
return response;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|