Skip to content

Commit e7df1bb

Browse files
committed
Change sembast dependencies to shared_preferences
1 parent b1a8eb8 commit e7df1bb

File tree

10 files changed

+183
-56
lines changed

10 files changed

+183
-56
lines changed

CHANGELOG.md

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@
55

66
## 0.0.2-dev
77

8-
- Add auth and reAuth for both rest and socketio client
9-
- Add socketio methods
10-
- Test socketio emit methods
11-
- Add global auth and reAuth which don't matter whatever you're calling via socketio or rest
8+
- Add auth and reAuth for both rest and socketio client
9+
- Add socketio methods
10+
- Test socketio emit methods
11+
- Add global auth and reAuth which don't matter whatever you're calling via socketio or rest
12+
13+
## 0.0.3-dev
14+
15+
- Change sembast dependencies to shared_preferences as sembast cause "OS Error: Read-only file system, errno = 30 in flutter"

example/pubspec.lock

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,6 @@ packages:
5757
url: "https://pub.dartlang.org"
5858
source: hosted
5959
version: "3.0.10"
60-
event_bus:
61-
dependency: transitive
62-
description:
63-
name: event_bus
64-
url: "https://pub.dartlang.org"
65-
source: hosted
66-
version: "1.1.1"
6760
fake_async:
6861
dependency: transitive
6962
description:
@@ -82,7 +75,7 @@ packages:
8275
name: flutter_feathersjs
8376
url: "https://pub.dartlang.org"
8477
source: hosted
85-
version: "0.0.1-dev"
78+
version: "0.0.2-dev"
8679
flutter_test:
8780
dependency: "direct dev"
8881
description: flutter

lib/flutter_feathersjs.dart

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
library flutter_feathersjs;
22

3+
import 'package:flutter_feathersjs/src/constants.dart';
34
import 'package:flutter_feathersjs/src/rest_client.dart';
45
import 'package:flutter_feathersjs/src/scketio_client.dart';
56
import 'package:meta/meta.dart';
@@ -10,6 +11,7 @@ class FlutterFeathersjs {
1011
RestClient rest;
1112
//SocketioClient
1213
SocketioClient scketio;
14+
Constants isCode;
1315

1416
///Using singleton
1517
static final FlutterFeathersjs _flutterFeathersjs =
@@ -44,7 +46,7 @@ class FlutterFeathersjs {
4446
Map<String, dynamic> socketioAuthResponse = await scketio.authWithJWT();
4547

4648
//Finally send response
47-
if (!restAuthResponse["error"] && socketioAuthResponse["error"]) {
49+
if (!restAuthResponse["error"] && !socketioAuthResponse["error"]) {
4850
authResponse = restAuthResponse;
4951
} else {
5052
authResponse["restResponse"] = restAuthResponse;
@@ -58,7 +60,7 @@ class FlutterFeathersjs {
5860
//Hold global auth infos
5961
Map<String, dynamic> authResponse = {
6062
"error": true,
61-
"error_zone": "UNKNOWN",
63+
"error_zone": isCode.UNKNOWN_ERROR,
6264
"message": "An error occured either on rest or socketio auth",
6365
"restResponse": {},
6466
"scketResponse": {}
@@ -72,11 +74,11 @@ class FlutterFeathersjs {
7274
//Finally send response
7375
if (!restAuthResponse["error"] && !socketioAuthResponse["error"]) {
7476
authResponse["error"] = false;
75-
authResponse["error_zone"] = "BOTH_CLIENT_AUTHED";
77+
authResponse["error_zone"] = isCode.BOTH_CLIENT_AUTHED;
7678
authResponse["message"] = socketioAuthResponse;
7779
} else {
7880
authResponse["error"] = true;
79-
authResponse["error_zone"] = "ONE_OR_BOTH_CLIENT_NOT_AUTHED";
81+
authResponse["error_zone"] = isCode.ONE_OR_BOTH_CLIENT_NOT_AUTHED;
8082
authResponse["message"] =
8183
"One or both client is(are) not authed. Please checkout restResponse field or scketResponse field for more infos.";
8284
authResponse["restResponse"] = restAuthResponse;

lib/src/constants.dart

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Constants {
2+
final INVALID_CREDENTIALS = "INVALID_CREDENTIALS";
3+
final JWT_EXPIRED_ERROR = "JWT_EXPIRED_ERROR";
4+
final NO_ERROR = "NO_ERROR";
5+
final UNKNOWN_ERROR = "UNKNOWN_ERROR";
6+
final JWT_ERROR = "JWT_ERROR";
7+
final JWT_NOT_FOUND = "JWT_NOT_FOUND";
8+
final INVALID_STRATEGY = "INVALID_STRATEGY";
9+
final DIO_ERROR = "DIO_ERROR";
10+
11+
final AUTH_WITH_JWT_SUCCEED = "AUTH_WITH_JWT_SUCCEED";
12+
final AUTH_WITH_JWT_FAILED = "AUTH_WITH_JWT_FAILED";
13+
14+
final BOTH_CLIENT_AUTHED = "BOTH_CLIENT_AUTHED";
15+
final ONE_OR_BOTH_CLIENT_NOT_AUTHED = "ONE_OR_BOTH_CLIENT_NOT_AUTHED";
16+
}

lib/src/rest_client.dart

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,14 @@ import 'package:flutter_feathersjs/src/featherjs_client_base.dart';
55
import 'package:flutter_feathersjs/src/utils.dart';
66
import 'package:meta/meta.dart';
77

8+
import 'constants.dart';
9+
810
/// Feathers Js rest client for rest api call
911
class RestClient extends FlutterFeathersjs {
1012
///Dio as http client
1113
Dio dio;
1214
Utils utils;
15+
Constants isCode;
1316

1417
//Using singleton to ensure we the same instance of it
1518
static final RestClient _restClient = RestClient._internal();
@@ -88,23 +91,23 @@ class RestClient extends FlutterFeathersjs {
8891
print("jwt expired or jwt malformed");
8992
authResponse["error"] = true;
9093
authResponse["message"] = "jwt expired";
91-
authResponse["error_zone"] = "JWT_EXPIRED_ERROR";
94+
authResponse["error_zone"] = isCode..JWT_EXPIRED_ERROR;
9295
} else if (response.statusCode == 200) {
9396
print("Jwt still validated");
9497
authResponse["error"] = false;
9598
authResponse["message"] = "Jwt still validated";
96-
authResponse["error_zone"] = "NO_ERROR";
99+
authResponse["error_zone"] = isCode.NO_ERROR;
97100
} else {
98101
print("Unknown error");
99102
authResponse["error"] = true;
100103
authResponse["message"] = "Unknown error";
101-
authResponse["error_zone"] = "UNKNOWN_ERROR";
104+
authResponse["error_zone"] = isCode.UNKNOWN_ERROR;
102105
}
103106
} catch (e) {
104107
print("Unable to connect to the server");
105108
authResponse["error"] = true;
106109
authResponse["message"] = e;
107-
authResponse["error_zone"] = "JWT_ERROR";
110+
authResponse["error_zone"] = isCode.JWT_ERROR;
108111
}
109112
}
110113

@@ -113,7 +116,7 @@ class RestClient extends FlutterFeathersjs {
113116
print("No old token found. Must reAuth user");
114117
authResponse["error"] = true;
115118
authResponse["message"] = "No old token found. Must reAuth user";
116-
authResponse["error_zone"] = "JWT_NOT_FOUND";
119+
authResponse["error_zone"] = isCode.JWT_NOT_FOUND;
117120
}
118121
asyncTask.complete(authResponse);
119122
return asyncTask.future;
@@ -139,16 +142,16 @@ class RestClient extends FlutterFeathersjs {
139142
//This is useful to display to end user why auth failed
140143
//With 401: it will be either Invalid credentials or strategy error
141144
if (response.data["message"] == "Invalid login") {
142-
authResponse["error_zone"] = "INVALID_CREDENTIALS";
145+
authResponse["error_zone"] = isCode.INVALID_CREDENTIALS;
143146
} else {
144-
authResponse["error_zone"] = "INVALID_STRATEGY";
147+
authResponse["error_zone"] = isCode.INVALID_STRATEGY;
145148
}
146149
authResponse["error"] = true;
147150
authResponse["message"] = response.data["message"];
148151
} else if (response.data['accessToken'] != null) {
149152
authResponse["error"] = false;
150153
authResponse["message"] = response.data["user"];
151-
authResponse["error_zone"] = "NO_ERROR";
154+
authResponse["error_zone"] = isCode.NO_ERROR;
152155

153156
//Storing the token
154157
utils.setAccessToken(token: response.data['accessToken']);
@@ -161,7 +164,7 @@ class RestClient extends FlutterFeathersjs {
161164
//Error caught by Dio
162165
authResponse["error"] = true;
163166
authResponse["message"] = e;
164-
authResponse["error_zone"] = "DIO_ERROR";
167+
authResponse["error_zone"] = isCode.DIO_ERROR;
165168
}
166169
//Send response
167170
asyncTask.complete(authResponse);

lib/src/scketio_client.dart

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import 'dart:async';
22

3+
import 'package:flutter_feathersjs/src/constants.dart';
34
import 'package:flutter_feathersjs/src/featherjs_client_base.dart';
45
import 'package:flutter_feathersjs/src/utils.dart';
56
import 'package:socket_io_client/socket_io_client.dart' as IO;
@@ -10,6 +11,7 @@ class SocketioClient extends FlutterFeathersjs {
1011
IO.Socket _socket;
1112
bool dev = true;
1213
Utils utils;
14+
Constants isCode;
1315

1416
//Using singleton
1517
static final SocketioClient _socketioClient = SocketioClient._internal();
@@ -67,7 +69,7 @@ class SocketioClient extends FlutterFeathersjs {
6769

6870
Map<String, dynamic> authResponse = {
6971
"error": true,
70-
"error_zone": "UNKNOWN",
72+
"error_zone": isCode.UNKNOWN_ERROR,
7173
"message": "An error occured"
7274
};
7375
Completer asyncTask = Completer<dynamic>();
@@ -88,7 +90,7 @@ class SocketioClient extends FlutterFeathersjs {
8890
//Auth is ok
8991
authResponse["error"] = false;
9092
authResponse["message"] = dataResponse[1];
91-
authResponse["error_zone"] = "AUTH_WITH_JWT_SUCCEED";
93+
authResponse["error_zone"] = isCode.AUTH_WITH_JWT_SUCCEED;
9294

9395
//Every emit or on will be authed
9496
this._socket.io.options['extraHeaders'] = {
@@ -99,7 +101,7 @@ class SocketioClient extends FlutterFeathersjs {
99101
//Auth is not ok
100102
authResponse["error"] = true;
101103
authResponse["message"] = dataResponse;
102-
authResponse["error_zone"] = "AUTH_WITH_JWT_FAILED";
104+
authResponse["error_zone"] = isCode.AUTH_WITH_JWT_FAILED;
103105
}
104106
asyncTask.complete(authResponse);
105107
});

lib/src/utils.dart

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,26 @@
11
//Help FlutterFeathersJs
22
import 'dart:async';
3-
import 'package:sembast/sembast.dart';
4-
import 'package:sembast/sembast_io.dart';
3+
4+
import 'package:shared_preferences/shared_preferences.dart';
55

66
class Utils {
77
/////////////////////////////////////////////////////////////////////
8-
// File path to a file in the current directory
9-
String dbPath = 'flutter_feathersjs.db';
10-
DatabaseFactory dbFactory = databaseFactoryIo;
11-
Database db;
8+
SharedPreferences prefs;
129

1310
////////////////////////////////////////////////////////////////////////
1411
1512
Utils();
1613

1714
//Allow to set rest access token
1815
Future<bool> setAccessToken({String token}) async {
19-
//Open th db
20-
db = await dbFactory.openDatabase(dbPath);
21-
var store = StoreRef.main();
22-
return await store.record('feathersjs_access_token').put(db, token);
16+
prefs = await SharedPreferences.getInstance();
17+
18+
return await prefs.setString('feathersjs_access_token', token);
2319
}
2420

2521
//Allow to get rest access token
2622
Future<String> getAccessToken({String token}) async {
27-
//Open db
28-
db = await dbFactory.openDatabase(dbPath);
29-
var store = StoreRef.main();
30-
//Get token from it
31-
return await store.record('feathersjs_access_token').get(db) as String;
23+
prefs = await SharedPreferences.getInstance();
24+
return await prefs.getString('feathersjs_access_token');
3225
}
3326
}

0 commit comments

Comments
 (0)