Wix routing and redirect according to specific condition.

Hi Wix Team,
I have a simple project with Home page and one dynamic page.

SCENARIO:-
The home page url is like “user.wixsite.com/mysite/”. The home page is of no use.
and the dynamic page url is like “user.wixsite.com/mysite/students/class1
the data “class1” in the above url is the dynamic value of the field “class” from the collection Students like below.

Student  Class  RoleID
-----------------------
Sam      class1     1
John     class2     2
Lui      class1     3

dynamic page url is like like below
user.wixsite.com/mysite/students/{class}

a student can be in any class from 1 to 12 like “class1, class2, class3 … class12”
My project is accessible to Teachers(Who register and login as a site member
Every Teacher is assigned to a class in Collection like below

Teacher                Class
-------------------------------
Teacher1@gmail.com     Class1
Teacher2@gmail.com     Class2

the page “user.wixsite.com/mysite/students/{class}” is bind to a dataset to list students of a specific class based on the url.
Its working fine for now.

The issue is that I want a functionality that when a teacher log in then

  1. It should automatically detect its assigned class (Which I can do by query database but I don’t know where to put this code, somewhere in routing or common page?)
  2. Load the url according to the logged in teacher assigned class, so that he/she can see all his class students only

for example if “Teacher1@gmail.com” logins, then my site automatically redirect to “user.wixsite.com/mysite/students/class1” and load students “Sam and Lui”.

The dataset is already bind to dynamic field class and loads data according to the url.
but when I log in,I land on home page and then I need to manually open the dynamic url.
also a teacher can open other class students also, which i don’t want.

NOTE:
I understand that its possible by routing but when I create a router the it adds a new router page. I want a routing code that is common for all the pages I open.
So in routing I can write logic to detect a Teacher class and redirect him/her to respective dynamic page.

Thanks for reading and please let me know if you don’t get anything from above points.

Hi Sudesh,

Did you use my name in your example so that I’d answer your question? Well, if you did, it worked. I hope this answer will help you out.

Yes, you can do this with routers, but I think there is an easier way (or two).

Using the setup you already have, when the user logs in (might be best to use the wix-users promptLogin( ) function and create your own login button) you query the teacher collection to get the right class and use the wix-location to( ) function to send the user to the right page. You still have the problem of teachers being able to see the other classes. You can also check in the onReady of the page to see if the person viewing the page is the teacher for that page, etc., but things are already starting to get complicated.

I suggest you have one page (it can be a dynamic page or a regular page with a dataset) whose dataset contains all the students. In the onReady( ) of the page you query to see the class of the teacher viewing the page and filter the dataset using setFilter( ) to only show the students for that teacher. (Note: you should check that the dataset is ready before filtering using the dataset.onReady( ) function.)