如果使用Firebase简单用户名和密码身份验证,如何返回用户列表不确定我是否做错了什么,但是使用这个apihttps:/www.Firebase.com/docs/security/简易-登录-电子邮件-password.html我可以根据返回消息成功地创建一个用户,但是在Forge控制台的任何地方都看不到该用户。您如何知道哪些用户已注册?我应该使用返回的用户ID并在Firebase中创建自己的用户对象,还是没有必要重复。我确实需要添加一些额外的用户属性,所以可能无论如何我都需要这样做。
3 回答
拉莫斯之舞
TA贡献1820条经验 获得超10个赞
id
email
auth
三国纷争
TA贡献1804条经验 获得超7个赞
SignInActivity.java
public class SignInActivity extends BaseActivity implements View.OnClickListener,View.OnKeyListener{ private DatabaseReference mDatabase; public static FirebaseAuth mAuth; private static final String TAG = "MainActivity"; EditText usernameField; EditText passwordField; TextView changeSignUpModeTextView; Button signUpButton; ImageView logo; RelativeLayout relativeLayout; Boolean signUpModeActive; static ArrayList<String> userList = new ArrayList<>(); @Override public void onStart() { super.onStart(); // Check auth on Activity start if (mAuth.getCurrentUser() != null) { onAuthSuccess(mAuth.getCurrentUser()); } } @Override public boolean onKey(View view, int i, KeyEvent keyEvent) { if(i == keyEvent.KEYCODE_ENTER && keyEvent.getAction() == keyEvent.ACTION_DOWN){ signUpOrLogIn(view); } return false; } @Override public void onClick(View view) { if(view.getId() == R.id.changeSignUpMode){ if (signUpModeActive == true){ signUpModeActive = false; changeSignUpModeTextView.setText("Sign Up"); signUpButton.setText("Log In"); }else{ signUpModeActive = true; changeSignUpModeTextView.setText("Log In"); signUpButton.setText("Sign Up"); } }else if(view.getId() == R.id.logo || view.getId() == R.id.relativeLayout){ InputMethodManager inm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE); inm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(),0); } } public void signUpOrLogIn(View view) { showProgressDialog(); String email = usernameField.getText().toString().trim(); String password = passwordField.getText().toString().trim(); if (signUpModeActive == true) { mAuth.createUserWithEmailAndPassword(email,password) .addOnCompleteListener(MainActivity.this, new OnCompleteListener<AuthResult>() { @Override public void onComplete(@NonNull Task<AuthResult> task) { hideProgressDialog(); Toast.makeText(MainActivity.this, "createUserWithEmail:onComplete:" + task.isSuccessful(), Toast.LENGTH_SHORT).show(); // If sign in fails, display a message to the user. If sign in succeeds // the auth state listener will be notified and logic to handle the // signed in user can be handled in the listener. if (!task.isSuccessful()) { Toast.makeText(MainActivity.this, "Authentication failed." + task.getException().toString().substring(task.getException().toString().indexOf(" ")), Toast.LENGTH_SHORT).show(); Log.i("Error", task.getException().toString()); } else { onAuthSuccess(task.getResult().getUser()); showUserList(); } } }); } else { mAuth.signInWithEmailAndPassword(email,password) .addOnCompleteListener(MainActivity.this, new OnCompleteListener<AuthResult>() { @Override public void onComplete(@NonNull Task<AuthResult> task) { hideProgressDialog(); // If sign in fails, display a message to the user. If sign in succeeds // the auth state listener will be notified and logic to handle the // signed in user can be handled in the listener. if (!task.isSuccessful()) { // there was an error Toast.makeText(MainActivity.this, task.getException().toString(), Toast.LENGTH_LONG).show(); } else { onAuthSuccess(task.getResult().getUser()); showUserList(); } } }); } } public void showUserList(){ startActivity(new Intent(getApplicationContext(), UserList.class)); finish(); } private void onAuthSuccess(FirebaseUser user) { String username = usernameFromEmail(user.getEmail()); // Write new user writeNewUser(user.getUid(), username, user.getEmail()); // Go to MainActivity } private String usernameFromEmail(String email) { if (email.contains("@")) { return email.split("@")[0]; } else { return email; } } private void writeNewUser(String userId, String name, String email) { User user = new User(name, email); mDatabase.child("users").child(userId).setValue(user); ArrayList<String> userNames = new ArrayList<>(); userNames.add(name); mDatabase.child("usernamelist").setValue(userNames); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mAuth = FirebaseAuth.getInstance(); mDatabase = FirebaseDatabase.getInstance().getReference(); if(mAuth.getCurrentUser()!=null){ showUserList(); } usernameField = (EditText) findViewById(R.id.username); passwordField = (EditText) findViewById(R.id.password); changeSignUpModeTextView = (TextView) findViewById(R.id.changeSignUpMode); signUpButton = (Button) findViewById(R.id.signupbutton); logo = (ImageView)findViewById(R.id.logo); relativeLayout= (RelativeLayout)findViewById(R.id.relativeLayout); signUpModeActive = true; changeSignUpModeTextView.setOnClickListener(this); usernameField.setOnKeyListener(this); passwordField.setOnKeyListener(this); logo.setOnClickListener(this); relativeLayout.setOnClickListener(this); } }
UserList.java
public class UserList extends AppCompatActivity { private static final String TAG = "UserList" ; private DatabaseReference userlistReference; private ValueEventListener mUserListListener; ArrayList<String> usernamelist = new ArrayList<>(); ArrayAdapter arrayAdapter;; ListView userListView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_user_list); userlistReference = FirebaseDatabase.getInstance().getReference().child("usernamelist"); onStart(); userListView = (ListView) findViewById(R.id.userlistview); } @Override protected void onStart() { super.onStart(); final ValueEventListener userListener = new ValueEventListener() { @Override public void onDataChange(DataSnapshot dataSnapshot) { usernamelist = new ArrayList<>((ArrayList) dataSnapshot.getValue()); usernamelist.remove(usernameOfCurrentUser()); Log.i(TAG, "onDataChange: "+usernamelist.toString()); arrayAdapter = new ArrayAdapter(UserList.this,android.R.layout.simple_list_item_1,usernamelist); userListView.setAdapter(arrayAdapter); } @Override public void onCancelled(DatabaseError databaseError) { Log.w(TAG, "onCancelled: ",databaseError.toException()); Toast.makeText(UserList.this, "Failed to load User list.", Toast.LENGTH_SHORT).show(); } }; userlistReference.addValueEventListener(userListener); mUserListListener = userListener; } public String usernameOfCurrentUser() { String email = MainActivity.mAuth.getCurrentUser().getEmail(); if (email.contains("@")) { return email.split("@")[0]; } else { return email; } } @Override public void onStop() { super.onStop(); // Remove post value event listener if (mUserListListener != null) { userlistReference.removeEventListener(mUserListListener); } } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.menu_main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { switch(item.getItemId()) { case R.id.action_logout: FirebaseAuth.getInstance().signOut(); startActivity(new Intent(this, MainActivity.class)); finish(); return true; default: return super.onOptionsItemSelected(item); } }
}
添加回答
举报
0/150
提交
取消