@@ -9,6 +9,7 @@ class Client {
99 bool initialized = false;
1010 Dio http;
1111 late PersistCookieJar cookieJar;
12+ late SharedPreferences _prefs;
1213
1314 Client({this.endPoint = '{{spec .endpoint }}', this.selfSigned = false, Dio? http}) : this.http = http ?? Dio() {
1415 // Platform is not supported in web so if web, set type to web automatically and skip Platform check
@@ -110,6 +111,8 @@ class Client {
110111 addHeader('user-agent', '${packageInfo.appName}/${packageInfo.version} $device');
111112 } else {
112113 // if web set withCredentials true to make cookies work
114+ _prefs = await SharedPreferences.getInstance();
115+ addHeader('X-Fallback-Cookies', _prefs.getString('cookieFallback') ?? '');
113116 this.http.options.extra['withCredentials'] = true;
114117 }
115118
@@ -139,20 +142,28 @@ class Client {
139142 listFormat: ListFormat.multiCompatible
140143 );
141144
145+ late Response res;
142146 try {
143147 if(headers['content-type'] == 'multipart/form-data') {
144- return await http.request(path, data: FormData.fromMap(params, ListFormat.multiCompatible), options: options);
145- }
146-
147- if (method == HttpMethod.get) {
148+ res = await http.request(path, data: FormData.fromMap(params, ListFormat.multiCompatible), options: options);
149+ } else if (method == HttpMethod.get) {
148150 params.keys.forEach((key) {if (params[key] is int || params[key] is double) {
149151 params[key] = params[key].toString();
150152 }});
151153
152- return await http.get(path, queryParameters: params, options: options);
154+ res = await http.get(path, queryParameters: params, options: options);
153155 } else {
154- return await http.request(path, data: params, options: options);
156+ res = await http.request(path, data: params, options: options);
157+ }
158+ if(kIsWeb) {
159+ final cookieFallback = res.headers.value('X-Fallback-Cookies');
160+ if(cookieFallback != null) {
161+ print('Appwrite is using localStorage for session management. Increase your security by adding a custom domain as your API endpoint.');
162+ addHeader('X-Fallback-Cookies',cookieFallback);
163+ _prefs.setString('cookieFallback', cookieFallback);
164+ }
155165 }
166+ return res;
156167 } on DioError catch(e) {
157168 if(e.response == null) {
158169 throw {{spec .title | caseUcfirst }}Exception(e.message);
0 commit comments