Search in sources :

Example 31 with FirebaseUser

use of com.google.firebase.auth.FirebaseUser in project BloodHub by kazijehangir.

the class OrganizationRegistrationActivity method registerNewOrg.

private void registerNewOrg() {
    AutoCompleteTextView mEmailView = (AutoCompleteTextView) findViewById(R.id.email);
    EditText mPasswordView = (EditText) findViewById(R.id.password);
    CheckBox mTermsAgree = (CheckBox) findViewById(R.id.agreeTerms);
    final ProgressBar progressBar = (ProgressBar) findViewById(R.id.progressBar);
    String email = mEmailView.getText().toString();
    String password = mPasswordView.getText().toString();
    if (!email.contains("@")) {
        Toast.makeText(this, "Not a valid email address", Toast.LENGTH_SHORT).show();
    } else {
        if (password.length() < 4) {
            Toast.makeText(this, "Password is too short, minimum length is 4.", Toast.LENGTH_SHORT).show();
        } else {
            if (email.contains(":") || password.contains(":")) {
                Toast.makeText(this, "Email address and Password cannot contain ':'", Toast.LENGTH_SHORT).show();
            } else {
                if (!mTermsAgree.isChecked()) {
                    Toast.makeText(this, "You need to agree to share your details.", Toast.LENGTH_SHORT).show();
                } else {
                    progressBar.setVisibility(View.VISIBLE);
                    mAuth.createUserWithEmailAndPassword(email, password).addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {

                        @Override
                        public void onComplete(@NonNull Task<AuthResult> task) {
                            Context context = getApplicationContext();
                            progressBar.setVisibility(View.GONE);
                            if (task.isSuccessful()) {
                                FirebaseUser user = mAuth.getCurrentUser();
                                sendVerificationEmail();
                                // take user to main screen
                                progressBar.setVisibility(View.GONE);
                                Intent intent = new Intent(context, LoginActivity.class);
                                startActivity(intent);
                            } else {
                                if (task.getException() instanceof FirebaseAuthUserCollisionException) {
                                    Toast.makeText(context, "User with this email already exists.", Toast.LENGTH_SHORT).show();
                                } else {
                                    Toast.makeText(context, "Error creating user", Toast.LENGTH_SHORT).show();
                                }
                            }
                        }
                    });
                }
            }
        }
    }
}
Also used : EditText(android.widget.EditText) Context(android.content.Context) AuthResult(com.google.firebase.auth.AuthResult) Intent(android.content.Intent) FirebaseUser(com.google.firebase.auth.FirebaseUser) CheckBox(android.widget.CheckBox) FirebaseAuthUserCollisionException(com.google.firebase.auth.FirebaseAuthUserCollisionException) ProgressBar(android.widget.ProgressBar) AutoCompleteTextView(android.widget.AutoCompleteTextView)

Example 32 with FirebaseUser

use of com.google.firebase.auth.FirebaseUser in project BloodHub by kazijehangir.

the class OrganizationRegistrationActivity method sendVerificationEmail.

private void sendVerificationEmail() {
    FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
    user.sendEmailVerification().addOnCompleteListener(new OnCompleteListener<Void>() {

        @Override
        public void onComplete(@NonNull Task<Void> task) {
            if (task.isSuccessful()) {
                FirebaseUser user = mAuth.getCurrentUser();
                writeNewUser(user.getUid(), user.getEmail());
                startActivity(new Intent(OrganizationRegistrationActivity.this, LoginActivity.class));
                finish();
            } else {
                overridePendingTransition(0, 0);
                finish();
                overridePendingTransition(0, 0);
                startActivity(getIntent());
            }
        }
    });
}
Also used : Intent(android.content.Intent) FirebaseUser(com.google.firebase.auth.FirebaseUser)

Example 33 with FirebaseUser

use of com.google.firebase.auth.FirebaseUser in project BloodHub by kazijehangir.

the class DonationsFragment method fetchData.

// Getting data from database
public ArrayList<BloodRequest> fetchData() {
    donations = new ArrayList<BloodRequest>();
    FirebaseAuth mAuth = FirebaseAuth.getInstance();
    FirebaseUser user = mAuth.getCurrentUser();
    db.orderByChild("userid").equalTo(user.getUid()).addValueEventListener(new ValueEventListener() {

        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            for (DataSnapshot child : dataSnapshot.getChildren()) {
                Donation donation = child.getValue(Donation.class);
                if (donation != null) {
                    FirebaseDatabase.getInstance().getReference().child("bloodrequests").child(donation.requestid).addListenerForSingleValueEvent(new ValueEventListener() {

                        @Override
                        public void onDataChange(DataSnapshot dataSnapshot) {
                            BloodRequest request = dataSnapshot.getValue(BloodRequest.class);
                            if (request != null) {
                                donations.add(request);
                                numDonations.setText("LIVES SAVED: " + donations.size());
                                mAdapter.notifyDataSetChanged();
                            }
                        }

                        @Override
                        public void onCancelled(DatabaseError databaseError) {
                        }
                    });
                }
                FirebaseDatabase.getInstance().getReference().child("bloodrequests").child(donation.requestid).addListenerForSingleValueEvent(new ValueEventListener() {

                    @Override
                    public void onDataChange(DataSnapshot dataSnapshot) {
                        BloodRequest request = dataSnapshot.getValue(BloodRequest.class);
                        donations.add(request);
                        numDonations.setText("LIVES SAVED: " + donations.size());
                        mAdapter.notifyDataSetChanged();
                    }

                    @Override
                    public void onCancelled(DatabaseError databaseError) {
                    }
                });
            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
        }
    });
    return donations;
}
Also used : DatabaseError(com.google.firebase.database.DatabaseError) Donation(com.jexapps.bloodhub.m_Model.Donation) FirebaseUser(com.google.firebase.auth.FirebaseUser) ValueEventListener(com.google.firebase.database.ValueEventListener) BloodRequest(com.jexapps.bloodhub.m_Model.BloodRequest) DataSnapshot(com.google.firebase.database.DataSnapshot) FirebaseAuth(com.google.firebase.auth.FirebaseAuth)

Example 34 with FirebaseUser

use of com.google.firebase.auth.FirebaseUser in project BloodHub by kazijehangir.

the class IndividualRegistrationActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_individual_registration);
    setTitle("Individual Registration");
    // make the actionbar show arrow to go back to login
    ActionBar actionBar = getSupportActionBar();
    actionBar.setDisplayHomeAsUpEnabled(true);
    mAuth = FirebaseAuth.getInstance();
    mAuthListener = new FirebaseAuth.AuthStateListener() {

        @Override
        public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
            FirebaseUser user = firebaseAuth.getCurrentUser();
            if (user != null) {
                Toast toast = Toast.makeText(getApplicationContext(), "Sending email", Toast.LENGTH_SHORT);
                toast.show();
                sendVerificationEmail();
            } else {
                Toast toast = Toast.makeText(getApplicationContext(), "Not sending email", Toast.LENGTH_SHORT);
                toast.show();
            }
        }
    };
    username = (AutoCompleteTextView) findViewById(R.id.name);
    mEmailView = (AutoCompleteTextView) findViewById(R.id.email);
    mPasswordView = (EditText) findViewById(R.id.password);
    // progressBar = (ProgressBar) findViewById(R.id.progressBar);
    bloodGroup = (Spinner) findViewById(R.id.spin);
    try {
        Field popup = Spinner.class.getDeclaredField("mPopup");
        popup.setAccessible(true);
        android.widget.ListPopupWindow popupWindow = (android.widget.ListPopupWindow) popup.get(bloodGroup);
        popupWindow.setHeight(500);
    } catch (NoClassDefFoundError | ClassCastException | NoSuchFieldException | IllegalAccessException e) {
    }
    List<String> bgroups = Arrays.asList(getResources().getStringArray(R.array.blood_groups));
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.spinner_text_view, bgroups) {

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View v = super.getView(position, convertView, parent);
            v.setPadding(8, 8, 8, 15);
            if (position == getCount()) {
                ((TextView) v.findViewById(R.id.text1)).setText("");
                ((TextView) v.findViewById(R.id.text1)).setHint(getItem(getCount()));
            }
            return v;
        }

        @Override
        public int getCount() {
            return super.getCount() - 1;
        }
    };
    adapter.setDropDownViewResource(R.layout.spinner_text_view);
    bloodGroup.setAdapter(adapter);
    bloodGroup.setSelection(adapter.getCount());
    // Set OnClick Listeners for buttons
    photo_btn = (ImageButton) findViewById(R.id.profile_photo);
    photo_btn.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            Intent intent = new Intent(Intent.ACTION_PICK);
            intent.setType("image/*");
            startActivityForResult(intent, GALLERY_INTENT);
        }
    });
    Button mRegisterButton = (Button) findViewById(R.id.register_individual_button);
    mRegisterButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            registerNewUser();
        }
    });
}
Also used : Field(java.lang.reflect.Field) Toast(android.widget.Toast) ImageButton(android.widget.ImageButton) Button(android.widget.Button) AutoCompleteTextView(android.widget.AutoCompleteTextView) TextView(android.widget.TextView) ActionBar(android.support.v7.app.ActionBar) FirebaseAuth(com.google.firebase.auth.FirebaseAuth) ViewGroup(android.view.ViewGroup) Intent(android.content.Intent) FirebaseUser(com.google.firebase.auth.FirebaseUser) View(android.view.View) AutoCompleteTextView(android.widget.AutoCompleteTextView) TextView(android.widget.TextView) ArrayAdapter(android.widget.ArrayAdapter)

Example 35 with FirebaseUser

use of com.google.firebase.auth.FirebaseUser in project BloodHub by kazijehangir.

the class IndividualRegistrationActivity method sendVerificationEmail.

private void sendVerificationEmail() {
    FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
    user.sendEmailVerification().addOnCompleteListener(new OnCompleteListener<Void>() {

        @Override
        public void onComplete(@NonNull Task<Void> task) {
            Toast.makeText(getApplicationContext(), "sent email", Toast.LENGTH_SHORT);
            if (task.isSuccessful()) {
                FirebaseAuth.getInstance().signOut();
                // startActivity(new Intent(IndividualRegistrationActivity.this, LoginActivity.class));
                finish();
                Toast.makeText(getApplicationContext(), "sent email success", Toast.LENGTH_SHORT);
            } else {
                overridePendingTransition(0, 0);
                finish();
                // overridePendingTransition(0, 0 );
                // startActivity(getIntent());
                Toast.makeText(getApplicationContext(), "sent email failed", Toast.LENGTH_SHORT);
            }
        }
    });
}
Also used : FirebaseUser(com.google.firebase.auth.FirebaseUser)

Aggregations

FirebaseUser (com.google.firebase.auth.FirebaseUser)115 AuthResult (com.google.firebase.auth.AuthResult)29 Intent (android.content.Intent)23 FirebaseAuth (com.google.firebase.auth.FirebaseAuth)16 AuthCredential (com.google.firebase.auth.AuthCredential)14 DatabaseReference (com.google.firebase.database.DatabaseReference)13 OnFailureListener (com.google.android.gms.tasks.OnFailureListener)12 DataSnapshot (com.google.firebase.database.DataSnapshot)12 DatabaseError (com.google.firebase.database.DatabaseError)12 ValueEventListener (com.google.firebase.database.ValueEventListener)11 View (android.view.View)10 Button (android.widget.Button)10 Task (com.google.android.gms.tasks.Task)10 NonNull (android.support.annotation.NonNull)8 OnCompleteListener (com.google.android.gms.tasks.OnCompleteListener)7 FirebaseAuthUserCollisionException (com.google.firebase.auth.FirebaseAuthUserCollisionException)7 TextView (android.widget.TextView)6 Test (org.junit.Test)6 Context (android.content.Context)5 SharedPreferences (android.content.SharedPreferences)5