Niels Vanwingh
Niels Vanwingh

Reputation: 604

Firebase Authentication Failing

I am using Firebase to register a User with Email and Password credentials.

I am using the following code for my Activity:

public class SignUpPage extends AppCompatActivity {

    private TextView profile;
    private EditText screen, mail, pass;
    private Button knop;
    private Typeface tfc_button;
    FirebaseAuth firebaseAuth;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_sign_up_page);
        setFontType();

        screen = (EditText)findViewById(R.id.SchermNaam);
        mail = (EditText)findViewById(R.id.PasWoord);
        knop = (Button)findViewById(R.id.SignUp_Button_SignUp);

        firebaseAuth = FirebaseAuth.getInstance();
    }

    public void setFontType(){

        profile = (TextView) findViewById(R.id.GebruikersProfiel);
        screen = (EditText) findViewById(R.id.SchermNaam);
        mail = (EditText) findViewById(R.id.EmailAdres);
        pass = (EditText) findViewById(R.id.PasWoord);
        knop = (Button) findViewById(R.id.SignUp_Button_SignUp);

        tfc_button = Typeface.createFromAsset(getAssets(), "fonts/TEMPSITC.TTF");
        profile.setTypeface(tfc_button);
        screen.setTypeface(tfc_button);
        mail.setTypeface(tfc_button);
        pass.setTypeface(tfc_button);
        knop.setTypeface(tfc_button);
    }

    public boolean paswoord_ok (final String passw_check){

        Pattern pattern;
        Matcher matcher;

        final String PASSWORD_PATTERN = "((?=.*\\d).{6,12})";

        pattern = Pattern.compile(PASSWORD_PATTERN);
        matcher = pattern.matcher(passw_check);

        return matcher.matches();
    }

    public boolean schermnaam_ok(final String scr_name_check){

        Pattern pattern;
        Matcher matcher;

        final String PASSWORD_PATTERN = "(.{5,15})";

        pattern = Pattern.compile(PASSWORD_PATTERN);
        matcher = pattern.matcher(scr_name_check);

        return matcher.matches();
    }

    //Hoe ga je dit testen?
    public void onClickSignUpPage(View view){

        String schermnaam = screen.getText().toString().trim();
        String emailadres = mail.getText().toString().trim();
        String paswoord = pass.getText().toString().trim();

        if(TextUtils.isEmpty(schermnaam)){
            Toast.makeText(this,"Schermnaam invullen", Toast.LENGTH_SHORT).show();
            return;
        }

        if(TextUtils.isEmpty(emailadres)){
            Toast.makeText(this,"Email invullen",Toast.LENGTH_SHORT).show();
            return;
        }

        if(!schermnaam_ok(schermnaam)){
            Toast.makeText(this,"schermnaam minstens 5 en maximum 15 tekens", Toast.LENGTH_SHORT).show();
            return;
        }

        if(!paswoord_ok(paswoord)){
            Toast.makeText(this,"paswoord tussen 6-12 karakters en minstens 1 cijfer", Toast.LENGTH_SHORT).show();
            return;
        }

        firebaseAuth.createUserWithEmailAndPassword(emailadres.trim(),paswoord)
                .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                    @Override
                    public void onComplete(@NonNull Task<AuthResult> task) {
                        if(task.isSuccessful()){
                            Toast.makeText(SignUpPage.this, "Nieuwe Speler Geregistreerd", Toast.LENGTH_SHORT).show();
                        }
                        else {
                            FirebaseAuthException e = (FirebaseAuthException) task.getException();
                            Toast.makeText(SignUpPage.this,"Fout in de SignUp"+e.getMessage(), Toast.LENGTH_SHORT).show();
                            Log.d("LoginActivity", "Failed Registration", e);
                            return;
                        }
                    }
                });

        Intent intent = new Intent(this, SignInPage.class);
        startActivity(intent);
    }

}

The onCompletListener that checks if the Task is succesful indicates that it is not. The ErrorMessage I am getting is:

Failed Registration com.google.firebase.auth.FirebaseAuthInvalidCredentialsException: The email address is badly formatted.

I have tried to add trim() to emailadres.trim(), but this is not functioning.

Any advice?

Upvotes: 0

Views: 48

Answers (1)

Niels Vanwingh
Niels Vanwingh

Reputation: 604

I used the wrong View in assigning to the "email" variable.. Correct code here..

public class SignUpPage extends AppCompatActivity {

    private TextView profile;
    private EditText screen, mail, pass;
    private Button knop;
    private Typeface tfc_button;
    FirebaseAuth firebaseAuth;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_sign_up_page);
        setFontType();

        screen = (EditText)findViewById(R.id.SchermNaam);
        mail = (EditText)findViewById(R.id.EmailAdres);
        knop = (Button)findViewById(R.id.SignUp_Button_SignUp);

        firebaseAuth = FirebaseAuth.getInstance();
    }

    public void setFontType(){

        profile = (TextView) findViewById(R.id.GebruikersProfiel);
        screen = (EditText) findViewById(R.id.SchermNaam);
        mail = (EditText) findViewById(R.id.EmailAdres);
        pass = (EditText) findViewById(R.id.PasWoord);
        knop = (Button) findViewById(R.id.SignUp_Button_SignUp);

        tfc_button = Typeface.createFromAsset(getAssets(), "fonts/TEMPSITC.TTF");
        profile.setTypeface(tfc_button);
        screen.setTypeface(tfc_button);
        mail.setTypeface(tfc_button);
        pass.setTypeface(tfc_button);
        knop.setTypeface(tfc_button);
    }

    public boolean paswoord_ok (final String passw_check){

        Pattern pattern;
        Matcher matcher;

        final String PASSWORD_PATTERN = "((?=.*\\d).{6,12})";

        pattern = Pattern.compile(PASSWORD_PATTERN);
        matcher = pattern.matcher(passw_check);

        return matcher.matches();
    }

    public boolean schermnaam_ok(final String scr_name_check){

        Pattern pattern;
        Matcher matcher;

        final String PASSWORD_PATTERN = "(.{5,15})";

        pattern = Pattern.compile(PASSWORD_PATTERN);
        matcher = pattern.matcher(scr_name_check);

        return matcher.matches();
    }

    //Hoe ga je dit testen?
    public void onClickSignUpPage(View view){

        String schermnaam = screen.getText().toString().trim();
        String emailadres = mail.getText().toString().trim();
        String paswoord = pass.getText().toString().trim();

        if(TextUtils.isEmpty(schermnaam)){
            Toast.makeText(this,"Schermnaam invullen", Toast.LENGTH_SHORT).show();
            return;
        }

        if(TextUtils.isEmpty(emailadres)){
            Toast.makeText(this,"Email invullen",Toast.LENGTH_SHORT).show();
            return;
        }

        if(!schermnaam_ok(schermnaam)){
            Toast.makeText(this,"schermnaam minstens 5 en maximum 15 tekens", Toast.LENGTH_SHORT).show();
            return;
        }

        if(!paswoord_ok(paswoord)){
            Toast.makeText(this,"paswoord tussen 6-12 karakters en minstens 1 cijfer", Toast.LENGTH_SHORT).show();
            return;
        }

        firebaseAuth.createUserWithEmailAndPassword(emailadres.trim(),paswoord)
                .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                    @Override
                    public void onComplete(@NonNull Task<AuthResult> task) {
                        if(task.isSuccessful()){
                            Toast.makeText(SignUpPage.this, "Nieuwe Speler Geregistreerd", Toast.LENGTH_SHORT).show();
                        }
                        else {
                            FirebaseAuthException e = (FirebaseAuthException) task.getException();
                            Toast.makeText(SignUpPage.this,"Fout in de SignUp"+e.getMessage(), Toast.LENGTH_SHORT).show();
                            Log.d("LoginActivity", "Failed Registration", e);
                            return;
                        }
                    }
                });

        Intent intent = new Intent(this, SignInPage.class);
        startActivity(intent);
    }

}

Upvotes: 1

Related Questions