[英]Connecting Android Studio to Firebase for Authentication
我似乎無法使用以下代碼通過注冊人員連接到Firebase身份驗證。 我按照Android Studio幫助上的說明進行了Firebase身份驗證。 我也在Gradle中添加了這一行: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();
}
嘗試使您的用戶使用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.