11package com .codetrixstudio .capacitor .GoogleAuth ;
22
33import android .content .Intent ;
4+ import androidx .activity .result .ActivityResult ;
45
56import com .codetrixstudio .capacitor .GoogleAuth .capacitorgoogleauth .R ;
67import com .getcapacitor .JSObject ;
7- import com .getcapacitor .NativePlugin ;
88import com .getcapacitor .Plugin ;
99import com .getcapacitor .PluginCall ;
1010import com .getcapacitor .PluginMethod ;
11+ import com .getcapacitor .annotation .ActivityCallback ;
12+ import com .getcapacitor .annotation .CapacitorPlugin ;
1113import com .google .android .gms .auth .api .signin .GoogleSignIn ;
1214import com .google .android .gms .auth .api .signin .GoogleSignInAccount ;
1315import com .google .android .gms .auth .api .signin .GoogleSignInClient ;
1921import org .json .JSONArray ;
2022import org .json .JSONException ;
2123
22- @ NativePlugin ( requestCodes = GoogleAuth . RC_SIGN_IN )
24+ @ CapacitorPlugin ( )
2325public class GoogleAuth extends Plugin {
24- static final int RC_SIGN_IN = 1337 ;
2526 private GoogleSignInClient googleSignInClient ;
2627
2728 @ Override
@@ -62,23 +63,14 @@ public void load() {
6263 public void signIn (PluginCall call ) {
6364 saveCall (call );
6465 Intent signInIntent = googleSignInClient .getSignInIntent ();
65- startActivityForResult (call , signInIntent , RC_SIGN_IN );
66+ startActivityForResult (call , signInIntent , "signInResult" );
6667 }
6768
68- @ Override
69- protected void handleOnActivityResult (int requestCode , int resultCode , Intent data ) {
70- super .handleOnActivityResult (requestCode , resultCode , data );
71-
72- if (requestCode == RC_SIGN_IN ) {
73- Task <GoogleSignInAccount > task = GoogleSignIn .getSignedInAccountFromIntent (data );
74- handleSignInResult (task );
75- }
76- }
69+ @ ActivityCallback
70+ protected void signInResult (PluginCall call , ActivityResult result ) {
71+ if (call == null ) return ;
7772
78- private void handleSignInResult (Task <GoogleSignInAccount > completedTask ) {
79- PluginCall signInCall = getSavedCall ();
80-
81- if (signInCall == null ) return ;
73+ Task <GoogleSignInAccount > completedTask = GoogleSignIn .getSignedInAccountFromIntent (result .getData ());
8274
8375 try {
8476 GoogleSignInAccount account = completedTask .getResult (ApiException .class );
@@ -90,29 +82,28 @@ private void handleSignInResult(Task<GoogleSignInAccount> completedTask) {
9082 user .put ("serverAuthCode" , account .getServerAuthCode ());
9183 user .put ("idToken" , account .getIdToken ());
9284 user .put ("authentication" , authentication );
93-
85+
9486 user .put ("displayName" , account .getDisplayName ());
9587 user .put ("email" , account .getEmail ());
9688 user .put ("familyName" , account .getFamilyName ());
9789 user .put ("givenName" , account .getGivenName ());
9890 user .put ("id" , account .getId ());
9991 user .put ("imageUrl" , account .getPhotoUrl ());
10092
101- signInCall .success (user );
102-
93+ call .resolve (user );
10394 } catch (ApiException e ) {
104- signInCall . error ("Something went wrong" , e );
95+ call . reject ("Something went wrong" , e );
10596 }
10697 }
10798
10899 @ PluginMethod ()
109100 public void refresh (final PluginCall call ) {
110- call .error ("I don't know how to refresh token on Android" );
101+ call .reject ("I don't know how to refresh token on Android" );
111102 }
112103
113104 @ PluginMethod ()
114105 public void signOut (final PluginCall call ) {
115106 googleSignInClient .signOut ();
116- call .success ();
107+ call .resolve ();
117108 }
118109}
0 commit comments