簡體   English   中英

Firebase數據庫和身份驗證中同一用戶的UID不同

[英]UID for same user is different in Firebase database and authentication

我需要檢索存儲在Firebase數據庫中的數據。 但是它表明,UID在身份驗證部分和數據庫部分是不同的。

更新 :現在我試圖從firebase中獲取數據時遇到問題。 我想在ListView中顯示它。 當我從登錄活動(成功登錄后)轉到常規活動(我將ListView放到那里)時,它沒有顯示,並且活動從常規切換為登錄一個。 我將代碼段附加在“ UPDATE CODE”下的底部。 幫助將不勝感激。 謝謝!

UIDS

這是注冊代碼

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_registration);
    setUpUIviews();

    firebaseAuth = FirebaseAuth.getInstance();

    db = FirebaseDatabase.getInstance().getReference().child("users");

    Register.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (validate()){
                String user_email = REmail.getText().toString().trim();
                String user_password = RPass.getText().toString().trim();

                firebaseAuth.createUserWithEmailAndPassword(user_email,user_password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
                    @Override
                    public void onComplete(@NonNull Task<AuthResult> task) {

                        if(task.isSuccessful()){
                            updateData();
                            Toast.makeText(Registration.this,"Registration Successful! ",Toast.LENGTH_SHORT).show();
                            startActivity(new Intent(Registration.this,MainActivity.class));
                        }else {
                            Toast.makeText(Registration.this,"Registration Unsuccessful ",Toast.LENGTH_SHORT).show();
                        }

                    }
                });
            }
        }
    });

這是更新數據部分:

private void updateData(){

    String uName = RName.getText().toString().trim();
    String uEmail = REmail.getText().toString().trim();

   String id = db.push().getKey();

   ProfileUpdate profileUpdate = new ProfileUpdate(uName,uEmail);

   db.child(id).setValue(profileUpdate);

   Toast.makeText(this,"Added info",Toast.LENGTH_SHORT).show();

}

更新碼

Login Activity

    public class LoginPage extends AppCompatActivity {

    private TextView userRegistration;
    private TextView Info;
    private EditText LName;
    private EditText LPass;
    private Button LSignIn;
    private int counter = 3;
    private FirebaseAuth firebaseAuth;
    private ProgressDialog progressDialog;

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

        userRegistration = (TextView)findViewById(R.id.tvRegister);
        LName = (EditText)findViewById(R.id.btnName);
        LPass = (EditText)findViewById(R.id.btnPass);
        LSignIn = (Button) findViewById(R.id.btnSignIn);
        Info = (TextView) findViewById(R.id.tvInfo);

         Info.setText( " No of attempts remaining: 3 " );

        firebaseAuth = FirebaseAuth.getInstance();
        progressDialog = new ProgressDialog(this);

        FirebaseUser user= null;

        if(user!=null){
           finish();
            startActivity(new Intent(LoginPage.this,General.class));
        }

         user = firebaseAuth.getCurrentUser();

        LSignIn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                validate(LName.getText().toString(),LPass.getText().toString());
            }
        });

        userRegistration.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(LoginPage.this,Registration.class));
            }
        });


    }

    private void validate(String username, String password){

        progressDialog.setMessage("Processing your request.... ");
        progressDialog.show();

        firebaseAuth.signInWithEmailAndPassword(username,password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
            @Override
            public void onComplete(@NonNull Task<AuthResult> task) {
                if(task.isSuccessful()){
                    progressDialog.dismiss();
                    Toast.makeText(LoginPage.this,"Login successful!",Toast.LENGTH_SHORT).show();
                    startActivity(new Intent(LoginPage.this,General.class));
                }else {
                    Info.setText("No of attempts remaining: " + String.valueOf(counter-1));
                    Toast.makeText(LoginPage.this,"Please enter corrent credentials",Toast.LENGTH_SHORT).show();
                    counter --;
                    progressDialog.dismiss();
                    if(counter<=0){
                        LSignIn.setEnabled(false);
                    }
                }
            }
        });


    }


}


General Activity

    public class General extends AppCompatActivity {

    private FirebaseAuth firebaseAuth;
    private Button Logout;

    ListView l1;
    ArrayAdapter<String> adapter;
    DatabaseReference databaseReference;
    FirebaseUser user;
    List<String> itemlist;
    String uid;




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


        firebaseAuth = FirebaseAuth.getInstance();
        Logout = (Button) findViewById(R.id.btnLogout);

        l1 = (ListView)findViewById(R.id.listView);
        user = FirebaseAuth.getInstance().getCurrentUser();
        uid = user.getUid();
        itemlist = new ArrayList<>();

        databaseReference = FirebaseDatabase.getInstance().getReference();

        databaseReference.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                itemlist.clear();;
                String user_name = dataSnapshot.child(uid).child("usrName").getValue(String.class);
                String user_email = dataSnapshot.child(uid).child("ursEmail").getValue(String.class);

                itemlist.add(user_name);
                itemlist.add(user_email);

                adapter = new ArrayAdapter<>(General.this,android.R.layout.simple_list_item_1,itemlist);
                l1.setAdapter(adapter);
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {

            }
        });


        Logout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                firebaseAuth.signOut();
                finish();
                startActivity(new Intent(General.this, MainActivity.class));
            }
        });


    }


}


activity_general.xml file

    <?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/myApp"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".General">

    <Button
        android:id="@+id/btnLogout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="8dp"
        android:text="LOGOUT"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.467"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/listView" />

    <ListView
        android:id="@+id/listView"
        android:layout_width="368dp"
        android:layout_height="270dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

該設計

在此處輸入圖片說明

這是將用戶數據存儲到datbase中的代碼:

String id = db.push().getKey();

ProfileUpdate profileUpdate = new ProfileUpdate(uName,uEmail);

db.child(id).setValue(profileUpdate);

在第一行中,您調用push().getKey()從數據庫SDK獲取新的唯一ID。 該ID與用戶個人資料無關。

如果要在用戶的UID下將用戶存儲在數據庫中,則需要在創建用戶后立即從AuthResult獲取UID。 就像是:

String uid = task.getResult().getUser().getUid();

當將此uid傳遞給updateData方法時,您將使用以下方法創建數據庫的子節點:

db.child(uid).setValue(profileUpdate);

暫無
暫無

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

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