|
| 1 | +import 'package:flutter_feathersjs/src/rest_client.dart'; |
| 2 | +import 'package:flutter_feathersjs/src/scketio_client.dart'; |
| 3 | +import 'dart:async'; |
| 4 | +import 'package:flutter_feathersjs/src/helper.dart'; |
| 5 | +import 'package:meta/meta.dart'; |
| 6 | + |
| 7 | +///FlutterFeatherJs allow you to communicate with your feathers js server |
| 8 | +/// |
| 9 | +///Response format: You get exactly what feathers server send when no error |
| 10 | +/// |
| 11 | +///Uploading file: Use rest client, socketio client cannot upload file |
| 12 | +/// |
| 13 | +class FlutterFeathersjs { |
| 14 | + //RestClient |
| 15 | + RestClient rest; |
| 16 | + //SocketioClient |
| 17 | + SocketioClient scketio; |
| 18 | + |
| 19 | + ///Using singleton |
| 20 | + static final FlutterFeathersjs _flutterFeathersjs = |
| 21 | + FlutterFeathersjs._internal(); |
| 22 | + factory FlutterFeathersjs() { |
| 23 | + return _flutterFeathersjs; |
| 24 | + } |
| 25 | + FlutterFeathersjs._internal(); |
| 26 | + |
| 27 | + ///Intialize both rest and scoketio client |
| 28 | + init({@required String baseUrl, Map<String, dynamic> extraHeaders}) { |
| 29 | + rest = new RestClient()..init(baseUrl: baseUrl, extraHeaders: extraHeaders); |
| 30 | + |
| 31 | + scketio = new SocketioClient()..init(baseUrl: baseUrl); |
| 32 | + } |
| 33 | + |
| 34 | + /// Authenticate rest and scketio clients so you can use both of them |
| 35 | + /// |
| 36 | + ///___________________________________________________________________ |
| 37 | + /// @params `username` can be : email, phone, etc; |
| 38 | + /// |
| 39 | + /// But ensure that `userNameFieldName` is correct with your chosed `strategy` |
| 40 | + /// |
| 41 | + /// By default this will be `email`and the strategy `local` |
| 42 | + Future<Map<String, dynamic>> authenticate( |
| 43 | + {String strategy = "local", |
| 44 | + @required String userName, |
| 45 | + @required String password, |
| 46 | + String userNameFieldName = "email"}) async { |
| 47 | + try { |
| 48 | + //Auth with rest to refresh or create accessToken |
| 49 | + var restAuthResponse = await rest.authenticate( |
| 50 | + strategy: strategy, |
| 51 | + userName: userName, |
| 52 | + userNameFieldName: userNameFieldName, |
| 53 | + password: password); |
| 54 | + |
| 55 | + try { |
| 56 | + //Then auth with jwt socketio |
| 57 | + bool isAuthenticated = await scketio.authWithJWT(); |
| 58 | + |
| 59 | + // Check wether both client are authenticated or not |
| 60 | + if (restAuthResponse != null && isAuthenticated == true) { |
| 61 | + return restAuthResponse; |
| 62 | + } else { |
| 63 | + // Both failed |
| 64 | + throw new FeatherJsError( |
| 65 | + type: FeatherJsErrorType.IS_AUTH_FAILED_ERROR, |
| 66 | + error: "Auth failed with unknown reason"); |
| 67 | + } |
| 68 | + } on FeatherJsError catch (e) { |
| 69 | + // Socketio failed |
| 70 | + throw new FeatherJsError(type: e.type, error: e); |
| 71 | + } |
| 72 | + } on FeatherJsError catch (e) { |
| 73 | + // Rest failed |
| 74 | + throw new FeatherJsError(type: e.type, error: e); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + /// ReAuthenticate rest and scketio clients |
| 79 | + /// |
| 80 | + ///___________________________________________________________________ |
| 81 | + Future<dynamic> reAuthenticate() async { |
| 82 | + try { |
| 83 | + //Auth with rest to refresh or create accessToken |
| 84 | + bool isRestAuthenticated = await rest.reAuthenticate(); |
| 85 | + |
| 86 | + try { |
| 87 | + //Then auth with jwt socketio |
| 88 | + bool isSocketioAuthenticated = await scketio.authWithJWT(); |
| 89 | + |
| 90 | + // Check wether both client are authenticated or not |
| 91 | + if (isRestAuthenticated == true && isSocketioAuthenticated == true) { |
| 92 | + return true; |
| 93 | + } else { |
| 94 | + // Both failed |
| 95 | + throw new FeatherJsError( |
| 96 | + type: FeatherJsErrorType.IS_AUTH_FAILED_ERROR, |
| 97 | + error: "Auth failed with unknown reason"); |
| 98 | + } |
| 99 | + } on FeatherJsError catch (e) { |
| 100 | + // Socketio failed |
| 101 | + throw new FeatherJsError(type: e.type, error: e); |
| 102 | + } |
| 103 | + } on FeatherJsError catch (e) { |
| 104 | + // Rest failed |
| 105 | + throw new FeatherJsError(type: e.type, error: e); |
| 106 | + } |
| 107 | + } |
| 108 | +} |
0 commit comments