简体   繁体   中英

How to make simple user sign in with just username (firebase)

I am completely noob (again sorry about my ignorance in this field I relly need help) in java and I need to make an app with firebase. Here is my register account code(its all the code I have copied from GitHub :P) I want to make it just to register with username, the register with email and password functionality and the verification sending future completely to be disabled , also I need to make it to go on a different activity or simply to login the user and show the posts feed. I hope someone could help me:

public class SignupActivity extends 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);
    }
}

}

I do NOT advise the ability to sign up with ONLY a username. You lose the ability to recover an account.

However, you may take the username given to you, and append @fakeemail.com to the end of it and continue to use the email/password method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM