用户在帐户基座创建帐户Android

时间:2018-05-09 15:11:29

标签: android firebase firebase-realtime-database firebase-authentication

我有一个简单的应用程序,人们可以注册(注册)。 之后我做了两件事:

1)我发送验证邮件;

2)我为该用户创建了firebase数据库信息。

我创建了两个节点,如图所示。

"用户"节点

"users" node

" users_settings"节点

"users_settings" node

有两个问题:

1)似乎每20个帐户中有1个在创建时出错,而且此特殊帐户未创建" users_settings"节点,它只是创建了"用户"节点,仅使用full_name字段。就像这张照片中的这个。

"users" node with error

没有" users_settings"节点

我问过两个用户遇到这个问题并且没有发现他们做错了什么,我创建了多个帐户但没有收到此错误。这就是解决这个问题难以解决的原因。

2)如果用户按下注册按钮并在加载时关闭应用程序,firebase会创建帐户,发送验证电子邮件,但不会创建任何firebase数据库信息,因为它不会进入这部分代码。有没有办法防止这种情况发生?

这是当人按下注册按钮时的代码,它只取字段(名称,emial,密码)并在firebase中注册。

RegisterFragment.java

    /**
 * When the user press the REGISTER BUTTON.
 */
private void init(){
    Log.i(TAG, "init: Initializing the button click");

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

            String email = mEmail.getText().toString();
            String fullName = mFullName.getText().toString();
            String password = mPassword.getText().toString();

            if (stringIsNull(email) || stringIsNull(fullName) || stringIsNull(password)){
                Toast.makeText(getActivity(), R.string.fillin_all_fields, Toast.LENGTH_SHORT).show();
            } else {

                firebaseMethods = new FirebaseMethods(getActivity());

                /*
                 * 1) Create an intent to the same Login Screen;
                 * 2) Register email (Send verification email, if OK start Login Screen again and send message to verify the email).
                 */
                Intent intentLogin = new Intent(getActivity(), LoginActivity.class);

                firebaseMethods.registerNewEmail(email, password, fullName, mProgressBar, intentLogin);

            }

        }
    });

}

这是我的 registerNewEmail Firebase方法:

FirebaseMethods.java

/**
 * Register a new user to the Firebase Authentication with his email and password.
 * @param email the user's email for registering
 * @param password the user's password for registering
 */
public void registerNewEmail(String email, String password, final String fullName, final ProgressBar progressBar, final Intent intent){

    progressBar.setVisibility(View.VISIBLE);

    mAuth.createUserWithEmailAndPassword(email, password)
            .addOnCompleteListener(new OnCompleteListener<AuthResult>() {
                @Override
                public void onComplete(@NonNull Task<AuthResult> task) {
                    if (task.isSuccessful()) {
                        // Sign in success, update UI with the signed-in user's information
                        Log.i(TAG, "createUserWithEmail:success");
                        final FirebaseUser user = mAuth.getCurrentUser();

                        if (user != null){

                            user.sendEmailVerification()
                                    .addOnCompleteListener(new OnCompleteListener<Void>() {
                                        @Override
                                        public void onComplete(@NonNull Task<Void> task) {
                                            if (task.isSuccessful()){
                                                Log.i(TAG, "onComplete: Successful send verification email");
                                                Toast.makeText(mContext, mContext.getString(R.string.confirm_email_verification)
                                                        + user.getEmail(), Toast.LENGTH_SHORT).show();

                                                // 1) Add user's info to database;
                                                // 2) Sign out to wait for the email verification;
                                                // 3) Go to login activity.
                                                addNewUser(fullName, "", "", "", "", "", false);

                                                mContext.startActivity(intent);

                                            } else {
                                                Log.i(TAG, "onComplete: Failed to send verification email");
                                                Toast.makeText(mContext, mContext.getString(R.string.failed_send_verification_email),
                                                        Toast.LENGTH_SHORT).show();
                                            }
                                        }
                                    });

                        }

                        /*updateUI(user);*/
                    } else {
                        // If sign in fails, display a message to the user.
                        Log.i(TAG, "createUserWithEmail:failure", task.getException());
                        if (task.getException() != null) {
                            Toast.makeText(mContext, "Authentication failed." + task.getException().getMessage(),
                                    Toast.LENGTH_LONG).show();
                        }
                        /*updateUI(null);*/
                    }

                    progressBar.setVisibility(View.GONE);
                }
            });

}

这是我的 addNewUser 方法:

FirebaseMethods.java

    private void addNewUser(String fullName, String birthDate, String beltColor, String registrationNumber, String dojo, String profileImgURL, boolean verified) {

    FirebaseUser currentUser = mAuth.getCurrentUser();

    if (currentUser != null) {

        String userID = currentUser.getUid();

        Log.i(TAG, "addNewUser: Adding new user database information: " + userID);


        // Creating the user model with the information provided.
        User user = new User(
                dojo,
                fullName,
                profileImgURL,
                registrationNumber,
                verified);

        // Adding to the database;
        myRef.child(mContext.getString(R.string.dbname_users))
                .child(userID)
                .setValue(user);

        // Creating the user_settings model with the information provided.
        UserSettings user_settings = new UserSettings(
                beltColor,
                birthDate,
                userID);

        // Adding to the database
        myRef.child(mContext.getString(R.string.dbname_users_settings))
                .child(userID)
                .setValue(user_settings);

        // 1) Added the information for the user that just registered;
        // 2) Sign out to wait for email verification.
        mAuth.signOut();

    }

}

您可以看到我创建完整模型User(dojo,fullName,profileImgURL,registrationNumber,已验证)和完整模型UserSettings(beltColor,birthDate,userID)。

我立刻将它添加到firebase中,所以我看不到它只添加全名字段的方式而且它没有添加UserSettings。

请帮帮我!这是我发布的第一个应用程序。

0 个答案:

没有答案