I have a login page, and right now after the user logs in I pass all of its data like the username
, password
and etc. I wanted to know if it's possible that I just pass like a whole user and it that user object I can just use getter and setters to get the user-related info. I am working on this project with Firebase so maybe perhaps I can check for current users logged in or something, I am not really sure what approach is the most suitable for this. Please let me know if there is any method like this.
How I currently pass data:
if (dataSnapshot.exists()) {
username.setError(null);
username.setErrorEnabled(false);
String passwordFromDB = dataSnapshot.child(userEnteredUsername).child("password").getValue(String.class);
if (passwordFromDB.equals(userEnteredPassword)) {
username.setError(null);
username.setErrorEnabled(false);
String userNameFromDB = dataSnapshot.child(userEnteredUsername).child("username").getValue(String.class);
String ageFromDB = dataSnapshot.child(userEnteredUsername).child("age").getValue(String.class);
String creditsFromDB = dataSnapshot.child(userEnteredUsername).child("credits").getValue(String.class);
String idFromDB = dataSnapshot.child(userEnteredUsername).child("id").getValue(String.class);
Intent intent = new Intent(getApplicationContext(),HomePage.class);
intent.putExtra("username",userNameFromDB);
intent.putExtra("password",passwordFromDB);
intent.putExtra("age",ageFromDB);
intent.putExtra("credits",creditsFromDB);
intent.putExtra("id",idFromDB);
Toast toast = Toast.makeText(getApplicationContext(),"Login Successful",Toast.LENGTH_SHORT);
toast.show();
startActivity(intent);
How my user looks like in Firebase
CodePudding user response:
Is there any way to just pass a "user" object instead of all the variables of the user?
For sure there is. Instead of adding all of those values, you can add the entire user object. But to achieve this, you have to create a model class that looks like this:
class User implements Serializable {
String username, password, age, credits, id;
public User(String username, String password, String age, String credits, String id) {
this.username = username;
this.password = password;
this.age = age;
this.credits = credits;
this.id = id;
}
@Override
public String toString() {
return "User{"
"username='" username '\''
", password='" password '\''
", age='" age '\''
", credits='" credits '\''
", id='" id '\''
'}';
}
}
Don't forget the implement the Serializable interface. Now to add the object to the intent, please create it like this:
String userNameFromDB = dataSnapshot.child(userEnteredUsername).child("username").getValue(String.class);
String passwordFromDB = dataSnapshot.child(userEnteredUsername).child("password").getValue(String.class);
String ageFromDB = dataSnapshot.child(userEnteredUsername).child("age").getValue(String.class);
String creditsFromDB = dataSnapshot.child(userEnteredUsername).child("credits").getValue(String.class);
String idFromDB = dataSnapshot.child(userEnteredUsername).child("id").getValue(String.class);
User user = new User(userNameFromDB, passwordFromDB, ageFromDB, creditsFromDB, idFromDB);
Intent intent = new Intent(getApplicationContext(),HomePage.class);
intent.putExtra("user", user);
And to get it back in the other activity, please use the following lines of code:
User user = (User) getIntent().getSerializableExtra("user");
Log.d("TAG", user);