Skip to content

Commit

Permalink
Add Firebase User
Browse files Browse the repository at this point in the history
  • Loading branch information
kengoon committed Dec 8, 2023
1 parent dc127e3 commit bc31c5b
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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<AuthResult>) callback);
.addOnCompleteListener((OnCompleteListener<AuthResult>) callback);
}

public void sign_in_with_email_and_password(String email, String password, Object callback) {
m_auth.signInWithEmailAndPassword(email, password)
.addOnCompleteListener((Executor) this, (OnCompleteListener<AuthResult>) callback);
.addOnCompleteListener((OnCompleteListener<AuthResult>) callback);
}
}
Original file line number Diff line number Diff line change
@@ -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<Void>) callback);
// [END update_profile]
}

public void update_email(String email, Object callback) {
get_current_user().updateEmail(email)
.addOnCompleteListener((OnCompleteListener<Void>) callback);
}

public void send_email_verification(Object callback) {
// [START send_email_verification]
get_current_user().sendEmailVerification()
.addOnCompleteListener((OnCompleteListener<Void>) 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<Void>) callback);
}

public void send_password_reset_email(String email, Object callback) {
// [START send_password_reset]
FirebaseAuth auth = FirebaseAuth.getInstance();

auth.sendPasswordResetEmail(email)
.addOnCompleteListener((OnCompleteListener<Void>) callback);
// [END send_password_reset]
}

public void deleteUser(Object callback) {
// [START delete_user]

get_current_user().delete()
.addOnCompleteListener((OnCompleteListener<Void>) 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<Void>) callback);
// [END reauthenticate]
}
}

0 comments on commit bc31c5b

Please sign in to comment.