Android studio firebase Authentication cannot login with registered user in app

  android, android-studio, java

I registered a user in the system with a registration code in android studio.

public class Register extends AppCompatActivity {

Button button;
private EditText email,password,nickname;
private FirebaseAuth firebaseAuth;
DatabaseReference databsereference;
FirebaseUser firebaseUser;
String language2="English";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_register);
    hideNavigationBan();
    Intent i = getIntent();
    language2 = i.getStringExtra("language");
    button = (Button) findViewById(R.id.Enter);
    firebaseAuth = FirebaseAuth.getInstance();
    databsereference = FirebaseDatabase.getInstance().getReference("user");
    email = findViewById(R.id.e_mail);
    password = findViewById(R.id.password);
    nickname = findViewById(R.id.name_enter);
    //check if some places are empty and register user in database
    hideHint(email,"E-mail");
    hideHint(password,"Password");
    hideHint(nickname,"Name");
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String Email = email.getText().toString();
            String Password = password.getText().toString();
            String Name = nickname.getText().toString();
            if(Name.isEmpty()){
                nickname.setError("Please enter nickname");
                nickname.requestFocus();
            }
            else if(Email.isEmpty()){
                email.setError("Please enter email id");
                email.requestFocus();
            }
            else  if(Password.isEmpty()){
                password.setError("Please enter your password");
                password.requestFocus();
            }
            else  if(Email.isEmpty() && Password.isEmpty()&&Name.isEmpty()){
                Toast.makeText(Register.this,"Fields Are Empty!",Toast.LENGTH_SHORT).show();
            }
            else  if(!(Email.isEmpty() && Password.isEmpty()&&Name.isEmpty())){
                firebaseAuth.createUserWithEmailAndPassword(Email, Password).addOnCompleteListener(Register.this, new OnCompleteListener<AuthResult>() {
                    @Override
                    public void onComplete(@NonNull Task<AuthResult> task) {
                        if(!task.isSuccessful()){
                            Toast.makeText(Register.this,"SignUp Unsuccessful, Please Try Again",Toast.LENGTH_SHORT).show();
                        }
                        else {
                            addUser();
                            Intent intent = new Intent(Register.this, nav.class);
                            intent.putExtra("language",language2);
                            startActivity(intent);

                        }
                    }

                });
            }
            else{
                Toast.makeText(Register.this,"Error Occurred!",Toast.LENGTH_SHORT).show();

            }
        }
    });
}
//method to add user into system
private void addUser(){
    String Email = email.getText().toString();
    String Name = nickname.getText().toString();
    String Password = password.getText().toString();
    int number = 0;
    RegisterDatabase registerDatabase = new RegisterDatabase(Name,Email,Password,number);
    databsereference.child(FirebaseAuth.getInstance().getCurrentUser().getUid()).setValue(registerDatabase);
    FirebaseUser user = firebaseAuth.getCurrentUser();
    updateUI(user);
}
public void hideNavigationBan(){
    this.getWindow().getDecorView().setSystemUiVisibility(
            View.SYSTEM_UI_FLAG_FULLSCREEN|
                    View.SYSTEM_UI_FLAG_HIDE_NAVIGATION|
                    View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY|
                    View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN|
                    View.SYSTEM_UI_FLAG_HIDE_NAVIGATION|
                    View.SYSTEM_UI_FLAG_LAYOUT_STABLE
    );
}
private void hideHint(EditText editText,String text){
    editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if(hasFocus){
                editText.setHint("");
            }
            else{
                editText.setHint(text);
            }
        }
    });
}

@Override
protected void onStart() {
    super.onStart();
    hideNavigationBan();
}

@Override
protected void onPause() {
    super.onPause();
    hideNavigationBan();
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState, @Nullable PersistableBundle persistentState) {
    hideNavigationBan();
    super.onCreate(savedInstanceState, persistentState);
}

@Override
protected void onResume() {
    super.onResume();
    hideNavigationBan();
}

@Override
protected void onRestart() {
    super.onRestart();
    hideNavigationBan();
}
private void updateUI(FirebaseUser user) {

}

}

When I logout with the created account and log in again it says that ‘Login Error, Please Login Again’, which means that authentication is failed but all data which I put in fields is right.

 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.login);
    hideSoftKeyboard();
    hideNavigationBan();
    initGoogleAPIClient();
    checkPermissions();
    language = findViewById(R.id.changelanguage);
    language2 = Locale.getDefault().getDisplayLanguage();
    language.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            changelangauge();
        }
    });
    button = findViewById(R.id.button2);
    firebaseAuth = FirebaseAuth.getInstance();
    email = findViewById(R.id.editText4);
    password = findViewById(R.id.editText5);
    hideHint(email,"E-mail");
    hideHint(password,"Password");
    // if user is already login then application send it to next page
    mfirebaseAuth = new FirebaseAuth.AuthStateListener() {
        @Override
        public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
            FirebaseUser mFirebaseUser = firebaseAuth.getCurrentUser();
            if( mFirebaseUser != null ){
                Intent i = new Intent(MainActivity.this, nav.class);
                i.putExtra("language",language2);
                startActivity(i);
            }
        }
    };
    //login into application and control if fields are empty
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String Email = email.getText().toString();
            String Password = password.getText().toString();
            if(Email.isEmpty()){
                email.setError("Please enter email id");
                email.requestFocus();
            }
            else if(Password.isEmpty()){
                password.setError("Please enter your password");
                password.requestFocus();
            }
            else if(!Email.isEmpty() && !Password.isEmpty()){
                firebaseAuth.signInWithEmailAndPassword(Email, Password).addOnCompleteListener(MainActivity.this, new OnCompleteListener<AuthResult>() {
                    @Override
                    public void onComplete(@NonNull Task<AuthResult> task) {
                        if(task.isSuccessful()){
                            FirebaseUser user = firebaseAuth.getCurrentUser();
                            Intent intToHome = new Intent(MainActivity.this,nav.class);
                            intToHome.putExtra("language",language2);
                            startActivity(intToHome);
                            finish();
                        }
                        else{
                            Toast.makeText(MainActivity.this,"Login Error, Please Login Again",Toast.LENGTH_SHORT).show();
                        }
                    }
                });
            }
            else{
                Toast.makeText(MainActivity.this,"Error Occurred!",Toast.LENGTH_SHORT).show();

            }
        }
    });
    Register = findViewById(R.id.buttonRegister);
    //open registration screen
    Register.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            openRegister();
        }
    });
    Anonymous = findViewById(R.id.Anonymous);
    // if user click on anonymous button then application login user anonymously
    Anonymous.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            firebaseAuth.signInAnonymously().
                    addOnCompleteListener(MainActivity.this,new OnCompleteListener<AuthResult>() {
                @Override
                public void onComplete(@NonNull Task<AuthResult> task)
                {
                    if (task.isSuccessful())
                    {
                        Intent intToHome = new Intent(MainActivity.this,nav.class);
                        intToHome.putExtra("language",language2);
                        startActivity(intToHome);
                        finish();
                    }
                }
            });
        }
    });


}
public void openSlider(){
    Intent intent = new Intent(this, Slider.class);
    startActivity(intent);
    finish();
}
public void openRegister(){
    Intent intent = new Intent(this, Register.class);
    intent.putExtra("language",language2);
    startActivity(intent);
    finish();
}
public void changelangauge(){
    Log.d("Counter",""+i);
            i = i+1;
    switch (language2) {
        case "English": {
            context = LocaleHelper.setLocale(MainActivity.this, "et");
            Locale locale = new Locale("et");
            Locale.setDefault(locale);
            language2 = Locale.getDefault().getDisplayLanguage();
            Log.d("Language", language2);
            resources = context.getResources();
            language.setText(resources.getString(R.string.language));
            button.setText(resources.getText(R.string.log_in));
            email.setHint(resources.getString(R.string.e_mail));
            password.setHint(resources.getString(R.string.password));
            Register.setText(resources.getString(R.string.register));
            Anonymous.setText(resources.getString(R.string.anonymous));
            break;
        }
        case "eesti": {
            context = LocaleHelper.setLocale(MainActivity.this, "ru");
            Locale locale = new Locale("ru");
            Locale.setDefault(locale);
            language2 = Locale.getDefault().getDisplayLanguage();
            Log.d("Language", language2);
            resources = context.getResources();
            language.setText(resources.getString(R.string.language));
            button.setText(resources.getText(R.string.log_in));
            email.setHint(resources.getString(R.string.e_mail));
            password.setHint(resources.getString(R.string.password));
            Register.setText(resources.getString(R.string.register));
            Anonymous.setText(resources.getString(R.string.anonymous));
            break;
        }
        case "русский": {
            context = LocaleHelper.setLocale(MainActivity.this, "en");
            Locale locale = new Locale("en");
            Locale.setDefault(locale);
            language2 = Locale.getDefault().getDisplayLanguage();
            Log.d("Language", language2);
            resources = context.getResources();
            language.setText(resources.getString(R.string.language));
            button.setText(resources.getText(R.string.log_in));
            email.setHint(resources.getString(R.string.e_mail));
            password.setHint(resources.getString(R.string.password));
            Register.setText(resources.getString(R.string.register));
            Anonymous.setText(resources.getString(R.string.anonymous));
            break;
        }
    }
}

@Override
protected void onStop() {
    super.onStop();
    if (mfirebaseAuth != null ){
        FirebaseAuth.getInstance().removeAuthStateListener(mfirebaseAuth);
    }
}
@Override
protected void onStart() {
    super.onStart();
    hideNavigationBan();
    firebaseAuth.addAuthStateListener(mfirebaseAuth);
}
private void hideSoftKeyboard(){
    this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
}

@Override
protected void onPause() {
    super.onPause();
    hideNavigationBan();
}

@Override
public void onCreate(@Nullable Bundle savedInstanceState, @Nullable PersistableBundle persistentState) {
    hideNavigationBan();
    super.onCreate(savedInstanceState, persistentState);
}

@Override
protected void onResume() {
    super.onResume();
    hideNavigationBan();
}

@Override
protected void onRestart() {
    super.onRestart();
    hideNavigationBan();
}

public void hideNavigationBan(){
    this.getWindow().getDecorView().setSystemUiVisibility(
            View.SYSTEM_UI_FLAG_FULLSCREEN|
                    View.SYSTEM_UI_FLAG_HIDE_NAVIGATION|
                    View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY|
                    View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN|
                    View.SYSTEM_UI_FLAG_HIDE_NAVIGATION|
                    View.SYSTEM_UI_FLAG_LAYOUT_STABLE
    );
}

@Override
public void finish() {
    super.finish();
    overridePendingTransition(R.anim.slide_in_left,R.anim.slide_in_right);
}
private void checkPermissions() {
    if (ContextCompat.checkSelfPermission(MainActivity.this,
            Manifest.permission.ACCESS_FINE_LOCATION)
            != PackageManager.PERMISSION_GRANTED)
        requestLocationPermission();
    else
        showSettingDialog();

}
private void requestLocationPermission() {
    if (ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, android.Manifest.permission.ACCESS_FINE_LOCATION)) {
        ActivityCompat.requestPermissions(MainActivity.this,
                new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION},
                ACCESS_FINE_LOCATION_INTENT_ID);

    } else {
        ActivityCompat.requestPermissions(MainActivity.this,
                new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
                ACCESS_FINE_LOCATION_INTENT_ID);
    }
}
private void showSettingDialog() {
    LocationRequest locationRequest = LocationRequest.create();
    locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);//Setting priotity of Location request to high
    locationRequest.setInterval(30 * 1000);
    locationRequest.setFastestInterval(5 * 1000);//5 sec Time interval for location update
    LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder()
            .addLocationRequest(locationRequest);
    builder.setAlwaysShow(true); //this is the key ingredient to show dialog always when GPS is off

    PendingResult<LocationSettingsResult> result =
            LocationServices.SettingsApi.checkLocationSettings(mGoogleApiClient, builder.build());
    result.setResultCallback(new ResultCallback<LocationSettingsResult>() {
        @Override
        public void onResult(LocationSettingsResult result) {
            final Status status = result.getStatus();
            switch (status.getStatusCode()) {
                case LocationSettingsStatusCodes.SUCCESS:
                    // All location settings are satisfied. The client can initialize location
                    // requests here.
                    break;
                case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
                    // Location settings are not satisfied. But could be fixed by showing the user
                    // a dialog.
                    try {
                        // Show the dialog by calling startResolutionForResult(),
                        // and check the result in onActivityResult().
                        status.startResolutionForResult(MainActivity.this, REQUEST_CHECK_SETTINGS);
                    } catch (IntentSender.SendIntentException e) {
                        e.printStackTrace();
                        // Ignore the error.
                    }
                    break;
                case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
                    // Location settings are not satisfied. However, we have no way to fix the
                    // settings so we won't show the dialog.
                    break;
            }
        }
    });
}
private void initGoogleAPIClient() {
    //Without Google API Client Auto Location Dialog will not work
    mGoogleApiClient = new GoogleApiClient.Builder(MainActivity.this)
            .addApi(LocationServices.API)
            .build();
    mGoogleApiClient.connect();
}
private void hideHint(EditText editText,String text){
    editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if(hasFocus){
                editText.setHint("");
            }
            else{
                editText.setHint(text);
            }
        }
    });
}

I also tested to create account in firebase and with that i can login but with created account in application I cannot.

Source: Android Questions

LEAVE A COMMENT