簡體   English   中英

Firebase實時數據庫長值始終返回0,即使它不是

[英]Firebase Realtime Database long values always return 0 even though it is not

我一直試圖尋找解決方案很長一段時間,但無濟於事。 我顯然不是Firebase專家,我還在學習如何編碼。

我目前正在創建一個登錄系統,在Firebase提供的電子郵件驗證系統之上,還有另一層我自己手動完成的身份驗證。 因此,為實現這一目標,我將Firebase實時數據庫引入了我的項目。 這是數據庫中JSON文件的片段。

{
 "users" : {
  "abfkbnqeiurbnafjbnaojn" : {
   "app_authorized" : 1,
   "email" : "example51245@gmail.com",
   "full_name" : "Test Sample",
   "user_id" : "abfkbnqeiurbnafjbnaojn"
  }
 }
}

從JSON文件中可以看出,app_authorized鍵值對將決定用戶是否可以登錄(0表示用戶不符合條件,1表示用戶有資格登錄)。 我使用來自Firebase登錄的用戶UID,電子郵件和密碼系統用於所有鍵值對的父級。 我也知道app_authorized的值類型很長。

這是我在SignInActivity中使用的代碼。

long isAppAuthorized;

//some codes here

private void init() {

        btnSignIn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d(TAG, "onClick: attempting to log in");

                String email = emailEditText.getText().toString();
                String password = passwordEditText.getText().toString();

                //validates respective EditTexts
                if (TextUtils.isEmpty(email)) {

                    emailEditText.setError("Please fill in your e-mail address!");

                }
                else if (TextUtils.isEmpty(password)) {

                    passwordEditText.setError("Please fill in your password!");

                } else {

                    progressDialog.show();

                    mAuth.signInWithEmailAndPassword(email, password)
                            .addOnCompleteListener(SignInActivity.this, new OnCompleteListener<AuthResult>() {
                                @Override
                                public void onComplete(@NonNull Task<AuthResult> task) {
                                    Log.d(TAG, "signInWithEmail:onComplete:" + task.isSuccessful());

                                    FirebaseUser user = mAuth.getCurrentUser();
                                    firebaseDatabase = FirebaseDatabase.getInstance();

                                    userID = user.getUid();

                                    final DatabaseReference myRef = firebaseDatabase.getReference("users/" + userID + "/app_authorized");

                                    myRef.addValueEventListener(new ValueEventListener() {
                                        @Override
                                        public void onDataChange(DataSnapshot dataSnapshot) {
                                            //gets value of isAppAuthorized
                                            isAppAuthorized = dataSnapshot.getValue(long.class);
                                        }

                                        @Override
                                        public void onCancelled(DatabaseError databaseError) {

                                        }
                                    });

                                    // 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()) {

                                        Log.w(TAG, "signInWithEmail:failed", task.getException());
                                        Toast.makeText(SignInActivity.this, R.string.auth_failed,
                                                Toast.LENGTH_SHORT).show();
                                        progressDialog.dismiss();

                                    } else {

                                        try {
                                            //this is the part where I check for both isEmailVerified and isAppAuthorized
                                            if(user.isEmailVerified() && isAppAuthorized == 1) {

                                                Log.d(TAG, "onComplete: success. e-mail is verified " + user.isEmailVerified() + " userIsAuthorized: " + isAppAuthorized);
                                                Intent intent = new Intent(SignInActivity.this, HomeActivity.class);
                                                startActivity(intent);

                                            } else {

                                                Log.d(TAG, "onComplete: failed: isEmailVerified: " + user.isEmailVerified() + " userIsAuthorized: " + isAppAuthorized);

                                                progressDialog.dismiss();
                                                mAuth.signOut();
                                                Intent intent = new Intent(SignInActivity.this, MainActivity.class);
                                                startActivity(intent);

                                            }

                                        } catch(NullPointerException e) {

                                            Log.e(TAG, "onComplete: NullPointerException: " + e.getMessage());

                                        }
                                    }

                                    // ...
                                }
                            });
                }
            }
        });

    }

我遇到的問題是isAppAuthorized long總是返回0,無論我放入數據庫的值是多少,因此,使我的第二層授權無效。

我希望有人能夠幫助我,因為我嘗試了很多東西,但仍然無法理解問題的根源。

采用:

isAppAuthorized = dataSnapshot.getValue(Integer.class);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM