繁体   English   中英

将Android Studio连接到Firebase进行身份验证

[英]Connecting Android Studio to Firebase for Authentication

我似乎无法使用以下代码通过注册人员连接到Firebase身份验证。 我按照Android Studio帮助上的说明进行了Firebase身份验证。 我也在Gradle中添加了这一行:compile'c​​om.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();
}

尝试使您的用户使用mAuth ,如果mAuth返回null,则在验证用户身份时出现问题

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);
                }

                // ...
            }
        });

确保您的gradle中有最新的编译器

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

确保您在firebase console启用了用于验证用户身份的选项

在此处输入图片说明

希望能帮助到你

编码愉快!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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