Skip to content

Commit 896bb5d

Browse files
authored
Merge pull request #173 from lohanidamodar/feat-fallback-cookie-support
feat-fallback-cookie-support-flutter
2 parents affde4f + f044519 commit 896bb5d

File tree

3 files changed

+20
-7
lines changed

3 files changed

+20
-7
lines changed

templates/flutter/lib/client.dart.twig

Lines changed: 17 additions & 6 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,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);

templates/flutter/lib/package.dart.twig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ library {{ language.params.packageName }};
22

33
import 'dart:io';
44
import 'dart:convert';
5+
import 'package:shared_preferences/shared_preferences.dart';
56
import 'package:universal_html/html.dart' as html;
67
import 'package:dio/dio.dart';
78
import 'package:flutter/foundation.dart';

templates/flutter/pubspec.yaml.twig

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,14 @@ dependencies:
1111
flutter:
1212
sdk: flutter
1313
cookie_jar: ^3.0.1
14+
device_info_plus: ^1.0.0
1415
dio: ^4.0.0
1516
dio_cookie_manager: ^2.0.0
1617
flutter_web_auth: ^0.3.0
1718
package_info_plus: ^1.0.0
1819
path_provider: ^2.0.1
20+
shared_preferences: ^2.0.5
1921
universal_html: ^2.0.8
20-
device_info_plus: ^1.0.0
2122
2223
dev_dependencies:
2324
flutter_test:

0 commit comments

Comments
 (0)