Specific menu for a specific user


#1

Hello guys, for my project, I’m using ionic 2 with firebase for database. I create two tables which contains two kinds of user. What I want, redirect user towards a specific menu (with pages dedicated to this menu) regarding which table user comes from. Eg: I have 2 tables, teacher and student, and if someone sign in into the app, he is redirected towards the appropriate menu. Teacher menu has a specific pages. Same case for students. None of them can see or navigate to other menu. The problem is I have no clue how to deal with that. I have to find appropriate condition to determine which table user comes from and display menu joined to this condition. I’m opened to all ideas, examples, links or tutorials. Thanks


#2

You probably should have 1 table “users” with a flag that decides if a user is “teacher” or “student”. Then you can branch the code to adapt depending on this value.


#3

Ooow ok…great idea! I didn’t see things in that way. You mean each user should be have a variable ‘teacher’ or ‘student’ and use it to make my decision? By the way, how to set root a menu?


#4

Something like that, or a flag ‘type’ that can be ‘teacher’ or ‘student’. Makes for simpler code than two different fields (except a user can actuall be teacher and student at the same time…).


#5

The way I would do this is to have a service that holds the notion of “current user” and exposes this as a ReplaySubject<User>(1). Then in the app component, you can simply do this:

userService.activeUser.subscribe((user) => {
  if (user) {
    if (user.role === 'teacher') {
      this.rootPage = TeacherRoot;
    } else {
      this.rootPage = StudentRoot;
    }
  } else {
    this.rootPage = LoginPage;
  }
});

The login page can inject this service and call userService.activeUser.next() with a user object, and the logout feature can do the same and call it with null.