@capgo/capacitor-social-login 0.0.38 → 0.0.40

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
@@ -8,13 +8,18 @@
8
8
 
9
9
  All social logins in one plugin
10
10
 
11
- WIP: Code is ready we ar now polishing documentation
12
-
13
11
  This plugin implement social auth for:
14
12
  - Google (with credential manager)
15
13
  - Apple (with 0auth on android)
16
14
  - Facebook ( with latest SDK)
17
15
 
16
+ We plan in the future to keep adding others social login and make this plugin the all in one solution.
17
+
18
+ FAQ:
19
+
20
+ - Do we support web login?
21
+ => Not yet but PR are welcome, our focus is the native part for now as any Web sdk will work for web
22
+
18
23
  ## Install
19
24
 
20
25
  ```bash
@@ -29,12 +29,18 @@ import org.json.JSONObject;
29
29
  public class GoogleProvider implements SocialProvider {
30
30
 
31
31
  private static final String LOG_TAG = "GoogleProvider";
32
+ private static final String SHARED_PREFERENCE_NAME =
33
+ "GOOGLE_LOGIN_F13oz0I_SHARED_PERF";
34
+ private static final String GOOGLE_DATA_PREFERENCE =
35
+ "GOOGLE_LOGIN_GOOGLE_DATA_9158025e-947d-4211-ba51-40451630cc47";
32
36
 
33
37
  private final Activity activity;
34
38
  private final Context context;
35
39
  private CredentialManager credentialManager;
36
40
  private String clientId;
37
41
 
42
+ private String idToken = null;
43
+
38
44
  public GoogleProvider(Activity activity, Context context) {
39
45
  this.activity = activity;
40
46
  this.context = context;
@@ -43,6 +49,30 @@ public class GoogleProvider implements SocialProvider {
43
49
  public void initialize(String clientId) {
44
50
  this.credentialManager = CredentialManager.create(activity);
45
51
  this.clientId = clientId;
52
+
53
+ String data = context
54
+ .getSharedPreferences(SHARED_PREFERENCE_NAME, Context.MODE_PRIVATE)
55
+ .getString(GOOGLE_DATA_PREFERENCE, null);
56
+
57
+ if (data == null || data.isEmpty()) {
58
+ Log.i(SocialLoginPlugin.LOG_TAG, "No data to restore for google login");
59
+ return;
60
+ }
61
+ try {
62
+ JSONObject object = new JSONObject(data);
63
+ GoogleProvider.this.idToken = object.optString("idToken", null);
64
+
65
+ Log.i(
66
+ SocialLoginPlugin.LOG_TAG,
67
+ String.format("Google restoreState: %s", object)
68
+ );
69
+ } catch (JSONException e) {
70
+ Log.e(
71
+ SocialLoginPlugin.LOG_TAG,
72
+ "Google restoreState: Failed to parse JSON",
73
+ e
74
+ );
75
+ }
46
76
  }
47
77
 
48
78
  @Override
@@ -95,6 +125,21 @@ public class GoogleProvider implements SocialProvider {
95
125
  );
96
126
  }
97
127
 
128
+ private void persistState(
129
+ String idToken
130
+ ) throws JSONException {
131
+ JSONObject object = new JSONObject();
132
+ object.put("idToken", idToken);
133
+
134
+ GoogleProvider.this.idToken = idToken;
135
+
136
+ activity
137
+ .getSharedPreferences(SHARED_PREFERENCE_NAME, Context.MODE_PRIVATE)
138
+ .edit()
139
+ .putString(GOOGLE_DATA_PREFERENCE, object.toString())
140
+ .apply();
141
+ }
142
+
98
143
  private void handleSignInResult(
99
144
  GetCredentialResponse result,
100
145
  PluginCall call
@@ -103,8 +148,19 @@ public class GoogleProvider implements SocialProvider {
103
148
  JSObject user = handleSignInResult(result);
104
149
  JSObject response = new JSObject();
105
150
  response.put("provider", "google");
106
- response.put("status", "success");
107
- response.put("user", user);
151
+ JSObject resultObj = new JSObject();
152
+
153
+ Credential credential = result.getCredential();
154
+ if (credential instanceof CustomCredential) {
155
+ if (GoogleIdTokenCredential.TYPE_GOOGLE_ID_TOKEN_CREDENTIAL.equals(credential.getType())) {
156
+ GoogleIdTokenCredential googleIdTokenCredential = GoogleIdTokenCredential.createFrom(credential.getData());
157
+ resultObj.put("idToken", googleIdTokenCredential.getIdToken());
158
+ persistState(googleIdTokenCredential.getIdToken());
159
+ }
160
+ }
161
+
162
+ resultObj.put("profile", user);
163
+ response.put("result", resultObj);
108
164
  Log.d(LOG_TAG, "Google Sign-In success: " + response.toString());
109
165
  call.resolve(response);
110
166
  } catch (JSONException e) {
@@ -162,6 +218,12 @@ public class GoogleProvider implements SocialProvider {
162
218
  new CredentialManagerCallback<Void, ClearCredentialException>() {
163
219
  @Override
164
220
  public void onResult(Void result) {
221
+ context
222
+ .getSharedPreferences(SHARED_PREFERENCE_NAME, Context.MODE_PRIVATE)
223
+ .edit()
224
+ .clear()
225
+ .apply();
226
+ GoogleProvider.this.idToken = null;
165
227
  call.resolve();
166
228
  }
167
229
 
@@ -176,61 +238,18 @@ public class GoogleProvider implements SocialProvider {
176
238
 
177
239
  @Override
178
240
  public void getAuthorizationCode(PluginCall call) {
179
- GetGoogleIdOption googleIdOption = new GetGoogleIdOption.Builder()
180
- .setFilterByAuthorizedAccounts(false)
181
- .setServerClientId(this.clientId)
182
- .build();
183
- GetCredentialRequest request = new GetCredentialRequest.Builder()
184
- .addCredentialOption(googleIdOption)
185
- .build();
186
-
187
- Executor executor = Executors.newSingleThreadExecutor();
188
- credentialManager.getCredentialAsync(
189
- context,
190
- request,
191
- null,
192
- executor,
193
- new CredentialManagerCallback<
194
- GetCredentialResponse,
195
- GetCredentialException
196
- >() {
197
- @Override
198
- public void onResult(GetCredentialResponse result) {
199
- Credential credential = result.getCredential();
200
- if (credential instanceof CustomCredential) {
201
- if (
202
- GoogleIdTokenCredential.TYPE_GOOGLE_ID_TOKEN_CREDENTIAL.equals(
203
- credential.getType()
204
- )
205
- ) {
206
- GoogleIdTokenCredential googleIdTokenCredential =
207
- GoogleIdTokenCredential.createFrom(
208
- ((CustomCredential) credential).getData()
209
- );
210
- String idToken = googleIdTokenCredential.getIdToken();
211
- JSObject response = new JSObject();
212
- response.put("code", idToken);
213
- call.resolve(response);
214
- } else {
215
- call.reject("Unexpected credential type");
216
- }
217
- } else {
218
- call.reject("Invalid credential");
219
- }
220
- }
221
-
222
- @Override
223
- public void onError(GetCredentialException e) {
224
- Log.e(LOG_TAG, "Failed to get authorization code", e);
225
- call.reject("Failed to get authorization code: " + e.getMessage());
226
- }
227
- }
228
- );
241
+ JSObject response = new JSObject();
242
+ if (GoogleProvider.this.idToken == null) {
243
+ call.reject("Not logged in to google, cannot get authorization code!");
244
+ return;
245
+ }
246
+ response.put("jwt", GoogleProvider.this.idToken);
247
+ call.resolve(response);
229
248
  }
230
249
 
231
250
  @Override
232
251
  public void isLoggedIn(PluginCall call) {
233
- call.resolve(new JSObject().put("isLoggedIn", true));
252
+ call.resolve(new JSObject().put("isLoggedIn", GoogleProvider.this.idToken != null));
234
253
  }
235
254
 
236
255
  @Override
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@capgo/capacitor-social-login",
3
- "version": "0.0.38",
3
+ "version": "0.0.40",
4
4
  "description": "All social logins in one plugin",
5
5
  "main": "dist/plugin.cjs.js",
6
6
  "module": "dist/esm/index.js",