use of com.google.firebase.auth.FirebaseUser in project MadMax by deviz92.
the class SignUpFragment method sendVerificationEmail.
private void sendVerificationEmail() {
Log.i(TAG, "sendVerificationEmail");
final FirebaseUser user = auth.getCurrentUser();
if (user == null) {
Log.e(TAG, "Error while retriving current user from db");
Toast.makeText(getContext(), "Error while retriving current user from db", Toast.LENGTH_LONG).show();
return;
}
SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this.getContext());
String defaultCurrency = sharedPref.getString(SettingsFragment.DEFAULT_CURRENCY, "");
String UID = user.getUid();
final User u = new User(UID, usernameView.getText().toString(), nameView.getText().toString(), surnameView.getText().toString(), emailView.getText().toString(), passwordView.getText().toString(), "", defaultCurrency);
progressDialog.setMessage("Sending email verification, please wait...");
progressDialog.show();
// for saving image
if (imageSetted) {
StorageReference uProfileImageFilenameRef = storageReference.child("users").child(UID).child(UID + "_profileImage.jpg");
// Get the data from an ImageView as bytes
profileImageView.setDrawingCacheEnabled(true);
profileImageView.buildDrawingCache();
Bitmap bitmap = profileImageView.getDrawingCache();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] data = baos.toByteArray();
UploadTask uploadTask = uProfileImageFilenameRef.putBytes(data);
uploadTask.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception exception) {
// todo Handle unsuccessful uploads
Log.e(TAG, "image upload failed");
}
}).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
// taskSnapshot.getMetadata() contains file metadata such as size, content-type, and download URL.
u.setProfileImage(taskSnapshot.getMetadata().getDownloadUrl().toString());
user.sendEmailVerification().addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
progressDialog.dismiss();
if (task.isSuccessful()) {
Log.i(TAG, "verification email successful sent");
Log.i(TAG, "insert new user into db");
HashMap<String, String> newUserEntry = new HashMap<>();
newUserEntry.put("email", u.getEmail());
newUserEntry.put("password", u.getPassword());
// newUserEntry.put("friends", u.getUserFriends().toString());
// newUserEntry.put("groups", u.getUserGroups().toString());
newUserEntry.put("image", u.getProfileImage());
newUserEntry.put("name", u.getName());
newUserEntry.put("surname", u.getSurname());
newUserEntry.put("username", u.getUsername());
databaseReference.child("users").child(u.getID()).setValue(newUserEntry);
Toast.makeText(getContext(), R.string.emailVerification_text, Toast.LENGTH_LONG).show();
} else {
// todo delete the account and restart the activity
Log.e(TAG, "verification email not sent, exception: " + task.getException());
}
onClickSignUpInterface.itemClicked(SignUpFragment.class.getSimpleName(), "1");
}
});
}
});
} else {
user.sendEmailVerification().addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
progressDialog.dismiss();
if (task.isSuccessful()) {
Log.i(TAG, "verification email successful sent");
Log.i(TAG, "insert new user into db");
HashMap<String, String> newUserEntry = new HashMap<>();
newUserEntry.put("email", u.getEmail());
newUserEntry.put("password", u.getPassword());
// newUserEntry.put("friends", u.getUserFriends().toString());
// newUserEntry.put("groups", u.getUserGroups().toString());
newUserEntry.put("image", u.getProfileImage());
newUserEntry.put("name", u.getName());
newUserEntry.put("surname", u.getSurname());
newUserEntry.put("username", u.getUsername());
databaseReference.child("users").child(u.getID()).setValue(newUserEntry);
Toast.makeText(getContext(), R.string.emailVerification_text, Toast.LENGTH_LONG).show();
} else {
// todo delete the account and restart the activity
Log.e(TAG, "verification email not sent, exception: " + task.getException());
}
onClickSignUpInterface.itemClicked(SignUpFragment.class.getSimpleName(), "1");
}
});
}
}
Aggregations