2929import com .google .firebase .auth .FirebaseUser ;
3030import com .google .firebase .auth .GetTokenResult ;
3131import com .google .firebase .auth .GoogleAuthProvider ;
32+ import com .google .firebase .auth .EmailAuthProvider ;
33+
3234
3335import io .fullstack .firestack .Utils ;
3436
@@ -51,7 +53,7 @@ public FirestackAuth(ReactApplicationContext reactContext) {
5153 mReactContext = reactContext ;
5254 mAuth = FirebaseAuth .getInstance ();
5355
54- Log .d (TAG , "New FirestackAuth instance" );
56+ Log .d (TAG , "New FirestackAuth instance" );
5557 }
5658
5759 @ Override
@@ -166,6 +168,43 @@ public void signInWithProvider(final String provider, final String authToken, fi
166168 Utils .todoNote (TAG , "signInWithProvider" , callback );
167169 }
168170
171+ @ ReactMethod
172+ public void linkPassword (final String email , final String password , final Callback callback ) {
173+ FirebaseUser user = mAuth .getCurrentUser ();
174+
175+ if (user != null ) {
176+ AuthCredential credential = EmailAuthProvider .getCredential (email , password );
177+ user
178+ .linkWithCredential (credential )
179+ .addOnCompleteListener (new OnCompleteListener <AuthResult >() {
180+ @ Override
181+ public void onComplete (@ NonNull Task <AuthResult > task ) {
182+ try {
183+ if (task .isSuccessful ()) {
184+ Log .d (TAG , "user linked with password credential" );
185+ userCallback (mAuth .getCurrentUser (), callback );
186+ } else {
187+ userErrorCallback (task , callback );
188+ }
189+ } catch (Exception ex ) {
190+ userExceptionCallback (ex , callback );
191+ }
192+ }
193+ });
194+ } else {
195+ callbackNoUser (callback , true );
196+ }
197+ }
198+
199+ @ ReactMethod
200+ public void link (final String provider , final String authToken , final String authSecret , final Callback callback ) {
201+ if (provider .equals ("password" )) {
202+ linkPassword (authToken , authSecret , callback );
203+ } else
204+ // TODO other providers
205+ Utils .todoNote (TAG , "linkWithProvider" , callback );
206+ }
207+
169208 @ ReactMethod
170209 public void signInAnonymously (final Callback callback ) {
171210 Log .d (TAG , "signInAnonymously:called:" );
@@ -514,7 +553,7 @@ public void onComplete(@NonNull Task<GetTokenResult> task) {
514553
515554 private void userErrorCallback (Task task , final Callback onFail ) {
516555 WritableMap error = Arguments .createMap ();
517- error .putString ("code" , ((FirebaseAuthException )task .getException ()).getErrorCode ());
556+ error .putString ("code" , ((FirebaseAuthException ) task .getException ()).getErrorCode ());
518557 error .putString ("message" , task .getException ().getMessage ());
519558 onFail .invoke (error );
520559 }
0 commit comments