@capgo/capacitor-social-login 0.0.71 → 0.0.76

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.
package/README.md CHANGED
@@ -233,6 +233,7 @@ const res = await SocialLogin.login({
233
233
  * [`getAuthorizationCode(...)`](#getauthorizationcode)
234
234
  * [`refresh(...)`](#refresh)
235
235
  * [Interfaces](#interfaces)
236
+ * [Type Aliases](#type-aliases)
236
237
 
237
238
  </docgen-index>
238
239
 
@@ -257,16 +258,16 @@ Initialize the plugin
257
258
  ### login(...)
258
259
 
259
260
  ```typescript
260
- login(options: LoginOptions) => Promise<LoginResult>
261
+ login<T extends "apple" | "google" | "facebook">(options: Extract<LoginOptions, { provider: T; }>) => Promise<{ provider: T; result: ProviderResponseMap[T]; }>
261
262
  ```
262
263
 
263
264
  Login with the selected provider
264
265
 
265
- | Param | Type |
266
- | ------------- | ----------------------------------------------------- |
267
- | **`options`** | <code><a href="#loginoptions">LoginOptions</a></code> |
266
+ | Param | Type |
267
+ | ------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
268
+ | **`options`** | <code><a href="#extract">Extract</a>&lt;{ provider: 'facebook'; options: <a href="#facebookloginoptions">FacebookLoginOptions</a>; }, { provider: T; }&gt; \| <a href="#extract">Extract</a>&lt;{ provider: 'google'; options: <a href="#googleloginoptions">GoogleLoginOptions</a>; }, { provider: T; }&gt; \| <a href="#extract">Extract</a>&lt;{ provider: 'apple'; options: <a href="#appleprovideroptions">AppleProviderOptions</a>; }, { provider: T; }&gt;</code> |
268
269
 
269
- **Returns:** <code>Promise&lt;<a href="#loginresult">LoginResult</a>&gt;</code>
270
+ **Returns:** <code>Promise&lt;{ provider: T; result: ProviderResponseMap[T]; }&gt;</code>
270
271
 
271
272
  --------------------
272
273
 
@@ -281,7 +282,7 @@ Logout
281
282
 
282
283
  | Param | Type |
283
284
  | ------------- | ------------------------------------------------------------- |
284
- | **`options`** | <code>{ provider: 'facebook' \| 'google' \| 'apple'; }</code> |
285
+ | **`options`** | <code>{ provider: 'apple' \| 'google' \| 'facebook'; }</code> |
285
286
 
286
287
  --------------------
287
288
 
@@ -347,14 +348,6 @@ Refresh the access token
347
348
  | **`apple`** | <code>{ clientId?: string; redirectUrl?: string; }</code> |
348
349
 
349
350
 
350
- #### LoginResult
351
-
352
- | Prop | Type | Description |
353
- | -------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ----------- |
354
- | **`provider`** | <code>'facebook' \| 'google' \| 'apple' \| 'twitter'</code> | Provider |
355
- | **`result`** | <code><a href="#facebookloginresponse">FacebookLoginResponse</a> \| <a href="#googleloginresponse">GoogleLoginResponse</a> \| <a href="#appleproviderresponse">AppleProviderResponse</a></code> | Payload |
356
-
357
-
358
351
  #### FacebookLoginResponse
359
352
 
360
353
  | Prop | Type |
@@ -397,14 +390,6 @@ Refresh the access token
397
390
  | **`profile`** | <code>{ user: string; email: string \| null; givenName: string \| null; familyName: string \| null; }</code> |
398
391
 
399
392
 
400
- #### LoginOptions
401
-
402
- | Prop | Type | Description |
403
- | -------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ----------- |
404
- | **`provider`** | <code>'facebook' \| 'google' \| 'apple' \| 'twitter'</code> | Provider |
405
- | **`options`** | <code><a href="#facebookloginoptions">FacebookLoginOptions</a> \| <a href="#googleloginoptions">GoogleLoginOptions</a> \| <a href="#appleprovideroptions">AppleProviderOptions</a></code> | Options |
406
-
407
-
408
393
  #### FacebookLoginOptions
409
394
 
410
395
  | Prop | Type | Description | Default |
@@ -416,12 +401,11 @@ Refresh the access token
416
401
 
417
402
  #### GoogleLoginOptions
418
403
 
419
- | Prop | Type | Description | Default | Since |
420
- | ------------------------ | --------------------- | ---------------------------------------------------------------------------------------------------------------------------------------- | ------------------ | ------ |
421
- | **`scopes`** | <code>string[]</code> | Specifies the scopes required for accessing Google APIs The default is defined in the configuration. | | |
422
- | **`nonce`** | <code>string</code> | Nonce | | |
423
- | **`grantOfflineAccess`** | <code>boolean</code> | Set if your application needs to refresh access tokens when the user is not present at the browser. In response use `serverAuthCode` key | <code>false</code> | 0.0.69 |
424
- | **`newUI`** | <code>boolean</code> | New UI | <code>false</code> | 0.1.0 |
404
+ | Prop | Type | Description | Default | Since |
405
+ | ------------------------ | --------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------ | ------ |
406
+ | **`scopes`** | <code>string[]</code> | Specifies the scopes required for accessing Google APIs The default is defined in the configuration. | | |
407
+ | **`nonce`** | <code>string</code> | Nonce | | |
408
+ | **`grantOfflineAccess`** | <code>boolean</code> | Set if your application needs to refresh access tokens when the user is not present at the browser. it will add offline_access to the scopes In response use `serverAuthCode` key | <code>false</code> | 0.0.69 |
425
409
 
426
410
 
427
411
  #### AppleProviderOptions
@@ -437,7 +421,7 @@ Refresh the access token
437
421
 
438
422
  | Prop | Type | Description |
439
423
  | -------------- | ---------------------------------------------- | ----------- |
440
- | **`provider`** | <code>'facebook' \| 'google' \| 'apple'</code> | Provider |
424
+ | **`provider`** | <code>'apple' \| 'google' \| 'facebook'</code> | Provider |
441
425
 
442
426
 
443
427
  #### AuthorizationCode
@@ -451,6 +435,26 @@ Refresh the access token
451
435
 
452
436
  | Prop | Type | Description |
453
437
  | -------------- | ---------------------------------------------- | ----------- |
454
- | **`provider`** | <code>'facebook' \| 'google' \| 'apple'</code> | Provider |
438
+ | **`provider`** | <code>'apple' \| 'google' \| 'facebook'</code> | Provider |
439
+
440
+
441
+ ### Type Aliases
442
+
443
+
444
+ #### ProviderResponseMap
445
+
446
+ <code>{ facebook: <a href="#facebookloginresponse">FacebookLoginResponse</a>; google: <a href="#googleloginresponse">GoogleLoginResponse</a>; apple: <a href="#appleproviderresponse">AppleProviderResponse</a>; }</code>
447
+
448
+
449
+ #### LoginOptions
450
+
451
+ <code>{ provider: "facebook"; options: <a href="#facebookloginoptions">FacebookLoginOptions</a>; } | { provider: "google"; options: <a href="#googleloginoptions">GoogleLoginOptions</a>; } | { provider: "apple"; options: <a href="#appleprovideroptions">AppleProviderOptions</a>; }</code>
452
+
453
+
454
+ #### Extract
455
+
456
+ <a href="#extract">Extract</a> from T those types that are assignable to U
457
+
458
+ <code>T extends U ? T : never</code>
455
459
 
456
460
  </docgen-api>
@@ -11,7 +11,6 @@ import com.facebook.FacebookCallback;
11
11
  import com.facebook.FacebookException;
12
12
  import com.facebook.FacebookSdk;
13
13
  import com.facebook.GraphRequest;
14
- import com.facebook.GraphResponse;
15
14
  import com.facebook.login.LoginBehavior;
16
15
  import com.facebook.login.LoginManager;
17
16
  import com.facebook.login.LoginResult;
@@ -21,6 +20,7 @@ import com.getcapacitor.PluginCall;
21
20
  import ee.forgr.capacitor.social.login.helpers.JsonHelper;
22
21
  import ee.forgr.capacitor.social.login.helpers.SocialProvider;
23
22
  import java.util.Collection;
23
+ import java.util.concurrent.CountDownLatch;
24
24
  import org.json.JSONException;
25
25
  import org.json.JSONObject;
26
26
 
@@ -212,38 +212,48 @@ public class FacebookProvider implements SocialProvider {
212
212
 
213
213
  private JSObject createProfileObject(AccessToken accessToken) {
214
214
  JSObject profileObject = new JSObject();
215
+ CountDownLatch latch = new CountDownLatch(1);
216
+
215
217
  GraphRequest request = GraphRequest.newMeRequest(
216
218
  accessToken,
217
- new GraphRequest.GraphJSONObjectCallback() {
218
- @Override
219
- public void onCompleted(JSONObject object, GraphResponse response) {
220
- if (response.getError() != null) {
221
- Log.e(
222
- LOG_TAG,
223
- "Error fetching profile",
224
- response.getError().getException()
225
- );
226
- } else {
227
- profileObject.put("userID", object.optString("id", ""));
228
- profileObject.put("email", object.optString("email", ""));
229
- profileObject.put("name", object.optString("name", ""));
230
-
231
- JSONObject pictureObject = object.optJSONObject("picture");
232
- if (pictureObject != null) {
233
- JSONObject dataObject = pictureObject.optJSONObject("data");
234
- if (dataObject != null) {
235
- profileObject.put("imageURL", dataObject.optString("url", ""));
236
- }
219
+ (object, response) -> {
220
+ if (response.getError() != null) {
221
+ Log.e(
222
+ LOG_TAG,
223
+ "Error fetching profile",
224
+ response.getError().getException()
225
+ );
226
+ } else {
227
+ profileObject.put("userID", object.optString("id", ""));
228
+ profileObject.put("email", object.optString("email", ""));
229
+ profileObject.put("name", object.optString("name", ""));
230
+
231
+ JSONObject pictureObject = object.optJSONObject("picture");
232
+ if (pictureObject != null) {
233
+ JSONObject dataObject = pictureObject.optJSONObject("data");
234
+ if (dataObject != null) {
235
+ profileObject.put("imageURL", dataObject.optString("url", ""));
237
236
  }
238
- // Add other fields as needed
239
237
  }
240
238
  }
239
+ latch.countDown();
241
240
  }
242
241
  );
242
+
243
243
  Bundle parameters = new Bundle();
244
244
  parameters.putString("fields", "id,name,email,picture.type(large)");
245
245
  request.setParameters(parameters);
246
- request.executeAndWait();
246
+
247
+ new Thread(() -> {
248
+ request.executeAndWait();
249
+ }).start();
250
+
251
+ try {
252
+ latch.await();
253
+ } catch (InterruptedException e) {
254
+ Log.e(LOG_TAG, "Interrupted while waiting for profile fetch", e);
255
+ }
256
+
247
257
  return profileObject;
248
258
  }
249
259
  }
@@ -23,6 +23,8 @@ import com.google.android.libraries.identity.googleid.GetSignInWithGoogleOption;
23
23
  import com.google.android.libraries.identity.googleid.GoogleIdTokenCredential;
24
24
  import ee.forgr.capacitor.social.login.helpers.SocialProvider;
25
25
  import java.io.IOException;
26
+ import java.util.HashSet;
27
+ import java.util.Set;
26
28
  import java.util.concurrent.Callable;
27
29
  import java.util.concurrent.Executor;
28
30
  import java.util.concurrent.ExecutorService;
@@ -93,6 +95,18 @@ public class GoogleProvider implements SocialProvider {
93
95
 
94
96
  // Extract scopes from the config
95
97
  JSONArray scopesArray = config.optJSONArray("scopes");
98
+ boolean grantOfflineAccess = config.optBoolean("grantOfflineAccess", false);
99
+ if (grantOfflineAccess) {
100
+ scopesArray.put("offline_access");
101
+ }
102
+ // Remove duplicates from scopes array
103
+ if (scopesArray != null) {
104
+ Set<String> uniqueScopes = new HashSet<>();
105
+ for (int i = 0; i < scopesArray.length(); i++) {
106
+ uniqueScopes.add(scopesArray.optString(i));
107
+ }
108
+ scopesArray = new JSONArray(uniqueScopes);
109
+ }
96
110
  if (scopesArray != null) {
97
111
  this.scopes = new String[scopesArray.length()];
98
112
  for (int i = 0; i < scopesArray.length(); i++) {
@@ -103,77 +117,40 @@ public class GoogleProvider implements SocialProvider {
103
117
  this.scopes = new String[] { "profile", "email" };
104
118
  }
105
119
 
106
- if (config.optBoolean("newUI", false)) {
107
- GetSignInWithGoogleOption.Builder googleIdOptionBuilder =
108
- new GetSignInWithGoogleOption.Builder(this.clientId);
120
+ GetSignInWithGoogleOption.Builder googleIdOptionBuilder =
121
+ new GetSignInWithGoogleOption.Builder(this.clientId);
109
122
 
110
- if (nonce != null && !nonce.isEmpty()) {
111
- googleIdOptionBuilder.setNonce(nonce);
112
- }
123
+ if (nonce != null && !nonce.isEmpty()) {
124
+ googleIdOptionBuilder.setNonce(nonce);
125
+ }
113
126
 
114
- GetSignInWithGoogleOption googleIdOptionFiltered =
115
- googleIdOptionBuilder.build();
116
- GetCredentialRequest filteredRequest = new GetCredentialRequest.Builder()
117
- .addCredentialOption(googleIdOptionFiltered)
118
- .build();
119
-
120
- Executor executor = Executors.newSingleThreadExecutor();
121
- credentialManager.getCredentialAsync(
122
- context,
123
- filteredRequest,
124
- null,
125
- executor,
126
- new CredentialManagerCallback<
127
- GetCredentialResponse,
128
- GetCredentialException
129
- >() {
130
- @Override
131
- public void onResult(GetCredentialResponse result) {
132
- handleSignInResult(result, call);
133
- }
134
-
135
- @Override
136
- public void onError(GetCredentialException e) {
137
- handleSignInError(e, call);
138
- }
139
- }
140
- );
141
- } else {
142
- GetSignInWithGoogleOption.Builder googleIdOptionBuilder =
143
- new GetSignInWithGoogleOption.Builder(this.clientId);
127
+ GetSignInWithGoogleOption googleIdOptionFiltered =
128
+ googleIdOptionBuilder.build();
129
+ GetCredentialRequest filteredRequest = new GetCredentialRequest.Builder()
130
+ .addCredentialOption(googleIdOptionFiltered)
131
+ .build();
144
132
 
145
- if (nonce != null && !nonce.isEmpty()) {
146
- googleIdOptionBuilder.setNonce(nonce);
147
- }
133
+ Executor executor = Executors.newSingleThreadExecutor();
134
+ credentialManager.getCredentialAsync(
135
+ context,
136
+ filteredRequest,
137
+ null,
138
+ executor,
139
+ new CredentialManagerCallback<
140
+ GetCredentialResponse,
141
+ GetCredentialException
142
+ >() {
143
+ @Override
144
+ public void onResult(GetCredentialResponse result) {
145
+ handleSignInResult(result, call);
146
+ }
148
147
 
149
- GetSignInWithGoogleOption googleIdOptionFiltered =
150
- googleIdOptionBuilder.build();
151
- GetCredentialRequest filteredRequest = new GetCredentialRequest.Builder()
152
- .addCredentialOption(googleIdOptionFiltered)
153
- .build();
154
-
155
- Executor executor = Executors.newSingleThreadExecutor();
156
- credentialManager.getCredentialAsync(
157
- context,
158
- filteredRequest,
159
- null,
160
- executor,
161
- new CredentialManagerCallback<
162
- GetCredentialResponse,
163
- GetCredentialException
164
- >() {
165
- @Override
166
- public void onResult(GetCredentialResponse result) {
167
- handleSignInResult(result, call);
168
- }
169
-
170
- @Override
171
- public void onError(GetCredentialException e) {
172
- handleSignInError(e, call);
173
- }
148
+ @Override
149
+ public void onError(GetCredentialException e) {
150
+ handleSignInError(e, call);
174
151
  }
175
- );
176
- }
152
+ }
153
+ );
177
154
  }
178
155
 
179
156
  private void persistState(String idToken) throws JSONException {
package/dist/docs.json CHANGED
@@ -30,15 +30,15 @@
30
30
  },
31
31
  {
32
32
  "name": "login",
33
- "signature": "(options: LoginOptions) => Promise<LoginResult>",
33
+ "signature": "<T extends \"apple\" | \"google\" | \"facebook\">(options: Extract<LoginOptions, { provider: T; }>) => Promise<{ provider: T; result: ProviderResponseMap[T]; }>",
34
34
  "parameters": [
35
35
  {
36
36
  "name": "options",
37
37
  "docs": "",
38
- "type": "LoginOptions"
38
+ "type": "Extract<{ provider: 'facebook'; options: FacebookLoginOptions; }, { provider: T; }> | Extract<{ provider: 'google'; options: GoogleLoginOptions; }, { provider: T; }> | Extract<{ provider: 'apple'; options: AppleProviderOptions; }, { provider: T; }>"
39
39
  }
40
40
  ],
41
- "returns": "Promise<LoginResult>",
41
+ "returns": "Promise<{ provider: T; result: ProviderResponseMap[T]; }>",
42
42
  "tags": [
43
43
  {
44
44
  "name": "description",
@@ -47,8 +47,10 @@
47
47
  ],
48
48
  "docs": "Login with the selected provider",
49
49
  "complexTypes": [
50
- "LoginResult",
51
- "LoginOptions"
50
+ "T",
51
+ "ProviderResponseMap",
52
+ "LoginOptions",
53
+ "Extract"
52
54
  ],
53
55
  "slug": "login"
54
56
  },
@@ -59,7 +61,7 @@
59
61
  {
60
62
  "name": "options",
61
63
  "docs": "",
62
- "type": "{ provider: 'facebook' | 'google' | 'apple'; }"
64
+ "type": "{ provider: 'apple' | 'google' | 'facebook'; }"
63
65
  }
64
66
  ],
65
67
  "returns": "Promise<void>",
@@ -177,43 +179,6 @@
177
179
  }
178
180
  ]
179
181
  },
180
- {
181
- "name": "LoginResult",
182
- "slug": "loginresult",
183
- "docs": "",
184
- "tags": [],
185
- "methods": [],
186
- "properties": [
187
- {
188
- "name": "provider",
189
- "tags": [
190
- {
191
- "text": "select provider to login with",
192
- "name": "description"
193
- }
194
- ],
195
- "docs": "Provider",
196
- "complexTypes": [],
197
- "type": "'facebook' | 'google' | 'apple' | 'twitter'"
198
- },
199
- {
200
- "name": "result",
201
- "tags": [
202
- {
203
- "text": "payload to login with",
204
- "name": "description"
205
- }
206
- ],
207
- "docs": "Payload",
208
- "complexTypes": [
209
- "FacebookLoginResponse",
210
- "GoogleLoginResponse",
211
- "AppleProviderResponse"
212
- ],
213
- "type": "FacebookLoginResponse | GoogleLoginResponse | AppleProviderResponse"
214
- }
215
- ]
216
- },
217
182
  {
218
183
  "name": "FacebookLoginResponse",
219
184
  "slug": "facebookloginresponse",
@@ -382,43 +347,6 @@
382
347
  }
383
348
  ]
384
349
  },
385
- {
386
- "name": "LoginOptions",
387
- "slug": "loginoptions",
388
- "docs": "",
389
- "tags": [],
390
- "methods": [],
391
- "properties": [
392
- {
393
- "name": "provider",
394
- "tags": [
395
- {
396
- "text": "select provider to login with",
397
- "name": "description"
398
- }
399
- ],
400
- "docs": "Provider",
401
- "complexTypes": [],
402
- "type": "'facebook' | 'google' | 'apple' | 'twitter'"
403
- },
404
- {
405
- "name": "options",
406
- "tags": [
407
- {
408
- "text": "payload to login with",
409
- "name": "description"
410
- }
411
- ],
412
- "docs": "Options",
413
- "complexTypes": [
414
- "FacebookLoginOptions",
415
- "GoogleLoginOptions",
416
- "AppleProviderOptions"
417
- ],
418
- "type": "FacebookLoginOptions | GoogleLoginOptions | AppleProviderOptions"
419
- }
420
- ]
421
- },
422
350
  {
423
351
  "name": "FacebookLoginOptions",
424
352
  "slug": "facebookloginoptions",
@@ -515,27 +443,7 @@
515
443
  "name": "since"
516
444
  }
517
445
  ],
518
- "docs": "Set if your application needs to refresh access tokens when the user is not present at the browser.\nIn response use `serverAuthCode` key",
519
- "complexTypes": [],
520
- "type": "boolean | undefined"
521
- },
522
- {
523
- "name": "newUI",
524
- "tags": [
525
- {
526
- "text": "use new UI for Google login",
527
- "name": "description"
528
- },
529
- {
530
- "text": "false",
531
- "name": "default"
532
- },
533
- {
534
- "text": "0.1.0",
535
- "name": "since"
536
- }
537
- ],
538
- "docs": "New UI",
446
+ "docs": "Set if your application needs to refresh access tokens when the user is not present at the browser. it will add offline_access to the scopes\nIn response use `serverAuthCode` key",
539
447
  "complexTypes": [],
540
448
  "type": "boolean | undefined"
541
449
  }
@@ -607,7 +515,7 @@
607
515
  ],
608
516
  "docs": "Provider",
609
517
  "complexTypes": [],
610
- "type": "'facebook' | 'google' | 'apple'"
518
+ "type": "'apple' | 'google' | 'facebook'"
611
519
  }
612
520
  ]
613
521
  },
@@ -649,12 +557,67 @@
649
557
  ],
650
558
  "docs": "Provider",
651
559
  "complexTypes": [],
652
- "type": "'facebook' | 'google' | 'apple'"
560
+ "type": "'apple' | 'google' | 'facebook'"
653
561
  }
654
562
  ]
655
563
  }
656
564
  ],
657
565
  "enums": [],
658
- "typeAliases": [],
566
+ "typeAliases": [
567
+ {
568
+ "name": "ProviderResponseMap",
569
+ "slug": "providerresponsemap",
570
+ "docs": "",
571
+ "types": [
572
+ {
573
+ "text": "{\n facebook: FacebookLoginResponse;\n google: GoogleLoginResponse;\n apple: AppleProviderResponse;\n}",
574
+ "complexTypes": [
575
+ "FacebookLoginResponse",
576
+ "GoogleLoginResponse",
577
+ "AppleProviderResponse"
578
+ ]
579
+ }
580
+ ]
581
+ },
582
+ {
583
+ "name": "LoginOptions",
584
+ "slug": "loginoptions",
585
+ "docs": "",
586
+ "types": [
587
+ {
588
+ "text": "{\n provider: \"facebook\";\n options: FacebookLoginOptions;\n }",
589
+ "complexTypes": [
590
+ "FacebookLoginOptions"
591
+ ]
592
+ },
593
+ {
594
+ "text": "{\n provider: \"google\";\n options: GoogleLoginOptions;\n }",
595
+ "complexTypes": [
596
+ "GoogleLoginOptions"
597
+ ]
598
+ },
599
+ {
600
+ "text": "{\n provider: \"apple\";\n options: AppleProviderOptions;\n }",
601
+ "complexTypes": [
602
+ "AppleProviderOptions"
603
+ ]
604
+ }
605
+ ]
606
+ },
607
+ {
608
+ "name": "Extract",
609
+ "slug": "extract",
610
+ "docs": "Extract from T those types that are assignable to U",
611
+ "types": [
612
+ {
613
+ "text": "T extends U ? T : never",
614
+ "complexTypes": [
615
+ "T",
616
+ "U"
617
+ ]
618
+ }
619
+ ]
620
+ }
621
+ ],
659
622
  "pluginConfigs": []
660
623
  }