首页 > 解决方案 > 如何仅使用用户名(firebase)进行简单用户登录

问题描述

我在java中完全是菜鸟(再次对我在这个领域的无知感到抱歉,我需要帮助),我需要用firebase制作一个应用程序。这是我的注册帐户代码(它是我从 GitHub 复制的所有代码:P)我只想使用用户名注册,使用电子邮件和密码功能注册以及完全禁用验证发送未来,我也需要使其继续进行不同的活动或只是登录用户并显示帖子提要。我希望有人可以帮助我:

公共类 SignupActivity 扩展 BaseActivity {

private static final String TAG = "SignupActivity";

private Context mContext = SignupActivity.this;

//firebase
private FirebaseAuth mAuth;
private FirebaseAuth.AuthStateListener mAuthListener;
private FireBaseMethods fireBaseMethods;


private String email, handleName, password;
private EditText mHandleName, mEmail, mPassword;
private Button mButtonRegister;
private TextView loadingPleaseWait;
private ProgressBar mProgressBar;

//firebase Database
private FirebaseDatabase mFirebaseDatabase;
private DatabaseReference myRef;
boolean isExisted;

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

    fireBaseMethods = new FireBaseMethods(mContext);

    Log.d(TAG, "onCreate: started");

    initWidgets();

    setupFirebaseAuth();

    init();

}

@Override
protected void performOnCreate(Bundle state) {

}

private void init() {

    mButtonRegister.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            handleName = mHandleName.getText().toString();
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.append(mEmail.getText().toString());
            stringBuilder.append("");
            email = stringBuilder.toString();
            password = mPassword.getText().toString();

            if (validate()) {
                mProgressBar.setVisibility(View.VISIBLE);
                loadingPleaseWait.setVisibility(View.VISIBLE);

                fireBaseMethods.registerNewEmail(handleName, email, password);


            }
        }
    });
}

/*
Initialize the activity widgets
 */
private void initWidgets() {
    Log.d(TAG, "initWidgets: Initialize Widgets");

    mHandleName = findViewById(R.id.handle_name);

    mEmail = findViewById(R.id.input_email_signup);

    mPassword = findViewById(R.id.input_password_signup);

    mButtonRegister = findViewById(R.id.btn_signup);

    mProgressBar = findViewById(R.id.progressBar);

    loadingPleaseWait = findViewById(R.id.loading_signup);

    mProgressBar.setVisibility(View.GONE);

    loadingPleaseWait.setVisibility(View.GONE);
}

public boolean validate() {
    boolean valid = true;

    if (handleName.isEmpty() || handleName.length() < 3) {
        mHandleName.setError("Внесете најмалку 3 карактери");
        valid = false;
    } else {
        mHandleName.setError(null);
    }

    if (email.isEmpty()) {
        mEmail.setError("Внесете валидна електронска пошта");
        valid = false;
    } else {
        mEmail.setError(null);
    }

    if (password.isEmpty() || password.length() < 4) {
        mPassword.setError("помеѓу 4 и 10 карактери");
        valid = false;
    } else {
        mPassword.setError(null);
    }

    return valid;
}

/*
------------------------------------- Firebase ---------------------------------------------------
 */


/**
 * Set up firebase auth object
 */
private void setupFirebaseAuth() {

    Log.d(TAG, "setupFirebaseAuth: setting up firebase auth");

    mAuth = FirebaseAuth.getInstance();

    mFirebaseDatabase = FirebaseDatabase.getInstance();

    myRef = mFirebaseDatabase.getReference();

    mAuthListener = new FirebaseAuth.AuthStateListener() {
        @Override
        public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
            FirebaseUser user = firebaseAuth.getCurrentUser();

            if (user != null) {
                // User is signed in
                Log.d(TAG, "onAuthStateChanged:signed_in:" + user.getUid());

                myRef.addListenerForSingleValueEvent(new ValueEventListener() {
                    @Override
                    public void onDataChange(DataSnapshot dataSnapshot) {

                        //1st check: make sure handle name is not ready in use

                        if (fireBaseMethods.checkIfHandleNameAlreadyExists(handleName, dataSnapshot)) {

                            mHandleName.setError("Тој ник веќе постои");

                            isExisted = true;

                        }

                        //add new user to the database
                        fireBaseMethods.addNewUser(handleName, email);

                        Toast.makeText(mContext, "Регистрирањето беше успешно.Ви пративме верификација на email", Toast.LENGTH_SHORT).show();

                        mAuth.signOut();


                    }

                    @Override
                    public void onCancelled(DatabaseError databaseError) {

                    }
                });

                finish();

            } else {
                // User is signed out
                Log.d(TAG, "onAuthStateChanged:signed_out");
            }
            // ...
        }
    };
}

@Override
public void onStart() {
    super.onStart();
    mAuth.addAuthStateListener(mAuthListener);
}

@Override
public void onStop() {
    super.onStop();
    if (mAuthListener != null) {
        mAuth.removeAuthStateListener(mAuthListener);
    }
}

}

标签: javaandroidfirebasefirebase-authentication

解决方案


我不建议仅使用用户名进行注册。您将失去恢复帐户的能力。

但是,您可以使用提供给您的用户名,并将其附加@fakeemail.com到其末尾并继续使用电子邮件/密码方法。


推荐阅读