2017-07-15 71 views
0

在下面的代碼中,我試圖打開一個登錄頁面。用戶將填寫電子郵件和密碼。當用戶點擊登錄按鈕時,調用checkLogin方法。無法成功完成身份驗證

據我所知,在onComplete方法中,第一次if檢查成功(填寫電子郵件,密碼和點擊登錄)未被調用。我總是得到Toast消息「Error login」(else塊)。

package com.awani.pocketblog; 

import android.app.ProgressDialog; 
import android.content.Intent; 
import android.support.annotation.NonNull; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.text.TextUtils; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.Toast; 

import com.google.android.gms.tasks.OnCompleteListener; 
import com.google.android.gms.tasks.Task; 
import com.google.firebase.auth.AuthResult; 
import com.google.firebase.auth.FirebaseAuth; 
import com.google.firebase.database.DataSnapshot; 
import com.google.firebase.database.DatabaseError; 
import com.google.firebase.database.DatabaseReference; 
import com.google.firebase.database.FirebaseDatabase; 
import com.google.firebase.database.ValueEventListener; 

public class LoginActivity extends AppCompatActivity { 

    private EditText mLoginEmailField; 
    private EditText mLoginPasswordField; 
    private Button mLoginButton; 
    private Button mNewAccountButton; 
    private FirebaseAuth mAuth; 
    private ProgressDialog mProgress; 

    private DatabaseReference mDatabaseUsers; 

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

     mAuth = FirebaseAuth.getInstance(); 
     mDatabaseUsers = FirebaseDatabase.getInstance().getReference().child("Users"); 
     mDatabaseUsers.keepSynced(true); 

     mLoginEmailField = (EditText) findViewById(R.id.loginEmailField); 
     mLoginPasswordField = (EditText) findViewById(R.id.loginPaswordField); 
     mLoginButton = (Button) findViewById(R.id.loginButton); 
     mNewAccountButton = (Button) findViewById(R.id.newAccountButton); 
     mProgress = new ProgressDialog(this); 

     mLoginButton.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 

       checkLogin(); 

      } 
     }); 
    } 

    private void checkLogin() { 

     //retrieve the data from database to check if user is logged in correctly 

     String email = mLoginEmailField.getText().toString().trim(); 
     String password = mLoginPasswordField.getText().toString().trim(); 

     if (!TextUtils.isEmpty(email) && !TextUtils.isEmpty(password)) { 
      mProgress.setMessage("Checking Login..."); 
      mProgress.show(); 

      mAuth.signInWithEmailAndPassword(email, password).addOnCompleteListener(new OnCompleteListener <AuthResult>() { 
       @Override 
       public void onComplete(@NonNull Task <AuthResult> task) { 
        //the following if block is never executed....WHY? 
        if (task.isSuccessful()) { 

         // Toast.makeText(LoginActivity.this,"hi",Toast.LENGTH_LONG).show(); 
         checkUserExist(); 

        } else { 
         mProgress.dismiss(); 
         Toast.makeText(LoginActivity.this, "Error Login", Toast.LENGTH_LONG).show(); 
        } 
       } 
      }); 

     } 

    } 

    private void checkUserExist() { 

     //retrieving UID 
     final String user_id = mAuth.getCurrentUser().getUid(); 
     //check if the user with thi UID already exists 
     mDatabaseUsers.addValueEventListener(new ValueEventListener() { 
      @Override 
      public void onDataChange(DataSnapshot dataSnapshot) { 

       if (dataSnapshot.hasChild(user_id)) { 

        Intent mainIntent = new Intent(LoginActivity.this, MainActivity.class); 
        mainIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
        startActivity(mainIntent); 

       } else { 
        Intent setUpIntent = new Intent(LoginActivity.this, SetUpActivity.class); 
        setUpIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
        startActivity(setUpIntent); 

       } 

      } 

      @Override 
      public void onCancelled(DatabaseError databaseError) { 

      } 
     }); 
    } 
} 
+0

添加'task.getException()的getMessage()'到烤麪包。登錄失敗。此答案中列出了可能的失敗原因:https://stackoverflow.com/a/39651068/4815718 –

+0

@BobSnyder我在代碼行Toast.makeText上添加了以上建議的代碼行(LoginActivity.this,「Error Login」,Toast .LENGTH_LONG).show();但它沒有改變任何東西:( –

+0

什麼是失敗消息文本在吐司? –

回答

0

對於電子郵件/密碼認證,用戶必須先與createUserWithEmailAndPassword()創建:

試圖創建與給定的電子郵件地址和密碼 一個新的用戶帳戶。如果成功的話,它也在簽署用戶到應用程序

設置在guide for password-based authentication的步驟4該實施例:

mAuth.createUserWithEmailAndPassword(email, password) 
     .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, "createUserWithEmail:success"); 
        FirebaseUser user = mAuth.getCurrentUser(); 
        updateUI(user); 
       } else { 
        // If sign in fails, display a message to the user. 
        Log.w(TAG, "createUserWithEmail:failure", task.getException()); 
        Toast.makeText(EmailPasswordActivity.this, "Authentication failed.", 
          Toast.LENGTH_SHORT).show(); 
        updateUI(null); 
       } 

       // ... 
      } 
     }); 
+0

謝謝,Bob!雖然困惑:getCurrentUser()檢查已存在於auth庫中的用戶,或者它只是考慮當前正在輸入信息的用戶? –

+0

@RamK:'getCurrentUser()'返回當前登錄的用戶,如果沒有,則返回null –

0

轉到您的火力控制檯中,啓用登錄方法:Email/PasswordAnonymous

enter image description here

如果它不能正常工作,請修改密碼,也許是太短了。