简体   繁体   中英

Connecting Android Studio to Firebase for Authentication

I can't seem to connect to firebase authentication with register people with the following code. I followed the instructions on the Android Studio help for firebase authentication. I added this line in the Gradle too: compile 'com.google.firebase:firebase-auth:10.0.1'

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

    mAuth = FirebaseAuth.getInstance();
    username = (EditText) (findViewById(R.id.username));
    password = (EditText) (findViewById(R.id.password));
    signIn = (Button) (findViewById(R.id.signIn));
    register = (Button) (findViewById(R.id.register));
}

private void registerUser()
{
    loginUsernameString = username.getText().toString();
    loginPasswordString = password.getText().toString();

    mAuth.createUserWithEmailAndPassword(loginUsernameString, loginPasswordString)
            .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                @Override
                public void onComplete(@NonNull Task<AuthResult> task) {
                    Log.d(TAG, "createUserWithEmail:onComplete:" + task.isSuccessful());

                    // If sign in fails, display a message to the user. If sign in succeeds
                    // the auth state listener will be notified and logic to handle the
                    // signed in user can be handled in the listener.
                    if (!task.isSuccessful()) {
                        Toast.makeText(LoginActivity.this, "Failed",
                                Toast.LENGTH_SHORT).show();
                    }

                    // ...
                }
            });
}
public void onClick(View view)
{
    if (view == signIn)
        registerUser();
}

Try to get your user with mAuth , if mAuth returns null there is a problem authenticating your user

mAuth.signInWithEmailAndPassword(loginUsernameString, loginPasswordString)
        .addOnCompleteListener(this, 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.d(TAG, "signInWithEmail:success");
                    FirebaseUser user = mAuth.getCurrentUser().getUid();
                    Log.d(userCreated, ""+user);
                } else {
                    // If sign in fails, display a message to the user.
                    Log.w(TAG, "signInWithEmail:failure", task.getException());
                    Toast.makeText(EmailPasswordActivity.this, "Authentication failed.",
                            Toast.LENGTH_SHORT).show();
                    updateUI(null);
                }

                // ...
            }
        });

make sure tu have the latest compile in your gradle which is this at the moment

compile 'com.google.firebase:firebase-auth:11.2.0'

make sure you enable in your firebase console the option for authenticating your users

在此处输入图片说明

Hope it helps

Happy coding !

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