Search in sources :

Example 26 with FirebaseUser

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");
            }
        });
    }
}
Also used : Task(com.google.android.gms.tasks.Task) UploadTask(com.google.firebase.storage.UploadTask) User(com.polito.mad17.madmax.entities.User) FirebaseUser(com.google.firebase.auth.FirebaseUser) HashMap(java.util.HashMap) Bitmap(android.graphics.Bitmap) UploadTask(com.google.firebase.storage.UploadTask) NonNull(android.support.annotation.NonNull) StorageReference(com.google.firebase.storage.StorageReference) SharedPreferences(android.content.SharedPreferences) FirebaseUser(com.google.firebase.auth.FirebaseUser) ByteArrayOutputStream(java.io.ByteArrayOutputStream) FirebaseAuthWeakPasswordException(com.google.firebase.auth.FirebaseAuthWeakPasswordException) FirebaseAuthUserCollisionException(com.google.firebase.auth.FirebaseAuthUserCollisionException) IOException(java.io.IOException) OnCompleteListener(com.google.android.gms.tasks.OnCompleteListener) OnFailureListener(com.google.android.gms.tasks.OnFailureListener)

Aggregations

FirebaseUser (com.google.firebase.auth.FirebaseUser)26 AuthResult (com.google.firebase.auth.AuthResult)7 Button (android.widget.Button)6 FakeAuthResult (com.firebase.ui.auth.testhelpers.FakeAuthResult)6 BuildConfig (com.firebase.ui.auth.BuildConfig)5 OnFailureListener (com.google.android.gms.tasks.OnFailureListener)5 Test (org.junit.Test)5 FirebaseAuth (com.google.firebase.auth.FirebaseAuth)4 Config (org.robolectric.annotation.Config)4 NonNull (android.support.annotation.NonNull)3 EditText (android.widget.EditText)3 ActivityHelperShadow (com.firebase.ui.auth.testhelpers.ActivityHelperShadow)3 TaskFailureLogger (com.firebase.ui.auth.ui.TaskFailureLogger)3 Task (com.google.android.gms.tasks.Task)3 FirebaseAuthUserCollisionException (com.google.firebase.auth.FirebaseAuthUserCollisionException)3 Intent (android.content.Intent)2 SharedPreferences (android.content.SharedPreferences)2 Bitmap (android.graphics.Bitmap)2 IdpResponse (com.firebase.ui.auth.IdpResponse)2 FirebaseAuthWeakPasswordException (com.google.firebase.auth.FirebaseAuthWeakPasswordException)2