Skip to content

Commit 02d59c7

Browse files
committed
Android Support #4
1 parent 1812c73 commit 02d59c7

File tree

4 files changed

+99
-15
lines changed

4 files changed

+99
-15
lines changed

README.md

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,21 @@ Make sure you have `GoogleService-Info.plist` with `CLIENT_ID`
4141

4242
Add `REVERSED_CLIENT_ID` as url scheme to `Info.plist`
4343

44+
### Android
45+
Inside your `strings.xml`
46+
```xml
47+
<resources>
48+
<string name="server_client_id">Your Web Client Key</string>
49+
</resources>
50+
```
51+
52+
Register plugin inside your `MainActivity.onCreate`
53+
```java
54+
this.init(savedInstanceState, new ArrayList<Class<? extends Plugin>>() {{
55+
add(GoogleAuth.class);
56+
}});
57+
```
58+
4459
### Configure
4560
Provide configuration in root `capacitor.config.json`
4661
```json
@@ -54,10 +69,3 @@ Provide configuration in root `capacitor.config.json`
5469
}
5570

5671
```
57-
58-
### Support
59-
✔️ iOS
60-
61-
✔️ Web
62-
63-
🔜 Android

android/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,5 +43,6 @@ dependencies {
4343
testImplementation 'junit:junit:4.12'
4444
androidTestImplementation 'com.android.support.test:runner:1.0.2'
4545
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
46+
implementation 'com.google.android.gms:play-services-auth:16.+'
4647
}
4748

Lines changed: 82 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,95 @@
11
package com.codetrixstudio.capacitor.GoogleAuth;
22

3+
import android.content.Intent;
4+
5+
import com.codetrixstudio.capacitor.GoogleAuth.capacitorgoogleauth.R;
36
import com.getcapacitor.JSObject;
47
import com.getcapacitor.NativePlugin;
58
import com.getcapacitor.Plugin;
69
import com.getcapacitor.PluginCall;
710
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;
821

9-
@NativePlugin()
22+
@NativePlugin(requestCodes = GoogleAuth.RC_SIGN_IN)
1023
public class GoogleAuth extends Plugin {
24+
static final int RC_SIGN_IN = 1337;
25+
private GoogleSignInClient googleSignInClient;
1126

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();
1533

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();
1944
}
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+
}
2095
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
<resources>
2-
<string name="my_string">Just a simple string</string>
2+
<string name="server_client_id">Your Web Client Key</string>
33
</resources>

0 commit comments

Comments
 (0)