Firebase的
这是我存储所需用户信息的第一次尝试:
createUser()
方法需要email和password字段,但如果我还想让用户像Snapchat、Instagram、StackOverflow等应用一样拥有自定义用户名怎么办?是否有办法修改现有方法以接受该字段,还是我需要手动推送和管理这些信息?这是我存储所需用户信息的第一次尝试:
Firebase ref = new Firebase(firebaseURL);
ref.createUser(email, password, new Firebase.ValueResultHandler<Map<String, Object>>() {
@Override
public void onSuccess(Map<String, Object> result) {
System.out.println("Successfully created user account with uid: " + result.get("uid"));
//Sign user in
Firebase ref = new Firebase(firebaseURL);
ref.authWithPassword(email, password, new Firebase.AuthResultHandler() {
@Override
public void onAuthenticated(AuthData authData) {
System.out.println("User ID: " + authData.getUid() + ", Provider: " + authData.getProvider());
//Save user info
Firebase userRef = new Firebase(firebaseURL + "Users/");
User user = new User(username, authData.getUid());
userRef.setValue(user);
这样做好吗?我想将UID与用户名一起存储,以便在将来处理更改时有所帮助。此外,如果这是一个社交媒体应用程序,我应该实现updateChildren()
或push()
方法以避免覆盖条目吗?
这是我的第二次尝试:
@Override
public void onAuthenticated(AuthData authData) {
System.out.println("User ID: " + authData.getUid() + ", Provider: " + authData.getProvider());
//Save user info and username
Firebase ref = new Firebase(firebaseURL);
Map<String, String> map = new HashMap<String, String>();
map.put("email", email);
map.put("username", username);
map.put("provider", authData.getProvider());
ref.child("users").child(authData.getUid()).setValue(map);