@falconeta/capacitor-aws-amplify 0.0.15 → 0.0.16

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -37,10 +37,17 @@ import java.util.Objects;
37
37
  import java.util.function.Consumer;
38
38
 
39
39
  public class AwsAmplify {
40
-
40
+ boolean isLoaded;
41
41
  @RequiresApi(api = Build.VERSION_CODES.N)
42
42
  public void load(JSObject cognitoConfig, Context context, @NonNull Consumer onSuccess, @NonNull Consumer<Exception> onError) {
43
43
 
44
+
45
+ if (isLoaded) {
46
+ onSuccess.accept(null);
47
+ return;
48
+ }
49
+ isLoaded = true;
50
+
44
51
  JSObject oauth = (JSObject) cognitoConfig.getJSObject("oauth");
45
52
 
46
53
  try {
@@ -187,6 +194,31 @@ public class AwsAmplify {
187
194
  });
188
195
  }
189
196
 
197
+ @RequiresApi(api = Build.VERSION_CODES.N)
198
+ public void getUserAttributes(@Nullable Consumer<JSObject> onSuccess,
199
+ @Nullable Consumer<Exception> onError) {
200
+ fetchUserAttributesInternal(
201
+ userAttributes -> {
202
+ JSObject ret = new JSObject();
203
+ ret.put("status", 0);
204
+ ret.put("userAttributes", userAttributes);
205
+ onSuccess.accept(ret);
206
+ },
207
+ error -> {
208
+ if (onError != null) {
209
+ String message = error.getMessage();
210
+ String suggestion = error.getRecoverySuggestion();
211
+ Throwable cause = error.getCause();
212
+ JSObject ret = new JSObject();
213
+ ret.put("status", -1);
214
+ if (AuthException.SignedOutException.class.isInstance(error)) {
215
+ ret.put("status", -3);
216
+ }
217
+ onSuccess.accept(ret);
218
+ }
219
+ });
220
+ }
221
+
190
222
  @RequiresApi(api = Build.VERSION_CODES.N)
191
223
  private void fetchSessionInternal(@Nullable Consumer<AwsAuthSession> onSuccess,
192
224
  @Nullable Consumer<AuthException> onError) {
@@ -237,6 +269,27 @@ public class AwsAmplify {
237
269
  });
238
270
  }
239
271
 
272
+ @RequiresApi(api = Build.VERSION_CODES.N)
273
+ private void fetchUserAttributesInternal(@Nullable Consumer<JSObject> onSuccess,
274
+ @Nullable Consumer<AuthException> onError) {
275
+ Amplify.Auth.fetchUserAttributes(
276
+ attributes -> {
277
+ var userAttributes = new JSObject();
278
+ attributes.forEach(attribute -> {
279
+ userAttributes.put(attribute.getKey().getKeyString(), attribute.getValue());
280
+ });
281
+
282
+ onSuccess.accept(userAttributes);
283
+ },
284
+ error -> {
285
+ // Log.e(TAG, "Session error: ", error);
286
+
287
+ if (onError != null) {
288
+ onError.accept(error);
289
+ }
290
+ });
291
+ }
292
+
240
293
  private AwsAuthSession getSessionInternal() {
241
294
  try {
242
295
  AWSMobileClient mClient = AWSMobileClient.getInstance();
@@ -64,4 +64,15 @@ public class AwsAmplifyPlugin extends Plugin {
64
64
  call.reject(error.toString());
65
65
  });
66
66
  }
67
+
68
+ @RequiresApi(api = Build.VERSION_CODES.N)
69
+ @PluginMethod
70
+ public void getUserAttributes(PluginCall call) {
71
+
72
+ implementation.getUserAttributes(
73
+ result -> call.resolve(result),
74
+ error -> {
75
+ call.reject(error.toString());
76
+ });
77
+ }
67
78
  }
@@ -184,6 +184,43 @@ import AWSMobileClient
184
184
  }
185
185
  }
186
186
 
187
+ public func getUserAttributes(
188
+ onSuccess: @escaping (JSObject) -> (),
189
+ onError: @escaping (any Error) -> ()
190
+ ) {
191
+ var ret: JSObject = [:]
192
+ Amplify.Auth.fetchUserAttributes { result in
193
+ do {
194
+ let attributes = try result.get()
195
+
196
+ var userAttributes: JSObject = [:]
197
+
198
+ attributes.forEach { attribute in
199
+ userAttributes[attribute.key.rawValue] = attribute.value
200
+ }
201
+ ret["userAttributes"] = userAttributes
202
+ ret["status"] = 0
203
+
204
+ print("\(self.TAG) - Fetch user attributes successfully")
205
+ onSuccess(ret)
206
+ } catch {
207
+ ret["status"] = -1
208
+ if let authError = error as? AuthError
209
+ {
210
+ let cognitoAuthError = authError
211
+ switch cognitoAuthError {
212
+ case .signedOut:
213
+ ret["status"] = -3
214
+ default:
215
+ ret["status"] = -1
216
+ }
217
+ }
218
+ onSuccess(ret)
219
+ print("\(self.TAG) Fetch user attributes failed with error - \(error)")
220
+ }
221
+ }
222
+ }
223
+
187
224
  public func getCurrentUser() -> AuthUser {
188
225
  return Amplify.Auth.getCurrentUser()!
189
226
  }
@@ -94,6 +94,17 @@ public class AwsAmplifyPlugin: CAPPlugin {
94
94
  call.reject(error.localizedDescription)
95
95
  })
96
96
  }
97
+
98
+ @objc func getUserAttributes(_ call: CAPPluginCall) {
99
+
100
+ self.implementation.getUserAttributes(
101
+ onSuccess: {session in
102
+ call.resolve(session)
103
+ },
104
+ onError: {error in
105
+ call.reject(error.localizedDescription)
106
+ })
107
+ }
97
108
 
98
109
  // var permissionCallID: String?
99
110
  // var locationManager: CLLocationManager?
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@falconeta/capacitor-aws-amplify",
3
- "version": "0.0.15",
3
+ "version": "0.0.16",
4
4
  "description": "plugin that handle amplify features",
5
5
  "main": "dist/plugin.cjs.js",
6
6
  "module": "dist/esm/index.js",