Search in sources :

Example 71 with FirebaseUser

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

the class AppointmentsFragment method fetchData.

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

        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            appointments.clear();
            keys.clear();
            for (DataSnapshot child : dataSnapshot.getChildren()) {
                Appointment appointment = child.getValue(Appointment.class);
                appointments.add(appointment);
                keys.add(child.getKey());
            }
            numAppointments.setText("Appointments: " + appointments.size());
            mAdapter.notifyDataSetChanged();
        }

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

Example 72 with FirebaseUser

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

the class AppointmentsOrgFragment method fetchData.

// Getting data from database
public void fetchData() {
    FirebaseAuth mAuth = FirebaseAuth.getInstance();
    FirebaseUser user = mAuth.getCurrentUser();
    db.orderByChild("orgid").equalTo(user.getUid()).addValueEventListener(new ValueEventListener() {

        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            appointments.clear();
            keys.clear();
            for (DataSnapshot child : dataSnapshot.getChildren()) {
                Appointment appointment = child.getValue(Appointment.class);
                appointments.add(appointment);
                keys.add(child.getKey());
            }
            numAppointments.setText("Appointments: " + appointments.size());
            mAdapter.notifyDataSetChanged();
        }

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

Example 73 with FirebaseUser

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

the class IndividualRegistrationActivity method registerNewUser.

private void registerNewUser() {
    email = mEmailView.getText().toString().trim();
    password = mPasswordView.getText().toString().trim();
    uname = username.getText().toString();
    bgroup = bloodGroup.getSelectedItem().toString();
    if (uname.isEmpty()) {
        Toast.makeText(this, "You forgot to enter your name.", Toast.LENGTH_SHORT).show();
    } else {
        if (!isEmailValid(email)) {
            Toast.makeText(this, "Not a valid email address", Toast.LENGTH_SHORT).show();
        } else {
            if (!isPasswordValid(password)) {
                Toast.makeText(this, "Password is too short, minimum length is 6.", Toast.LENGTH_SHORT).show();
            } else {
                if (bgroup.contentEquals("Blood Group")) {
                    Toast.makeText(this, "You forgot to choose your blood group.", Toast.LENGTH_SHORT).show();
                } else {
                    // register user with firebase
                    // 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();
                                Toast.makeText(context, "Registration Successful!", Toast.LENGTH_SHORT).show();
                                // set default subscriptions
                                int id = Arrays.asList(getResources().getStringArray(R.array.blood_groups)).indexOf(bgroup);
                                FirebaseMessaging.getInstance().subscribeToTopic("Request_" + id);
                                FirebaseMessaging.getInstance().subscribeToTopic("URGENT");
                                // take user to main screen
                                sendVerificationEmail();
                                writeNewUser(user.getUid(), user.getEmail());
                                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 : Context(android.content.Context) AuthResult(com.google.firebase.auth.AuthResult) Intent(android.content.Intent) FirebaseAuthUserCollisionException(com.google.firebase.auth.FirebaseAuthUserCollisionException) FirebaseUser(com.google.firebase.auth.FirebaseUser)

Example 74 with FirebaseUser

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

the class LoginActivity method attemptLogin.

private void attemptLogin() {
    // Reset errors.
    mEmailView.setError(null);
    mPasswordView.setError(null);
    // Store values at the time of the login attempt.
    String email = mEmailView.getText().toString();
    String password = mPasswordView.getText().toString();
    boolean cancel = false;
    View focusView = null;
    // Check for a valid password, if the user entered one.
    if (TextUtils.isEmpty(password) && !isPasswordValid(password)) {
        mPasswordView.setError(getString(R.string.error_invalid_password));
        focusView = mPasswordView;
        cancel = true;
    }
    // Check for a valid email address.
    if (TextUtils.isEmpty(email)) {
        mEmailView.setError(getString(R.string.error_field_required));
        focusView = mEmailView;
        cancel = true;
    } else if (!isEmailValid(email)) {
        mEmailView.setError(getString(R.string.error_invalid_email));
        focusView = mEmailView;
        cancel = true;
    }
    if (cancel) {
        // There was an error; don't attempt login and focus the first
        // form field with an error.
        focusView.requestFocus();
    } else {
        // Show a progress spinner, and kick off a background task to
        // perform the user login attempt.
        showProgress(true);
        final Context context = getApplicationContext();
        mAuth.signInWithEmailAndPassword(email, password).addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {

            @Override
            public void onComplete(@NonNull Task<AuthResult> task) {
                if (task.isSuccessful()) {
                    FirebaseUser user = mAuth.getCurrentUser();
                    if (user.isEmailVerified()) {
                        setView(user.getUid(), user.getEmail());
                    } else {
                        Toast toast = Toast.makeText(getApplicationContext(), "Please verify email address", Toast.LENGTH_SHORT);
                        toast.show();
                        showProgress(false);
                        FirebaseAuth.getInstance().signOut();
                    }
                } else {
                    showProgress(false);
                    Toast.makeText(context, "Authentication failed.", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
}
Also used : Context(android.content.Context) Toast(android.widget.Toast) AuthResult(com.google.firebase.auth.AuthResult) FirebaseUser(com.google.firebase.auth.FirebaseUser) View(android.view.View) AutoCompleteTextView(android.widget.AutoCompleteTextView) TextView(android.widget.TextView)

Example 75 with FirebaseUser

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

the class MyRequestsFragment method fetchData.

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

        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            requests.clear();
            keys.clear();
            for (DataSnapshot child : dataSnapshot.getChildren()) {
                BloodRequest request = child.getValue(BloodRequest.class);
                requests.add(request);
                keys.add(child.getKey());
            }
            numRequests.setText("Total Requests: " + requests.size());
            mAdapter.notifyDataSetChanged();
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
        }
    });
    return;
}
Also used : DatabaseError(com.google.firebase.database.DatabaseError) 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)

Aggregations

FirebaseUser (com.google.firebase.auth.FirebaseUser)100 AuthResult (com.google.firebase.auth.AuthResult)26 Intent (android.content.Intent)22 FirebaseAuth (com.google.firebase.auth.FirebaseAuth)16 DataSnapshot (com.google.firebase.database.DataSnapshot)12 DatabaseError (com.google.firebase.database.DatabaseError)12 OnFailureListener (com.google.android.gms.tasks.OnFailureListener)11 AuthCredential (com.google.firebase.auth.AuthCredential)11 DatabaseReference (com.google.firebase.database.DatabaseReference)11 ValueEventListener (com.google.firebase.database.ValueEventListener)11 View (android.view.View)10 Task (com.google.android.gms.tasks.Task)10 Button (android.widget.Button)9 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