|
1 | 1 | import 'dart:async'; |
2 | 2 |
|
3 | 3 | import 'package:dio/dio.dart'; |
4 | | -import 'package:feathersjs/src/featherjs_client_base.dart'; |
| 4 | +import 'package:flutter_feathersjs/src/featherjs_client_base.dart'; |
5 | 5 | import 'package:meta/meta.dart'; |
6 | 6 |
|
7 | | -class RestClient extends FeathersJsClient { |
8 | | - ///////////////////////////////////////////////////////////////////// |
| 7 | +/// Feathers Js rest client |
| 8 | +class RestClient extends FlutterFeathersjs { |
| 9 | + ///Dio as http client |
9 | 10 | Dio dio; |
10 | | - //////////////////////////////////////////////////////////////////////// |
11 | 11 |
|
12 | 12 | RestClient({@required String baseUrl, Map<String, dynamic> extraHeaders}) { |
13 | 13 | //Setup Http client |
14 | 14 | dio = Dio(BaseOptions( |
15 | 15 | baseUrl: baseUrl, |
16 | 16 | headers: extraHeaders, |
17 | 17 | )); |
| 18 | + |
| 19 | + dio.interceptors |
| 20 | + .add(InterceptorsWrapper(onRequest: (RequestOptions options) async { |
| 21 | + // Do something before request is sent |
| 22 | + //Adding stored token early with sembast |
| 23 | + var oldToken = await this.utils.getRestAccessToken(); |
| 24 | + dio.options.headers["Authorization"] = "Bearer $oldToken"; |
| 25 | + return options; //continue |
| 26 | + // If you want to resolve the request with some custom data, |
| 27 | + // you can return a `Response` object or return `dio.resolve(data)`. |
| 28 | + // If you want to reject the request with a error message, |
| 29 | + // you can return a `DioError` object or return `dio.reject(errMsg)` |
| 30 | + }, onResponse: (Response response) async { |
| 31 | + // Do something with response data |
| 32 | + return response; // continue |
| 33 | + }, onError: (DioError e) async { |
| 34 | + // Do something with response error |
| 35 | + |
| 36 | + if (e.response != null) { |
| 37 | + // print(e.response.data); |
| 38 | + // print(e.response.headers); |
| 39 | + // print(e.response.request); |
| 40 | + //Only send the error message from feathers js server not for Dio |
| 41 | + return e.response.data; //continue |
| 42 | + } else { |
| 43 | + // Something happened in setting up or sending the request that triggered an Error |
| 44 | + // print(e.request); |
| 45 | + // print(e.message); |
| 46 | + //By returning null, it means that error is from client |
| 47 | + return null; |
| 48 | + } |
| 49 | + })); |
| 50 | + } |
| 51 | + |
| 52 | + Future<dynamic> reAuthenticate({String serviceName = "users"}) async { |
| 53 | + //AsyncTask manager |
| 54 | + Completer asyncTask = Completer<dynamic>(); |
| 55 | + Map<String, dynamic> authResponse = { |
| 56 | + "error": true, |
| 57 | + "error_zone": "UNKNOWN", |
| 58 | + "message": "An error occured" |
| 59 | + }; |
| 60 | + |
| 61 | + //Getting the early sotored rest access token and send the request by using it |
| 62 | + var oldToken = await this.utils.getRestAccessToken(); |
| 63 | + |
| 64 | + //If an oldToken exist really |
| 65 | + if (oldToken != null) { |
| 66 | + dio.options.headers["Authorization"] = "Bearer $oldToken"; |
| 67 | + var response = |
| 68 | + await find(serviceName: "$serviceName", query: {"\$limit": 1}); |
| 69 | + |
| 70 | + if (response.statusCode == 401) { |
| 71 | + print("jwt expired or jwt malformed"); |
| 72 | + authResponse["error"] = true; |
| 73 | + authResponse["message"] = "jwt expired"; |
| 74 | + authResponse["error_zone"] = "JWT_EXPIRED_ERROR"; |
| 75 | + } else if (response.statusCode == 200) { |
| 76 | + print("Jwt still validated"); |
| 77 | + authResponse["error"] = false; |
| 78 | + authResponse["message"] = "Jwt still validated"; |
| 79 | + authResponse["error_zone"] = "NO_ERROR"; |
| 80 | + } else { |
| 81 | + print("Unknown error"); |
| 82 | + authResponse["error"] = true; |
| 83 | + authResponse["message"] = "Unknown error"; |
| 84 | + authResponse["error_zone"] = "UNKNOWN_ERROR"; |
| 85 | + } |
| 86 | + } else { |
| 87 | + print("No old token found. Must reAuth user"); |
| 88 | + authResponse["error"] = true; |
| 89 | + authResponse["message"] = "No old token found. Must reAuth user"; |
| 90 | + authResponse["error_zone"] = "JWT_NOT_FOUND"; |
| 91 | + } |
| 92 | + asyncTask.complete(authResponse); |
| 93 | + return asyncTask.future; |
18 | 94 | } |
19 | 95 |
|
20 | | - Future<bool> authenticate( |
| 96 | + Future<dynamic> authenticate( |
21 | 97 | {strategy = "local", String email, String password}) async { |
22 | | - Completer asyncTask = Completer<bool>(); |
| 98 | + Completer asyncTask = Completer<dynamic>(); |
| 99 | + |
| 100 | + Map<String, dynamic> authResponse = { |
| 101 | + "error": true, |
| 102 | + "error_zone": "UNKNOWN", |
| 103 | + "message": "An error occured" |
| 104 | + }; |
| 105 | + |
23 | 106 | try { |
24 | 107 | //Making http request to get auth token |
25 | 108 | var response = await dio.post("/authentication", |
26 | 109 | data: {"strategy": strategy, "email": email, "password": password}); |
27 | | - var token = response.data['accessToken']; |
28 | | - dio.options.headers["Authorization"] = "Bearer $token"; |
29 | | - asyncTask.complete(true); |
| 110 | + //Check is auth is successfully before storing token |
| 111 | + if (response.data["code"] != null && response.data["code"] == 401) { |
| 112 | + //This is useful to display to end user why auth failed |
| 113 | + //With 401: it will be either Invalid credentials or strategy error |
| 114 | + if (response.data["message"] == "Invalid login") { |
| 115 | + authResponse["error_zone"] = "INVALID_CREDENTIALS"; |
| 116 | + } else { |
| 117 | + authResponse["error_zone"] = "INVALID_STRATEGY"; |
| 118 | + } |
| 119 | + authResponse["error"] = true; |
| 120 | + authResponse["message"] = response.data["message"]; |
| 121 | + } else if (response.data['accessToken'] != null) { |
| 122 | + authResponse["error"] = false; |
| 123 | + authResponse["message"] = response.data["user"]; |
| 124 | + authResponse["error_zone"] = "NO_ERROR"; |
| 125 | + |
| 126 | + //Store the authenticated user |
| 127 | + await this |
| 128 | + .utils |
| 129 | + .setAuthenticatedFeathersUser(user: response.data['user']); |
| 130 | + //Storing the token |
| 131 | + await this |
| 132 | + .utils |
| 133 | + .setRestAccessToken(token: response.data['accessToken']); |
| 134 | + } else { |
| 135 | + //Unknown error |
| 136 | + authResponse["error"] = true; |
| 137 | + authResponse["message"] = "Unknown error occured"; |
| 138 | + } |
30 | 139 | } catch (e) { |
31 | | - asyncTask.complete(false); |
| 140 | + //Error caught by Dio |
| 141 | + authResponse["error"] = true; |
| 142 | + authResponse["message"] = e; |
| 143 | + authResponse["error_zone"] = "DIO_ERROR"; |
32 | 144 | } |
| 145 | + //Send response |
| 146 | + asyncTask.complete(authResponse); |
33 | 147 | return asyncTask.future; |
34 | 148 | } |
35 | 149 |
|
|
0 commit comments