Skip to content

Commit 409b8b4

Browse files
committed
fallback cookie
1 parent 70c112a commit 409b8b4

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

templates/flutter/lib/client.dart.twig

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -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,27 @@ 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);
148+
res = await http.request(path, data: FormData.fromMap(params, ListFormat.multiCompatible), options: options);
145149
}
146150

147151
if (method == HttpMethod.get) {
148152
params.keys.forEach((key) {if (params[key] is int || params[key] is double) {
149153
params[key] = params[key].toString();
150154
}});
151155

152-
return await http.get(path, queryParameters: params, options: options);
156+
res = await http.get(path, queryParameters: params, options: options);
153157
} else {
154-
return await http.request(path, data: params, options: options);
158+
res = await http.request(path, data: params, options: options);
155159
}
160+
final cookieFallback = res.headers.value('X-Fallback-Cookies');
161+
if(cookieFallback != null) {
162+
print('Appwrite is using localStorage for session management. Increase your security by adding a custom domain as your API endpoint.');
163+
prefs.setString('cookieFallback', cookieFallback);
164+
}
165+
return res;
156166
} on DioError catch(e) {
157167
if(e.response == null) {
158168
throw {{spec.title | caseUcfirst}}Exception(e.message);

0 commit comments

Comments
 (0)