问题
I am new to Android and Firebase and I am trying to build an app with a Login Interface in it. Once a user is logged in, the app would direct him/her to a specific screen depending on the users user-type. I will have 3 user-types: Admin
, Driver
and Bus dispatcher
.
Do you have any ideas how to get this working? I am creating a Capstone Project on which I really do need some help.
I did not code anything until now.
Looking forward to your ideas and suggestions, thanks.
回答1:
Have your users choose the type of user they want to be when they're signing up. Write that value to the object of that user in the Firebase database. For example:
$UniqueUID:
name: "John Doe"
userType: "Driver"
When the user logs in, in your MainActivity, check which type of user has logged in. For example:
if(userType.equals("Driver")){
//Load the Driver screen
}
Now obviously the above example is pretty rough. You'll need to set up a listener to the child that has the particular user's data and retrieve the DataSnapshot from it. Then you can compare it and execute the necessary code.
回答2:
Assuming you have a firebase structure similar to this:
root
- users
- -userUID
- --attributes...
- --type
which means you would have an appropriate User.java model which reflects those attributes from firebase.
Then you would need to Create the user in firebase, which is pretty straight forward.
Once your user is in the firebase, you can use something like this to launch the appropriate activity (most of this is directly from the firebase docs) (https://firebase.google.com/docs/auth/android/google-signin)
public static final int ADMIN = 101;
public static final int DRIVER = 102;
public static final int BUS_DISPATCHER= 103;
@Override
protected void onCreate(Bundle savedInstanceState) {
// ...
mAuthListener = new FirebaseAuth.AuthStateListener() {
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
FirebaseUser firebaseUser = firebaseAuth.getCurrentUser();
if (user != null) { // User is signed in
Log.d(TAG, "onAuthStateChanged:signed_in:" + firebaseUser.getUid());
FirebaseDatabase.getInstance().getReference().child("users").child(firebaseUser.getUid()).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) {
User user = dataSnapshot.getValue(User.class);
int type = user.getType();
switch (type) {
case DRIVER:
launchDriverScreen();
break;
case ADMIN:
launchAdminScreen();
break;
case BUS_DISPATCHER():
launchBusDispatcherScreen();
break;
default:
Log.e(TAG, "unknown user type: " + type);
break;
}
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
} else { // User is signed out
Log.d(TAG, "onAuthStateChanged:signed_out");
}
}
};
}
@Override
public void onStart() {
super.onStart();
mAuth.addAuthStateListener(mAuthListener);
}
@Override
public void onStop() {
super.onStop();
if (mAuthListener != null) {
mAuth.removeAuthStateListener(mAuthListener);
}
}
来源:https://stackoverflow.com/questions/41550558/firebase-authentication-depending-on-custom-user-type