简体   繁体   English

Firebase数据库和身份验证中同一用户的UID不同

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

I need to retrieve the data that i have stored in the Firebase database. 我需要检索存储在Firebase数据库中的数据。 But it shows that the UIDs are different in authentication part and database part. 但是它表明,UID在身份验证部分和数据库部分是不同的。

UPDATE : Now i am facing problems while trying to fetch data from the firebase. 更新 :现在我试图从firebase中获取数据时遇到问题。 I want to display it in a ListView. 我想在ListView中显示它。 When i go from the Login Activity(after successful login) to the General Activity (where i put the ListView), it does not show up and the Activity switches from General to the Login one. 当我从登录活动(成功登录后)转到常规活动(我将ListView放到那里)时,它没有显示,并且活动从常规切换为登录一个。 I am attaching the code segments at the bottom under "UPDATE CODE" . 我将代码段附加在“ UPDATE CODE”下的底部。 Help would be highly appreciated. 帮助将不胜感激。 Thanks! 谢谢!

UIDS

Here is the code for registration 这是注册代码

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

                    }
                });
            }
        }
    });

And this is the update data part: 这是更新数据部分:

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

}

UPDATE CODE 更新码

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>

The Design 该设计

enter image description here 在此处输入图片说明

This is the code of how you store the user data into the datbase: 这是将用户数据存储到datbase中的代码:

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

ProfileUpdate profileUpdate = new ProfileUpdate(uName,uEmail);

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

In the first line you call push().getKey() to get a new unique ID from the database SDK. 在第一行中,您调用push().getKey()从数据库SDK获取新的唯一ID。 This ID is not related to the user profile. 该ID与用户个人资料无关。

If you want to store the users in the database under their UID, you will need to get the UID from the AuthResult just after you created the user. 如果要在用户的UID下将用户存储在数据库中,则需要在创建用户后立即从AuthResult获取UID。 Something like: 就像是:

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

When you pass this uid to your updateData method, you then create the child node the database with: 当将此uid传递给updateData方法时,您将使用以下方法创建数据库的子节点:

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

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

相关问题 Firebase 实时数据库 UID 与身份验证 UID 不同 - Firebase Realtime Database UID different from Authentication UID Firebase 身份验证用户 UID 作为实时数据库中的子节点 - Firebase Authentication User UID as child node in Realtime Database 有没有办法从 Firebase 数据库身份验证和 Firebase 数据库 UID 中删除匿名用户? - Is there a way to delete anonymous user from Firebase Database Authentication and Firebase Database UID? 使用相同的uid将多个数据保存到Firebase数据库 - Save multiple data with same uid to Firebase database 如何使用Firebase电子邮件/密码身份验证获取UID并添加用户信息 - How to get UID and add user information with Firebase email/password authentication 如何为 Firebase 身份验证用户设置自定义用户 UID? - How to set custom User UID for Firebase Authentication users? 在 Groupie Recycler 视图中,它每 7 行重复一次 Firebase 数据库的相同 uid - On Groupie Recycler view every 7 rows it repeats the same uid of firebase database 从 firebase 数据库 flutter 中的 uid 获取用户数据 - get user data from uid in firebase database flutter Android Firebase从实时数据库获取其他用户的uid - Android Firebase get other user's uid from realtime database 如何在 Android 上的 Firebase 数据库中存储用户 Uid? - How to store user Uid in our Firebase Database on Android?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM