保存数据时登录失败。(注册)

时间:2017-10-04 14:46:15

标签: android firebase firebase-authentication

我正在制作一个Android项目,对于初学者,我一直在研究注册的工作原理。顺便说一句,我使用了Firebase。这是我的代码:

package com....

import...


public class MainActivity extends AppCompatActivity {

    //VIEW AND WIDGETS
    Button createUser, moveToLoginBtn;
    EditText userEmailEdit, userPasswordEdit;

    //FIREBASE AUTH FIELDS
    FirebaseAuth nAuth;
    FirebaseAuth.AuthStateListener nAuthlistener;

    DatabaseReference mDatabaseRef, mUserCheckData;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        //ASSIGN ID
        createUser = (Button) findViewById(R.id.createUserBtn);
        moveToLoginBtn = (Button) findViewById(R.id.moveToLogin);
        userEmailEdit = (EditText) findViewById(R.id.emailEditTextCreate);
        userPasswordEdit = (EditText) findViewById(R.id.passEditTextCreate);

        //ASSIGN INSTANCE
        mDatabaseRef = FirebaseDatabase.getInstance().getReference();
        mUserCheckData =  FirebaseDatabase.getInstance().getReference().child("Users");
        nAuth = FirebaseAuth.getInstance();


        nAuthlistener = new FirebaseAuth.AuthStateListener(){
            @Override
            public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {

                FirebaseUser user = firebaseAuth.getCurrentUser();

                if (user != null) {

                    final String emailForVer = user.getEmail();
                    mUserCheckData.addValueEventListener(new ValueEventListener() {
                        @Override
                        public void onDataChange(DataSnapshot dataSnapshot) {

                            checkUserValidation(dataSnapshot, emailForVer);

                        }

                        @Override
                        public void onCancelled(DatabaseError databaseError) {

                        }
                    });

                } else {


                }


            }

        };

        //ON CLICK LISTENER

        createUser.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view) {

            final String userEmailString, userPassString;
            userEmailString = userEmailEdit.getText().toString().trim();
            userPassString = userPasswordEdit.getText().toString().trim();

             if (!TextUtils.isEmpty(userEmailString) && !TextUtils.isEmpty(userPassString))
             {
                 nAuth.createUserWithEmailAndPassword(userEmailString,userPassString).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
                     @Override
                     public void onComplete(@NonNull Task<AuthResult> task) {

                        if (task.isSuccessful())
                        {


                            DatabaseReference mChildDatabase = mDatabaseRef.child("Users").push();
                            String key_user = mChildDatabase.getKey();
                            mChildDatabase.child("isVerified").setValue("unverified");
                            mChildDatabase.child("userKey").setValue(key_user);
                            mChildDatabase.child("emailUser").setValue(userEmailString);
                            mChildDatabase.child("passWordUser").setValue(userPassString);
                            Toast.makeText(MainActivity.this, "User Account Created!", Toast.LENGTH_LONG).show();

                            startActivity(new Intent(MainActivity.this, Profile.class));
                        }
                        else
                        {
                            Toast.makeText(MainActivity.this, "User Account Creation Fail", Toast.LENGTH_LONG).show();
                            startActivity(new Intent(MainActivity.this, MainActivity.class));

                        }
                     }
                 });
             }



            }
        });

        //MOVE TO LOGIN

        moveToLoginBtn.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view){

            startActivity(new Intent(MainActivity.this, LoginActivity.class));



            }
        });

    }

    private void checkUserValidation(DataSnapshot dataSnapshot, String emailForVer) {


        Iterator iterator = dataSnapshot.getChildren().iterator();

        while (iterator.hasNext())
        {

            DataSnapshot dataUser = (DataSnapshot) iterator.next();



            if(String.valueOf(dataUser.child("emailUser").getValue()).equals(emailForVer) &&  dataUser.child("emailUser") != null)
            {

                if(String.valueOf(dataUser.child("isVerified").getValue()).equals("unverified") && dataUser.child("isVerified") != null)
                {

                    Intent in = new Intent(MainActivity.this, Profile.class);
                    in.putExtra("USER_KEY" ,  String.valueOf(dataUser.child("userKey").getValue()));
                    startActivity(in);
                    //in.putExtra("NAME_KEY" ,  String.valueOf(dataUser.child("nameKey").getValue()));



                }else
                {
                    startActivity(new Intent(MainActivity.this, Welcome.class));
                }

            }

        }

    }

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

        nAuth.addAuthStateListener(nAuthlistener);
    }

    @Override
    protected void onStop() {
        super.onStop();

        nAuth.removeAuthStateListener(nAuthlistener);
    }
}

我认为我已正确实施了我的方法。但它敬酒:

  

用户帐户创建失败

是我的 checkUseValidation()方法错了吗?任何形式的帮助表示赞赏。

另请注意我的:

 public void onComplete

方法,我认为我做对了。我不知道为什么数据没有保存到firebase数据库。或者为什么这个任务不成功,如下所示:

  if (task.isSuccessful())

非常感谢。

2 个答案:

答案 0 :(得分:1)

首先,您应该在失败条件下将吐司更改为

Toast.makeText(MainActivity.this, task.getException().getMessage(), Toast.LENGTH_LONG).show();

这样你就会在任务中出错。

但在此之前, 控制台

启用电子邮件/密码

答案 1 :(得分:1)

请参阅从网址注册新用户:https://firebase.google.com/docs/auth/android/start/?authuser=0

如果task.isSuccessful()为false。您可以添加此项以获取错误

Log.w(TAG, "createUserWithEmail:failure",task.getException());

将代码更新为:

createUser.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view) {

                final String userEmailString, userPassString;
                userEmailString = userEmailEdit.getText().toString().trim();
                userPassString = userPasswordEdit.getText().toString().trim();

                if (!TextUtils.isEmpty(userEmailString) && !TextUtils.isEmpty(userPassString))
                {
                    nAuth.createUserWithEmailAndPassword(userEmailString,userPassString).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
                        @Override
                        public void onComplete(@NonNull Task<AuthResult> task) {

                            if (task.isSuccessful())
                            {


                                DatabaseReference mChildDatabase = mDatabaseRef.child("Users").push();
                                String key_user = mChildDatabase.getKey();
                                mChildDatabase.child("isVerified").setValue("unverified");
                                mChildDatabase.child("userKey").setValue(key_user);
                                mChildDatabase.child("emailUser").setValue(userEmailString);
                                mChildDatabase.child("passWordUser").setValue(userPassString);
                                Toast.makeText(MainActivity.this, "User Account Created!", Toast.LENGTH_LONG).show();

                                startActivity(new Intent(MainActivity.this, Profile.class));
                            }
                            else
                            {
                                Log.w("TAG", "createUserWithEmail:failure",task.getException());
                                Toast.makeText(MainActivity.this, "User Account Creation Fail", Toast.LENGTH_LONG).show();
                                startActivity(new Intent(MainActivity.this, MainActivity.class));

                            }
                        }
                    });
                }



            }
        });

你应该在logcat中看到警告:

Logcat Android