diff --git a/sjfirebase/src/main/java/com/simplejnius/sjfirebase/SJFirebaseAuthEmail.java b/sjfirebase/src/main/java/com/simplejnius/sjfirebase/SJFirebaseAuthEmail.java index e9f912d..2db294d 100644 --- a/sjfirebase/src/main/java/com/simplejnius/sjfirebase/SJFirebaseAuthEmail.java +++ b/sjfirebase/src/main/java/com/simplejnius/sjfirebase/SJFirebaseAuthEmail.java @@ -1,11 +1,9 @@ package com.simplejnius.sjfirebase; -import com.google.firebase.auth.FirebaseAuth; -import com.google.firebase.auth.FirebaseUser; import com.google.android.gms.tasks.OnCompleteListener; import com.google.firebase.auth.AuthResult; - -import java.util.concurrent.Executor; +import com.google.firebase.auth.FirebaseAuth; +import com.google.firebase.auth.FirebaseUser; public class SJFirebaseAuthEmail { private static final String TAG = "EmailPassword"; @@ -33,11 +31,11 @@ public boolean check_user_signed_in() { public void create_user_with_email_and_password( String email, String password, Object callback) { m_auth.createUserWithEmailAndPassword(email, password) - .addOnCompleteListener((Executor) this, (OnCompleteListener) callback); + .addOnCompleteListener((OnCompleteListener) callback); } public void sign_in_with_email_and_password(String email, String password, Object callback) { m_auth.signInWithEmailAndPassword(email, password) - .addOnCompleteListener((Executor) this, (OnCompleteListener) callback); + .addOnCompleteListener((OnCompleteListener) callback); } } diff --git a/sjfirebase/src/main/java/com/simplejnius/sjfirebase/SJFirebaseUser.java b/sjfirebase/src/main/java/com/simplejnius/sjfirebase/SJFirebaseUser.java index 831b254..1f515ec 100644 --- a/sjfirebase/src/main/java/com/simplejnius/sjfirebase/SJFirebaseUser.java +++ b/sjfirebase/src/main/java/com/simplejnius/sjfirebase/SJFirebaseUser.java @@ -1,15 +1,99 @@ package com.simplejnius.sjfirebase; +import android.net.Uri; + +import com.google.android.gms.tasks.OnCompleteListener; +import com.google.firebase.auth.ActionCodeSettings; +import com.google.firebase.auth.AuthCredential; +import com.google.firebase.auth.EmailAuthProvider; import com.google.firebase.auth.FirebaseAuth; +import com.google.firebase.auth.FirebaseAuthRecentLoginRequiredException; import com.google.firebase.auth.FirebaseUser; +import com.google.firebase.auth.UserProfileChangeRequest; import java.util.Objects; public class SJFirebaseUser { - private FirebaseUser current_user; public FirebaseUser get_current_user() { - current_user = FirebaseAuth.getInstance().getCurrentUser(); - return current_user; + return FirebaseAuth.getInstance().getCurrentUser(); + } + + public void update_profile(String name, String photo_url, Object callback) { + // [START update_profile] + UserProfileChangeRequest.Builder profile_updates = new UserProfileChangeRequest.Builder(); + UserProfileChangeRequest user_profile_change_request; + + if (photo_url == null) { + user_profile_change_request = profile_updates.setDisplayName(name).build(); + } else if (name == null) { + user_profile_change_request = profile_updates.setPhotoUri(Uri.parse(photo_url)).build(); + } else { + user_profile_change_request = profile_updates.setDisplayName(name) + .setPhotoUri(Uri.parse(photo_url)) + .build(); + } + + get_current_user().updateProfile(user_profile_change_request) + .addOnCompleteListener((OnCompleteListener) callback); + // [END update_profile] + } + + public void update_email(String email, Object callback) { + get_current_user().updateEmail(email) + .addOnCompleteListener((OnCompleteListener) callback); + } + + public void send_email_verification(Object callback) { + // [START send_email_verification] + get_current_user().sendEmailVerification() + .addOnCompleteListener((OnCompleteListener) callback); + // [END send_email_verification] + } + + public void send_email_verification_with_continue_url( + String app_package_name, String url, Object callback) { + // [START send_email_verification_with_continue_url] + ActionCodeSettings actionCodeSettings = ActionCodeSettings.newBuilder() + .setUrl(url) + .setIOSBundleId(app_package_name) + // The default for this is populated with the current android package name. + .setAndroidPackageName(app_package_name, false, null) + .build(); + + get_current_user().sendEmailVerification(actionCodeSettings) + .addOnCompleteListener((OnCompleteListener) callback); + } + + public void send_password_reset_email(String email, Object callback) { + // [START send_password_reset] + FirebaseAuth auth = FirebaseAuth.getInstance(); + + auth.sendPasswordResetEmail(email) + .addOnCompleteListener((OnCompleteListener) callback); + // [END send_password_reset] + } + + public void deleteUser(Object callback) { + // [START delete_user] + + get_current_user().delete() + .addOnCompleteListener((OnCompleteListener) callback); + // [END delete_user] + } + + public void reauthenticate(String email, String password, Object callback) { + // [START reauthenticate] + + // Get auth credentials from the user for re-authentication. The example below shows + // email and password credentials but there are multiple possible providers, + // such as GoogleAuthProvider or FacebookAuthProvider. + AuthCredential credential = EmailAuthProvider + .getCredential(email, password); + + // Prompt the user to re-provide their sign-in credentials + get_current_user().reauthenticate(credential) + .addOnCompleteListener((OnCompleteListener) callback); + // [END reauthenticate] } }