diff --git a/android/src/main/java/io/fullstack/firestack/FirestackAuth.java b/android/src/main/java/io/fullstack/firestack/FirestackAuth.java index 676b27f..e715e77 100644 --- a/android/src/main/java/io/fullstack/firestack/FirestackAuth.java +++ b/android/src/main/java/io/fullstack/firestack/FirestackAuth.java @@ -103,25 +103,20 @@ public void unlistenForAuth(final Callback callback) { @ReactMethod public void createUserWithEmail(final String email, final String password, final Callback callback) { - mAuth = FirebaseAuth.getInstance(); + mAuth = FirebaseAuth.getInstance(); - mAuth.createUserWithEmailAndPassword(email, password) - .addOnCompleteListener(new OnCompleteListener() { - @Override - public void onComplete(@NonNull Task task) { - if (task.isSuccessful()) { - FirestackAuthModule.this.user = task.getResult().getUser(); - userCallback(FirestackAuthModule.this.user, callback); - } else { - // userErrorCallback(task, callback); - } - } - }).addOnFailureListener(new OnFailureListener() { - @Override - public void onFailure(@NonNull Exception ex) { + mAuth.createUserWithEmailAndPassword(email, password).addOnSuccessListener(new OnSuccessListener() { + @Override + public void onSuccess(AuthResult authResult) { + FirestackAuthModule.this.user = authResult.getUser(); + userCallback(FirestackAuthModule.this.user, callback); + } + }).addOnFailureListener(new OnFailureListener() { + @Override + public void onFailure(@NonNull Exception ex) { userExceptionCallback(ex, callback); - } - }); + } + }); } @ReactMethod @@ -129,25 +124,22 @@ public void signInWithEmail(final String email, final String password, final Cal mAuth = FirebaseAuth.getInstance(); mAuth.signInWithEmailAndPassword(email, password) - .addOnCompleteListener(new OnCompleteListener() { - @Override - public void onComplete(@NonNull Task task) { - if (task.isSuccessful()) { - FirestackAuthModule.this.user = task.getResult().getUser(); - userCallback(FirestackAuthModule.this.user, callback); - } else { - // userErrorCallback(task, callback); - } - } - }).addOnFailureListener(new OnFailureListener() { - @Override - public void onFailure(@NonNull Exception ex) { + .addOnSuccessListener(new OnSuccessListener() { + @Override + public void onSuccess(AuthResult authResult) { + FirestackAuthModule.this.user = authResult.getUser(); + userCallback(FirestackAuthModule.this.user, callback); + } + }).addOnFailureListener(new OnFailureListener() { + @Override + public void onFailure(@NonNull Exception ex) { Log.e(TAG, "An exception occurred: " + ex.getMessage()); userExceptionCallback(ex, callback); - } - }); + } + }); } + @ReactMethod public void signInWithProvider(final String provider, final String authToken, final String authSecret, final Callback callback) { if (provider.equals("facebook")) { @@ -166,48 +158,37 @@ public void signInAnonymously(final Callback callback) { mAuth = FirebaseAuth.getInstance(); mAuth.signInAnonymously() - .addOnCompleteListener(new OnCompleteListener() { + .addOnSuccessListener(new OnSuccessListener() { @Override - public void onComplete(@NonNull Task task) { - Log.d(TAG, "signInAnonymously:onComplete:" + task.isSuccessful()); - - if (task.isSuccessful()) { - FirestackAuthModule.this.user = task.getResult().getUser(); - userCallback(FirestackAuthModule.this.user, callback); - } else { - // userErrorCallback(task, callback); - } - } - }).addOnFailureListener(new OnFailureListener() { - @Override - public void onFailure(@NonNull Exception ex) { - userExceptionCallback(ex, callback); - } - }); + public void onSuccess(AuthResult authResult) { + FirestackAuthModule.this.user = authResult.getUser(); + userCallback(FirestackAuthModule.this.user, callback); + } + }).addOnFailureListener(new OnFailureListener() { + @Override + public void onFailure(@NonNull Exception ex) { + userExceptionCallback(ex, callback); + } + }); } @ReactMethod public void signInWithCustomToken(final String customToken, final Callback callback) { - mAuth = FirebaseAuth.getInstance(); + mAuth = FirebaseAuth.getInstance(); - mAuth.signInWithCustomToken(customToken) - .addOnCompleteListener(new OnCompleteListener() { + mAuth.signInWithCustomToken(customToken) + .addOnSuccessListener(new OnSuccessListener() { + @Override + public void onSuccess(AuthResult authResult) { + FirestackAuthModule.this.user = authResult.getUser(); + userCallback(FirestackAuthModule.this.user, callback); + } + }).addOnFailureListener(new OnFailureListener() { @Override - public void onComplete(@NonNull Task task) { - Log.d(TAG, "signInWithCustomToken:onComplete:" + task.isSuccessful()); - if (task.isSuccessful()) { - FirestackAuthModule.this.user = task.getResult().getUser(); - userCallback(FirestackAuthModule.this.user, callback); - } else { - // userErrorCallback(task, callback); - } - } - }).addOnFailureListener(new OnFailureListener() { - @Override - public void onFailure(@NonNull Exception ex) { + public void onFailure(@NonNull Exception ex) { userExceptionCallback(ex, callback); - } - }); + } + }); } @ReactMethod @@ -231,18 +212,19 @@ public void reauthenticateWithCredentialForProvider(final String provider, final FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser(); if (user != null) { user.reauthenticate(credential) - .addOnCompleteListener(new OnCompleteListener() { - @Override - public void onComplete(@NonNull Task task) { - if (task.isSuccessful()) { + .addOnSuccessListener(new OnSuccessListener() { + @Override + public void onSuccess(Void aVoid) { Log.d(TAG, "User re-authenticated with " + provider); FirebaseUser u = FirebaseAuth.getInstance().getCurrentUser(); userCallback(u, callback); - } else { - // userErrorCallback(task, callback); } - } - }); + }).addOnFailureListener(new OnFailureListener() { + @Override + public void onFailure(@NonNull Exception e) { + userExceptionCallback(e, callback); + } + }); } else { WritableMap err = Arguments.createMap(); err.putInt("errorCode", NO_CURRENT_USER); @@ -253,65 +235,56 @@ public void onComplete(@NonNull Task task) { @ReactMethod public void updateUserEmail(final String email, final Callback callback) { - FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser(); + FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser(); - if (user != null) { - user.updateEmail(email) - .addOnCompleteListener(new OnCompleteListener() { - @Override - public void onComplete(@NonNull Task task) { - if (task.isSuccessful()) { - Log.d(TAG, "User email address updated"); - FirebaseUser u = FirebaseAuth.getInstance().getCurrentUser(); - userCallback(u, callback); - } else { - // userErrorCallback(task, callback); - } - } - }).addOnFailureListener(new OnFailureListener() { - @Override - public void onFailure(@NonNull Exception ex) { - userExceptionCallback(ex, callback); - } + if (user != null) { + user.updateEmail(email) + .addOnSuccessListener(new OnSuccessListener() { + @Override + public void onSuccess(Void aVoid) { + Log.d(TAG, "User password updated"); + FirebaseUser u = FirebaseAuth.getInstance().getCurrentUser(); + userCallback(u, callback); + } + }).addOnFailureListener(new OnFailureListener() { + @Override + public void onFailure(@NonNull Exception ex) { + userExceptionCallback(ex, callback); + } }); - } else { - WritableMap err = Arguments.createMap(); - err.putInt("errorCode", NO_CURRENT_USER); - err.putString("errorMessage", "No current user"); - callback.invoke(err); - } + } else { + WritableMap err = Arguments.createMap(); + err.putInt("errorCode", NO_CURRENT_USER); + err.putString("errorMessage", "No current user"); + callback.invoke(err); + } } @ReactMethod public void updateUserPassword(final String newPassword, final Callback callback) { - FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser(); - - if (user != null) { - user.updatePassword(newPassword) - .addOnCompleteListener(new OnCompleteListener() { - @Override - public void onComplete(@NonNull Task task) { - if (task.isSuccessful()) { - Log.d(TAG, "User password updated"); + FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser(); - FirebaseUser u = FirebaseAuth.getInstance().getCurrentUser(); - userCallback(u, callback); - } else { - // userErrorCallback(task, callback); - } - } - }).addOnFailureListener(new OnFailureListener() { - @Override - public void onFailure(@NonNull Exception ex) { - userExceptionCallback(ex, callback); - } + if (user != null) { + user.updatePassword(newPassword) + .addOnSuccessListener(new OnSuccessListener() { + @Override + public void onSuccess(Void aVoid) { + Log.d(TAG, "User password updated"); + FirebaseUser u = FirebaseAuth.getInstance().getCurrentUser(); + userCallback(u, callback); + } + }).addOnFailureListener(new OnFailureListener() { + @Override + public void onFailure(@NonNull Exception ex) { + userExceptionCallback(ex, callback); + } }); - } else { - WritableMap err = Arguments.createMap(); - err.putInt("errorCode", NO_CURRENT_USER); - err.putString("errorMessage", "No current user"); - callback.invoke(err); - } + } else { + WritableMap err = Arguments.createMap(); + err.putInt("errorCode", NO_CURRENT_USER); + err.putString("errorMessage", "No current user"); + callback.invoke(err); + } } @ReactMethod @@ -319,126 +292,107 @@ public void sendPasswordResetWithEmail(final String email, final Callback callba mAuth = FirebaseAuth.getInstance(); mAuth.sendPasswordResetEmail(email) - .addOnCompleteListener(new OnCompleteListener() { + .addOnSuccessListener(new OnSuccessListener() { @Override - public void onComplete(@NonNull Task task) { - if(task.isSuccessful()){ - WritableMap resp = Arguments.createMap(); - resp.putString("status", "complete"); - callback.invoke(null, resp); - } else { - callback.invoke(task.getException().toString()); - } + public void onSuccess(Void aVoid) { + WritableMap resp = Arguments.createMap(); + resp.putString("status", "complete"); + callback.invoke(null, resp); } - }).addOnFailureListener(new OnFailureListener() { - @Override - public void onFailure(@NonNull Exception ex) { + }).addOnFailureListener(new OnFailureListener() { + @Override + public void onFailure(@NonNull Exception ex) { userExceptionCallback(ex, callback); - } - }); + } + }); } @ReactMethod public void deleteUser(final Callback callback) { - FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser(); + FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser(); - if (user != null) { - user.delete() - .addOnCompleteListener(new OnCompleteListener() { - @Override - public void onComplete(@NonNull Task task) { - if (task.isSuccessful()) { - Log.d(TAG, "User account deleted"); - WritableMap resp = Arguments.createMap(); - resp.putString("status", "complete"); - resp.putString("msg", "User account deleted"); - callback.invoke(null, resp); - } else { - // userErrorCallback(task, callback); - } - } - }).addOnFailureListener(new OnFailureListener() { - @Override - public void onFailure(@NonNull Exception ex) { - userExceptionCallback(ex, callback); - } + if (user != null) { + user.delete() + .addOnSuccessListener(new OnSuccessListener() { + @Override + public void onSuccess(Void aVoid) { + Log.d(TAG, "User account deleted"); + WritableMap resp = Arguments.createMap(); + resp.putString("status", "complete"); + resp.putString("msg", "User account deleted"); + callback.invoke(null, resp); + } + }).addOnFailureListener(new OnFailureListener() { + @Override + public void onFailure(@NonNull Exception ex) { + userExceptionCallback(ex, callback); + } }); - } else { - WritableMap err = Arguments.createMap(); - err.putInt("errorCode", NO_CURRENT_USER); - err.putString("errorMessage", "No current user"); - callback.invoke(err); - } + } else { + WritableMap err = Arguments.createMap(); + err.putInt("errorCode", NO_CURRENT_USER); + err.putString("errorMessage", "No current user"); + callback.invoke(err); + } } @ReactMethod public void getToken(final Callback callback) { - FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser(); - - user.getToken(true) - .addOnCompleteListener(new OnCompleteListener() { - @Override - public void onComplete(@NonNull Task task) { - if (task.isSuccessful()) { - String token = task.getResult().getToken(); - WritableMap resp = Arguments.createMap(); - resp.putString("status", "complete"); - resp.putString("token", token); - callback.invoke(null, resp); - } else { - WritableMap err = Arguments.createMap(); - err.putInt("errorCode", ERROR_FETCHING_TOKEN); - err.putString("errorMessage", task.getException().getMessage()); - callback.invoke(err); - } - } - }).addOnFailureListener(new OnFailureListener() { - @Override - public void onFailure(@NonNull Exception ex) { + FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser(); + + user.getToken(true) + .addOnSuccessListener(new OnSuccessListener() { + @Override + public void onSuccess(GetTokenResult getTokenResult) { + String token = getTokenResult.getToken(); + WritableMap resp = Arguments.createMap(); + resp.putString("status", "complete"); + resp.putString("token", token); + callback.invoke(null, resp); + + } + }).addOnFailureListener(new OnFailureListener() { + @Override + public void onFailure(@NonNull Exception ex) { userExceptionCallback(ex, callback); - } - }); + } + }); } @ReactMethod public void updateUserProfile(ReadableMap props, final Callback callback) { - FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser(); + FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser(); - UserProfileChangeRequest.Builder profileBuilder = new UserProfileChangeRequest.Builder(); + UserProfileChangeRequest.Builder profileBuilder = new UserProfileChangeRequest.Builder(); - Map m = FirestackUtils.recursivelyDeconstructReadableMap(props); + Map m = FirestackUtils.recursivelyDeconstructReadableMap(props); - if (m.containsKey("displayName")) { - String displayName = (String) m.get("displayName"); - profileBuilder.setDisplayName(displayName); - } + if (m.containsKey("displayName")) { + String displayName = (String) m.get("displayName"); + profileBuilder.setDisplayName(displayName); + } - if (m.containsKey("photoUri")) { - String photoUriStr = (String) m.get("photoUri"); - Uri uri = Uri.parse(photoUriStr); - profileBuilder.setPhotoUri(uri); - } + if (m.containsKey("photoUri")) { + String photoUriStr = (String) m.get("photoUri"); + Uri uri = Uri.parse(photoUriStr); + profileBuilder.setPhotoUri(uri); + } - UserProfileChangeRequest profileUpdates = profileBuilder.build(); - - user.updateProfile(profileUpdates) - .addOnCompleteListener(new OnCompleteListener() { - @Override - public void onComplete(@NonNull Task task) { - if (task.isSuccessful()) { - Log.d(TAG, "User profile updated"); - FirebaseUser u = FirebaseAuth.getInstance().getCurrentUser(); - userCallback(u, callback); - } else { - // userErrorCallback(task, callback); + UserProfileChangeRequest profileUpdates = profileBuilder.build(); + + user.updateProfile(profileUpdates).addOnSuccessListener(new OnSuccessListener() { + @Override + public void onSuccess(Void aVoid) { + Log.d(TAG, "User profile updated"); + FirebaseUser u = FirebaseAuth.getInstance().getCurrentUser(); + userCallback(u, callback); } - } }).addOnFailureListener(new OnFailureListener() { - @Override - public void onFailure(@NonNull Exception ex) { + @Override + public void onFailure(@NonNull Exception ex) { userExceptionCallback(ex, callback); - } - }); + } + }); } @ReactMethod