|
1 | 1 | package com.codetrixstudio.capacitor.GoogleAuth; |
2 | 2 |
|
| 3 | +import android.content.Intent; |
| 4 | + |
| 5 | +import com.codetrixstudio.capacitor.GoogleAuth.capacitorgoogleauth.R; |
3 | 6 | import com.getcapacitor.JSObject; |
4 | 7 | import com.getcapacitor.NativePlugin; |
5 | 8 | import com.getcapacitor.Plugin; |
6 | 9 | import com.getcapacitor.PluginCall; |
7 | 10 | import com.getcapacitor.PluginMethod; |
| 11 | +import com.google.android.gms.auth.api.signin.GoogleSignIn; |
| 12 | +import com.google.android.gms.auth.api.signin.GoogleSignInAccount; |
| 13 | +import com.google.android.gms.auth.api.signin.GoogleSignInClient; |
| 14 | +import com.google.android.gms.auth.api.signin.GoogleSignInOptions; |
| 15 | +import com.google.android.gms.common.api.ApiException; |
| 16 | +import com.google.android.gms.common.api.Scope; |
| 17 | +import com.google.android.gms.tasks.Task; |
| 18 | + |
| 19 | +import org.json.JSONArray; |
| 20 | +import org.json.JSONException; |
8 | 21 |
|
9 | | -@NativePlugin() |
| 22 | +@NativePlugin(requestCodes = GoogleAuth.RC_SIGN_IN) |
10 | 23 | public class GoogleAuth extends Plugin { |
| 24 | + static final int RC_SIGN_IN = 1337; |
| 25 | + private GoogleSignInClient googleSignInClient; |
11 | 26 |
|
12 | | - @PluginMethod() |
13 | | - public void echo(PluginCall call) { |
14 | | - String value = call.getString("value"); |
| 27 | + @Override |
| 28 | + public void load() { |
| 29 | + String clientId = this.getContext().getString(R.string.server_client_id); |
| 30 | + GoogleSignInOptions.Builder googleSignInBuilder = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN) |
| 31 | + .requestIdToken(clientId) |
| 32 | + .requestEmail(); |
15 | 33 |
|
16 | | - JSObject ret = new JSObject(); |
17 | | - ret.put("value", value); |
18 | | - call.success(ret); |
| 34 | + try { |
| 35 | + JSONArray scopeArray = (JSONArray) getConfigValue("scopes"); |
| 36 | + Scope[] scopes = new Scope[scopeArray.length() - 1]; |
| 37 | + Scope firstScope = new Scope(scopeArray.getString(0)); |
| 38 | + for (int i = 1; i < scopeArray.length(); i++) { |
| 39 | + scopes[i - 1] = new Scope(scopeArray.getString(i)); |
| 40 | + } |
| 41 | + googleSignInBuilder.requestScopes(firstScope, scopes); |
| 42 | + } catch (JSONException e) { |
| 43 | + e.printStackTrace(); |
19 | 44 | } |
| 45 | + |
| 46 | + GoogleSignInOptions googleSignInOptions = googleSignInBuilder.build(); |
| 47 | + googleSignInClient = GoogleSignIn.getClient(this.getContext(), googleSignInOptions); |
| 48 | + } |
| 49 | + |
| 50 | + @PluginMethod() |
| 51 | + public void signIn(PluginCall call) { |
| 52 | + saveCall(call); |
| 53 | + Intent signInIntent = googleSignInClient.getSignInIntent(); |
| 54 | + startActivityForResult(call, signInIntent, RC_SIGN_IN); |
| 55 | + } |
| 56 | + |
| 57 | + @Override |
| 58 | + protected void handleOnActivityResult(int requestCode, int resultCode, Intent data) { |
| 59 | + super.handleOnActivityResult(requestCode, resultCode, data); |
| 60 | + |
| 61 | + if (requestCode == RC_SIGN_IN) { |
| 62 | + Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data); |
| 63 | + handleSignInResult(task); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + private void handleSignInResult(Task<GoogleSignInAccount> completedTask) { |
| 68 | + PluginCall signInCall = getSavedCall(); |
| 69 | + |
| 70 | + if (signInCall == null) return; |
| 71 | + |
| 72 | + try { |
| 73 | + GoogleSignInAccount account = completedTask.getResult(ApiException.class); |
| 74 | + |
| 75 | + JSObject authentication = new JSObject(); |
| 76 | + authentication.put("idToken", account.getIdToken()); |
| 77 | + |
| 78 | + JSObject user = new JSObject(); |
| 79 | + user.put("serverAuthCode", account.getServerAuthCode()); |
| 80 | + user.put("authentication", authentication); |
| 81 | + |
| 82 | + signInCall.success(user); |
| 83 | + |
| 84 | + } catch (ApiException e) { |
| 85 | + signInCall.error("Something went wrong", e); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + |
| 90 | + @PluginMethod() |
| 91 | + public void signOut(final PluginCall call) { |
| 92 | + googleSignInClient.signOut(); |
| 93 | + call.success(); |
| 94 | + } |
20 | 95 | } |
0 commit comments