1
我正在瞭解Firebase並嘗試在用戶系統中實施Firebase身份驗證和Firebase數據庫。 我知道如何在Facebook上使用Firebase身份驗證,但我想爲每個用戶添加一些屬性(在Firebase身份驗證中,我們只有電子郵件,uid,提供程序和個人資料圖片url)。檢查Firebase數據庫中是否存在特定密鑰
我的解決方案是創建一個用戶註冊表:
問題
所以我想要做的只是做一個推僅在第一次在用戶登錄這是我的MainActivity:
public class MainActivity extends AppCompatActivity {
DatabaseReference myRef1;
DatabaseReference myRef;
private int RC_SIGN_IN = 1;
private String urlI, uid, name, uide;
boolean check_rate;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myRef1 = FirebaseDatabase.getInstance().getReference();
myRef = myRef1.child("Usuarios");
FirebaseAuth auth = FirebaseAuth.getInstance();
id();
if (auth.getCurrentUser() != null) {
uide = auth.getInstance().getCurrentUser().getUid().toString();
if (check_rate) {
startActivity(new Intent(this, HomeActivity.class));
finish();
} else {
Map mProfile = new HashMap();
mProfile.put("name", auth.getInstance().getCurrentUser().getDisplayName().toString());
mProfile.put("image", auth.getInstance().getCurrentUser().getPhotoUrl().toString());
mProfile.put("uid", auth.getInstance().getCurrentUser().getUid().toString());
myRef.push().setValue(mProfile);
startActivity(new Intent(this, HomeActivity.class));
finish();
}
} else {
// abre a tela de login
startActivityForResult(
AuthUI.getInstance()
.createSignInIntentBuilder()
.setIsSmartLockEnabled(false)
.setProviders(
AuthUI.FACEBOOK_PROVIDER)
.build(),
RC_SIGN_IN);
}
}
private void id() {
myRef.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
Map data = (Map) dataSnapshot.getValue();
//profile
urlI = (String) (data.get("image"));
name = (String) (data.get("name"));
uid = (String) (data.get("uid"));
check_rate = data.containsKey(uide);
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
Map data = (Map) dataSnapshot.getValue();
//profile
//Map mData = (HashMap) data.get("profile");
urlI = (String) (data.get("image"));
name = (String) (data.get("name"));
uid = (String) (data.get("uid"));
check_rate = data.containsKey(uide);
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
這是行不通的。用戶每次登錄時都會上傳。 請幫助
這工作很棒弗蘭克。非常感謝 –