Android Firebase:用户重新登录时,用户特定的实时数据将被删除 [英] Android Firebase: User-specific real-time data is removed when user logs back in

查看:302
本文介绍了Android Firebase:用户重新登录时,用户特定的实时数据将被删除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Firebase Google身份验证进行登录,并且有一个与用户关联的数据点列表。用户通过在登录的会话中创建元素来填充他们的数据,并且这些会更新Firebase中的列表,这些列表存储在他们的uid下的RTDB的用户引用中。



要点:$ b​​
$ b


  1. 注销时,数据仍然存在。 / li>
  2. 当我重新登录时,在RTDB中的uid是一样的

  3. 当我重新登录时,用户特定的列表被删除。 / li>

如何使数据在RTDB中保持不变?

UserDataListActivity

  @Override 
public void onClick(View view){
switch view.getId()){
case R.id.signout_button:
FirebaseAuth.getInstance()。signOut();
Log.i(TAG,用户涉嫌注销);
Intent backToLogin = new Intent(UserDataListActivity.this,LoginActivity.class);
startActivity(backToLogin);
finish();
break;




LoginActivity



编辑:我的印象是写功能不会覆盖现有的数据。如何在Google登录时向用户添加引用特定用户的数据,而不覆盖他们已有的任何信息?

  private void onAuthSuccess (FirebaseUser用户){
字符串用户名= usernameFromEmail(user.getEmail());
String [] names = firstAndLastNameFromDisplayName(user.getDisplayName());
$ b $ //写新用户
writeNewUser(user.getUid(),names,username,user.getEmail(),user.getUid());

//转到MainActivity
Intent startMainActivity = new Intent(LoginActivity.this,MainActivity.class);
startActivity(startMainActivity);
finish();
}
private String usernameFromEmail(String email){
if(email.contains(@)){
return email.split(@)[0];
} else {
return email;

$ b private String [] firstAndLastNameFromDisplayName(String fullName){
if(fullName!= null){
if(fullName.contains()) {
return new String [] {fullName.split()[0],fullName.split()[1]};
} else {
return new String [] {fullName,emptyLastName};
}
} else {
return new String [] {defaultfirst,defaultlast};

$ b private void writeNewUser(String userId,String [] names,String username,String email,String uid){
User user = new User(username,names [ 0],名称[1],电子邮件,uid);
myUsers.child(userId).setValue(user);


private boolean isEmailValid(String email){
return email.contains(@);
}

private boolean isPasswordValid(String password){
return(password.length()> 4)&& !password.contains( \\);
}

解决方案

要检查用户是否在Firebase实时数据库中存在,然后在用户登录/ Android注册时添加一个用户:

  private void writeNewUser(final String userId,String [] names,String username,String email,String uid){$ b $ final User user = new User用户名,名字[0],名字[1],电子邮件,uid); 
myUsers.addListenerForSingleValueEvent(new ValueEventListener(){
@Override $ b $ public void onDataChange(DataSnapshot dataSnapshot){
if(!dataSnapshot.hasChild(userId)){
myUsers.child(userId).setValue(user);
}
}
@Override $ b $ public void onCancelled(DatabaseError databaseError){}
});




$ b我从这里和@囚徒的答案的指导。


I am using Firebase Google authentication for login, and I had a list of data points associated with a user. The user populates their data by creating elements in a logged-in session, and these update a list in Firebase that is stored under their uid in a 'user' reference of the RTDB.

Key points:

  1. When I log out, the data persists.
  2. When I log back in, the uid is the same in the RTDB
  3. When I log back in, the user-specific list is deleted.

How can I make the data persist in the RTDB?

UserDataListActivity

@Override
public void onClick(View view) {
    switch (view.getId()) {
        case R.id.signout_button:
            FirebaseAuth.getInstance().signOut();
            Log.i(TAG, "User allegedly logged out.");
            Intent backToLogin = new Intent(UserDataListActivity.this, LoginActivity.class);
            startActivity(backToLogin);
            finish();
            break;
    }
}

LoginActivity

EDIT: I was under the impression that the write function would not overwrite the existing data. How can I add to the users reference a specific user's data upon Google login without overwriting whatever information they already have?

private void onAuthSuccess(FirebaseUser user) {
    String username = usernameFromEmail(user.getEmail());
    String[] names = firstAndLastNameFromDisplayName(user.getDisplayName());

    // Write new user
    writeNewUser(user.getUid(), names, username, user.getEmail(), user.getUid());

    // Go to MainActivity
    Intent startMainActivity = new Intent(LoginActivity.this, MainActivity.class);
    startActivity(startMainActivity);
    finish();
}
private String usernameFromEmail(String email) {
    if (email.contains("@")) {
        return email.split("@")[0];
    } else {
        return email;
    }
}
private String[] firstAndLastNameFromDisplayName(String fullName) {
    if (fullName != null) {
        if (fullName.contains(" ")) {
            return new String[]{fullName.split(" ")[0], fullName.split(" ")[1]};
        } else {
            return new String[]{fullName, "emptyLastName"};
        }
    } else {
        return new String[]{"defaultfirst","defaultlast"};
    }
}
private void writeNewUser(String userId, String[] names, String username, String email,String uid) {
    User user = new User(username,names[0],names[1], email, uid);
    myUsers.child(userId).setValue(user);
}

private boolean isEmailValid(String email) {
    return email.contains("@");
}

private boolean isPasswordValid(String password) {
    return (password.length() > 4) && !password.contains("\\");
}

解决方案

To check if a user exists in Firebase Realtime Database before adding one upon user login/registration in Android:

private void writeNewUser(final String userId, String[] names, String username, String email,String uid) {
    final User user = new User(username,names[0],names[1], email, uid);
    myUsers.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            if (!dataSnapshot.hasChild(userId)) {
                myUsers.child(userId).setValue(user);
            }
        }
        @Override
        public void onCancelled(DatabaseError databaseError) {}
    });

}

I got the tip from here and the guidance of @Prisoner's answer.

这篇关于Android Firebase:用户重新登录时,用户特定的实时数据将被删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆