Skip to content

Commit f6adac1

Browse files
committed
Start feature/errorManagment
1 parent fba34a2 commit f6adac1

File tree

9 files changed

+517
-265
lines changed

9 files changed

+517
-265
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
## 2.0.3-dev
12
## 2.0.2-dev
23
## 2.0.1-dev
34
## 2.0.0-dev

lib/flutter_feathersjs.dart

Lines changed: 66 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,23 @@
11
/// Communicate with your feathers js (https://feathersjs.com/) server from flutter app.
2+
///
3+
/// Documentation at: https://dahkenangnon.github.io/flutter_feathersjs.dart/
4+
///
5+
/// Repository at: https://github.com/Dahkenangnon/flutter_feathersjs.dart
6+
///
7+
/// Pub.dev: https://pub.dev/packages/flutter_feathersjs/
8+
///
9+
/// Demo api at: https://flutter-feathersjs.herokuapp.com/
10+
///
11+
/// Demo app's repo at: https://github.com/Dahkenangnon/flutter_feathersjs_demo
12+
///
13+
///
14+
///
15+
/// Happy hacking
216
library flutter_feathersjs;
317

4-
import 'package:flutter_feathersjs/src/constants.dart';
518
import 'package:flutter_feathersjs/src/rest_client.dart';
619
import 'package:flutter_feathersjs/src/scketio_client.dart';
20+
import 'package:flutter_feathersjs/src/utils.dart';
721
import 'package:meta/meta.dart';
822
//import 'package:flutter/foundation.dart' as Foundation;
923

@@ -22,7 +36,7 @@ import 'package:meta/meta.dart';
2236
///
2337
/// Authentification is processed by:
2438
///
25-
/// // Global auth (Rest and Socketio)
39+
/// //Global auth (Rest and Socketio)
2640
/// var rep = await flutterFeathersjs
2741
/// .authenticate(userName: user["email"], password: user["password"]);
2842
///
@@ -36,9 +50,6 @@ class FlutterFeathersjs {
3650
//SocketioClient
3751
SocketioClient scketio;
3852

39-
// For debug purpose
40-
bool dev = false;
41-
4253
///Using singleton
4354
static final FlutterFeathersjs _flutterFeathersjs =
4455
FlutterFeathersjs._internal();
@@ -67,65 +78,65 @@ class FlutterFeathersjs {
6778
@required String userName,
6879
@required String password,
6980
String userNameFieldName = "email"}) async {
70-
//Hold global auth infos
71-
Map<String, dynamic> authResponse = {
72-
"error": true,
73-
"error_zone": "UNKNOWN",
74-
"message": "An error occured either on rest or socketio auth",
75-
"restResponse": {},
76-
"scketResponse": {}
77-
};
81+
try {
82+
//Auth with rest to refresh or create accessToken
83+
var restAuthResponse = await rest.authenticate(
84+
strategy: strategy,
85+
userName: userName,
86+
userNameFieldName: userNameFieldName,
87+
password: password);
7888

79-
//Auth with rest to refresh or create accessToken
80-
Map<String, dynamic> restAuthResponse = await rest.authenticate(
81-
strategy: strategy,
82-
userName: userName,
83-
userNameFieldName: userNameFieldName,
84-
password: password);
85-
//Then auth with jwt socketio
86-
Map<String, dynamic> socketioAuthResponse = await scketio.authWithJWT();
89+
try {
90+
//Then auth with jwt socketio
91+
bool isAuthenticated = await scketio.authWithJWT();
8792

88-
//Finally send response
89-
if (!restAuthResponse["error"] && !socketioAuthResponse["error"]) {
90-
authResponse = restAuthResponse;
91-
} else {
92-
authResponse["restResponse"] = restAuthResponse;
93-
authResponse["scketResponse"] = socketioAuthResponse;
93+
// Check wether both client are authenticated or not
94+
if (restAuthResponse != null && isAuthenticated == true) {
95+
return restAuthResponse;
96+
} else {
97+
// Both failed
98+
throw new FeatherJsError(
99+
type: FeatherJsErrorType.IS_AUTH_FAILED_ERROR,
100+
error: "Auth failed with unknown reason");
101+
}
102+
} on FeatherJsError catch (e) {
103+
// Socketio failed
104+
throw new FeatherJsError(type: e.type, error: e);
105+
}
106+
} on FeatherJsError catch (e) {
107+
// Rest failed
108+
throw new FeatherJsError(type: e.type, error: e);
94109
}
95-
return authResponse;
96110
}
97111

98-
/// Authenticate rest and scketio clients so you can use both of them
112+
/// ReAuthenticate rest and scketio clients
99113
///
100114
///___________________________________________________________________
101-
Future<Map<String, dynamic>> reAuthenticate() async {
102-
//Hold global auth infos
103-
Map<String, dynamic> authResponse = {
104-
"error": true,
105-
"error_zone": Constants.UNKNOWN_ERROR,
106-
"message": "An error occured either on rest or socketio auth",
107-
"restResponse": {},
108-
"scketResponse": {}
109-
};
115+
Future<dynamic> reAuthenticate() async {
116+
try {
117+
//Auth with rest to refresh or create accessToken
118+
bool isRestAuthenticated = await rest.reAuthenticate();
110119

111-
//Auth with rest to refresh or create accessToken
112-
Map<String, dynamic> restAuthResponse = await rest.reAuthenticate();
113-
//Then auth with jwt socketio
114-
Map<String, dynamic> socketioAuthResponse = await scketio.authWithJWT();
120+
try {
121+
//Then auth with jwt socketio
122+
bool isSocketioAuthenticated = await scketio.authWithJWT();
115123

116-
//Finally send response
117-
if (!restAuthResponse["error"] && !socketioAuthResponse["error"]) {
118-
authResponse["error"] = false;
119-
authResponse["error_zone"] = Constants.BOTH_CLIENT_AUTHED;
120-
authResponse["message"] = socketioAuthResponse["message"];
121-
} else {
122-
authResponse["error"] = true;
123-
authResponse["error_zone"] = Constants.ONE_OR_BOTH_CLIENT_NOT_AUTHED;
124-
authResponse["message"] =
125-
"One or both client is(are) not authed. Please checkout restResponse field or scketResponse field for more infos.";
126-
authResponse["restResponse"] = restAuthResponse;
127-
authResponse["scketResponse"] = socketioAuthResponse;
124+
// Check wether both client are authenticated or not
125+
if (isRestAuthenticated == true && isSocketioAuthenticated == true) {
126+
return true;
127+
} else {
128+
// Both failed
129+
throw new FeatherJsError(
130+
type: FeatherJsErrorType.IS_AUTH_FAILED_ERROR,
131+
error: "Auth failed with unknown reason");
132+
}
133+
} on FeatherJsError catch (e) {
134+
// Socketio failed
135+
throw new FeatherJsError(type: e.type, error: e);
136+
}
137+
} on FeatherJsError catch (e) {
138+
// Rest failed
139+
throw new FeatherJsError(type: e.type, error: e);
128140
}
129-
return authResponse;
130141
}
131142
}

lib/src/constants.dart

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,6 @@
11
import 'package:meta/meta.dart';
22

3-
class Constants {
4-
static const String INVALID_CREDENTIALS = "INVALID_CREDENTIALS";
5-
static const String JWT_EXPIRED_ERROR = "JWT_EXPIRED_ERROR";
6-
static const String NO_ERROR = "NO_ERROR";
7-
static const String UNKNOWN_ERROR = "UNKNOWN_ERROR";
8-
static const String JWT_ERROR = "JWT_ERROR";
9-
static const String JWT_NOT_FOUND = "JWT_NOT_FOUND";
10-
static const String INVALID_STRATEGY = "INVALID_STRATEGY";
11-
static const String DIO_ERROR = "DIO_ERROR";
12-
13-
static const String AUTH_WITH_JWT_SUCCEED = "AUTH_WITH_JWT_SUCCEED";
14-
static const String AUTH_WITH_JWT_FAILED = "AUTH_WITH_JWT_FAILED";
15-
16-
static const String BOTH_CLIENT_AUTHED = "BOTH_CLIENT_AUTHED";
17-
static const String ONE_OR_BOTH_CLIENT_NOT_AUTHED =
18-
"ONE_OR_BOTH_CLIENT_NOT_AUTHED";
19-
static const String FEATHERSJS_ACCESS_TOKEN = "FEATHERSJS_ACCESS_TOKEN";
20-
}
3+
const String FEATHERSJS_ACCESS_TOKEN = "FEATHERSJS_ACCESS_TOKEN";
214

225
class Connected {}
236

0 commit comments

Comments
 (0)