Firebase身份验证失败

时间:2018-03-30 14:52:20

标签: java android firebase-authentication

我正在使用Firebase向用户注册电子邮件和密码凭据。

我在我的活动中使用以下代码:

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);
    }

}

检查Task是否成功的onCompletListener表明它不成功。我得到的ErrorMessage是:

  

注册失败com.google.firebase.auth.FirebaseAuthInvalidCredentialsException:电子邮件地址格式错误。

我曾尝试将trim()添加到emailadres.trim(),但这不起作用。

有什么建议吗?

1 个答案:

答案 0 :(得分:1)

我在分配给“email”变量时使用了错误的View ..在这里纠正代码..

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);
    }

}