30
30
import com .facebook .react .bridge .WritableArray ;
31
31
import com .facebook .react .bridge .WritableMap ;
32
32
import com .google .android .gms .tasks .OnCompleteListener ;
33
+ import com .google .android .gms .tasks .OnFailureListener ;
34
+ import com .google .android .gms .tasks .OnSuccessListener ;
35
+ import com .google .android .gms .tasks .Task ;
33
36
import com .google .firebase .FirebaseApp ;
34
37
import com .google .firebase .FirebaseException ;
35
38
import com .google .firebase .FirebaseNetworkException ;
56
59
import com .google .firebase .auth .MultiFactorResolver ;
57
60
import com .google .firebase .auth .MultiFactorSession ;
58
61
import com .google .firebase .auth .OAuthProvider ;
62
+ import com .google .firebase .auth .OAuthCredential ;
59
63
import com .google .firebase .auth .PhoneAuthCredential ;
60
64
import com .google .firebase .auth .PhoneAuthOptions ;
61
65
import com .google .firebase .auth .PhoneAuthProvider ;
@@ -203,7 +207,6 @@ public void addIdTokenListener(final String appName) {
203
207
204
208
FirebaseApp firebaseApp = FirebaseApp .getInstance (appName );
205
209
FirebaseAuth firebaseAuth = FirebaseAuth .getInstance (firebaseApp );
206
-
207
210
if (!mIdTokenListeners .containsKey (appName )) {
208
211
FirebaseAuth .IdTokenListener newIdTokenListener =
209
212
firebaseAuth1 -> {
@@ -839,6 +842,87 @@ private void signInWithCredential(
839
842
}
840
843
}
841
844
845
+ @ ReactMethod
846
+ public void signInWithProvider (String appName , String providerId , @ Nullable String email , Promise promise ){
847
+ OAuthProvider .Builder provider = OAuthProvider .newBuilder (providerId );
848
+ if (email != null ){
849
+ provider .addCustomParameter ("login_hint" , email );
850
+ }
851
+ Activity activity = getCurrentActivity ();
852
+ FirebaseApp firebaseApp = FirebaseApp .getInstance (appName );
853
+ FirebaseAuth firebaseAuth = FirebaseAuth .getInstance (firebaseApp );
854
+
855
+ OnSuccessListener onSuccess = new OnSuccessListener <AuthResult >(){
856
+ @ Override
857
+ public void onSuccess (AuthResult authResult ) {
858
+ Log .d (TAG , "signInWithProvider:onComplete:success" );
859
+ promiseWithAuthResult (authResult , promise );
860
+ }
861
+ };
862
+
863
+ OnFailureListener onFailure = new OnFailureListener (){
864
+ @ Override
865
+ public void onFailure (@ NonNull Exception e ) {
866
+ Log .w (TAG , "signInWithProvider:onComplete:failure" , e );
867
+ promiseRejectAuthException (promise , e );
868
+ }
869
+ };
870
+
871
+
872
+ Task <AuthResult > pendingResultTask = firebaseAuth .getPendingAuthResult ();
873
+ if (pendingResultTask != null ){
874
+ pendingResultTask
875
+ .addOnSuccessListener (onSuccess )
876
+ .addOnFailureListener (onFailure );
877
+ } else {
878
+ firebaseAuth
879
+ .startActivityForSignInWithProvider (activity , provider .build ())
880
+ .addOnSuccessListener (onSuccess )
881
+ .addOnFailureListener (onFailure );
882
+ }
883
+ }
884
+
885
+ @ ReactMethod
886
+ public void linkWithProvider (String appName , String providerId , @ Nullable String email , Promise promise ){
887
+ OAuthProvider .Builder provider = OAuthProvider .newBuilder (providerId );
888
+ if (email != null ){
889
+ provider .addCustomParameter ("login_hint" , email );
890
+ }
891
+ Activity activity = getCurrentActivity ();
892
+ FirebaseApp firebaseApp = FirebaseApp .getInstance (appName );
893
+ FirebaseAuth firebaseAuth = FirebaseAuth .getInstance (firebaseApp );
894
+ FirebaseUser firebaseUser = firebaseAuth .getCurrentUser ();
895
+
896
+ OnSuccessListener onSuccess = new OnSuccessListener <AuthResult >(){
897
+ @ Override
898
+ public void onSuccess (AuthResult authResult ) {
899
+ Log .d (TAG , "linkWithProvider:onComplete:success" );
900
+ promiseWithAuthResult (authResult , promise );
901
+ }
902
+ };
903
+
904
+ OnFailureListener onFailure = new OnFailureListener (){
905
+ @ Override
906
+ public void onFailure (@ NonNull Exception e ) {
907
+ Log .w (TAG , "linkInWithProvider:onComplete:failure" , e );
908
+ promiseRejectAuthException (promise , e );
909
+ }
910
+ };
911
+
912
+
913
+ Task <AuthResult > pendingResultTask = firebaseAuth .getPendingAuthResult ();
914
+ if (pendingResultTask != null ){
915
+ pendingResultTask
916
+ .addOnSuccessListener (onSuccess )
917
+ .addOnFailureListener (onFailure );
918
+ } else {
919
+ firebaseUser
920
+ .startActivityForLinkWithProvider (activity , provider .build ())
921
+ .addOnSuccessListener (onSuccess )
922
+ .addOnFailureListener (onFailure );
923
+ }
924
+ }
925
+
842
926
/**
843
927
* signInWithPhoneNumber
844
928
*
@@ -1866,6 +1950,30 @@ private void promiseWithAuthResult(AuthResult authResult, Promise promise) {
1866
1950
WritableMap authResultMap = Arguments .createMap ();
1867
1951
WritableMap userMap = firebaseUserToMap (authResult .getUser ());
1868
1952
1953
+ if (authResult .getCredential () != null ){
1954
+ if (authResult .getCredential () instanceof OAuthCredential ){
1955
+ OAuthCredential creds = (OAuthCredential ) authResult .getCredential ();
1956
+ WritableMap credentialMap = Arguments .createMap ();
1957
+
1958
+ credentialMap .putString ("providerId" , creds .getProvider ());
1959
+ credentialMap .putString ("signInMethod" , creds .getSignInMethod ());
1960
+
1961
+ if (creds .getIdToken () != null ){
1962
+ credentialMap .putString ("idToken" , creds .getIdToken ());
1963
+ }
1964
+
1965
+ if (creds .getAccessToken () != null ){
1966
+ credentialMap .putString ("accessToken" , creds .getAccessToken ());
1967
+ }
1968
+
1969
+ if (creds .getSecret () != null ){
1970
+ credentialMap .putString ("secret" , creds .getSecret ());
1971
+ }
1972
+
1973
+ authResultMap .putMap ("credential" , credentialMap );
1974
+ }
1975
+ }
1976
+
1869
1977
if (authResult .getAdditionalUserInfo () != null ) {
1870
1978
WritableMap additionalUserInfoMap = Arguments .createMap ();
1871
1979
@@ -1906,6 +2014,7 @@ private void promiseWithAuthResult(AuthResult authResult, Promise promise) {
1906
2014
*/
1907
2015
private void promiseRejectAuthException (Promise promise , Exception exception ) {
1908
2016
WritableMap error = getJSError (exception );
2017
+
1909
2018
final String sessionId = error .getString ("sessionId" );
1910
2019
final MultiFactorResolver multiFactorResolver = mCachedResolvers .get (sessionId );
1911
2020
WritableMap resolverAsMap = Arguments .createMap ();
@@ -1927,6 +2036,8 @@ private WritableMap getJSError(Exception exception) {
1927
2036
String message = exception .getMessage ();
1928
2037
String invalidEmail = "The email address is badly formatted." ;
1929
2038
2039
+ System .out .print (exception );
2040
+
1930
2041
try {
1931
2042
FirebaseAuthException authException = (FirebaseAuthException ) exception ;
1932
2043
code = authException .getErrorCode ();
0 commit comments