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
216library flutter_feathersjs;
317
4- import 'package:flutter_feathersjs/src/constants.dart' ;
518import 'package:flutter_feathersjs/src/rest_client.dart' ;
619import 'package:flutter_feathersjs/src/scketio_client.dart' ;
20+ import 'package:flutter_feathersjs/src/utils.dart' ;
721import '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}
0 commit comments