fullstackreact / react-native-firestack

A firestack v3 react-native implementation
MIT License
715 stars 132 forks source link

firebase #351

Open dprasetyo opened 6 years ago

dprasetyo commented 6 years ago

Error:Execution failed for task ':app:transformClassesWithDexForDebug'.

com.android.build.api.transform.TransformException: com.android.ide.common.process.ProcessException: java.util.concurrent.ExecutionException: com.android.dex.DexException: Multiple dex files define Lcom/google/android/gms/internal/zzbth;

initFirebase(); ButterKnife.bind(this); loggedIn = isLoggedIn(); if (loggedIn) { // go to home goToHome(); } }

private void initFirebase() {
    firebaseAuth = FirebaseAuth.getInstance();
}

private void goToHome() {
    Intent intent = new Intent(this, home.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
    startActivity(intent);
}

@OnClick({R.id.button_login_activity_main, R.id.button_sign_up_activity_main})
public void onClick(Button button) {
    switch (button.getId()) {
        case R.id.button_login_activity_main:
            String username = editTextUsername.getText().toString().trim();
            String password = editTextPassword.getText().toString().trim();
            login(username, password);
            break;
        case R.id.button_sign_up_activity_main:
            //  go to form pendaftaran
            startActivity(new Intent(this, SignupActivity.class));
            break;
    }
}

private void login(final String username, final String password) {
    if (TextUtils.isEmpty(username)) {
        Snackbar.make(findViewById(android.R.id.content), R.string.error_message_username_empty, Snackbar.LENGTH_LONG)
                .show();
    } else if (TextUtils.isEmpty(password)) {
        Snackbar.make(findViewById(android.R.id.content), R.string.error_message_password_empty, Snackbar.LENGTH_LONG)
                .show();
    } else {
        //  do login
        showProgress();
        firebaseAuth.signInWithEmailAndPassword(username, password)
                .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                    @Override
                    public void onComplete(@NonNull Task<AuthResult> task) {
                        hideProgress();
                        if (task.isSuccessful()) {
                            //  login sucess
                            //  go to dashboard
                            goToHome();
                        } else {
                            //  login failed
                            showMessageBox("Login failed. Your username and password is not matched");
                        }
                    }
                });

    }
}

private void hideProgress() {
    relativeLayoutProgress.setVisibility(View.GONE);
    editTextUsername.setEnabled(true);
    editTextPassword.setEnabled(true);
}

private void showProgress() {
    relativeLayoutProgress.setVisibility(View.VISIBLE);
    editTextUsername.setEnabled(false);
    editTextPassword.setEnabled(false);
}

private void showMessageBox(String message) {
    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
    alertDialogBuilder.setTitle("Login");
    alertDialogBuilder.setMessage(message);
    alertDialogBuilder.setCancelable(false);
    alertDialogBuilder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {
            dialogInterface.dismiss();
        }
    });
    alertDialogBuilder.show();
}

public boolean isLoggedIn() {
    if (firebaseAuth.getCurrentUser() != null) {
        //  user logged in
        return true;
    } else {
        return false;
    }
}