This is the first in a series of articles about redirecting users when they log in. Often, you have a good idea where a user wants to go when he or she first logs in to the site. In MODX Revolution, it’s very easy to send them there. In this article we’ll see the simplest way to do it if every user should go to the same place.
Redirecting All Users to the Same Page
You don’t need anything fancy to redirect a user if all users should go to the same page. You can just set the
&loginResourceId property of the snippet tag with the ID of the resource you want to send the users to:
The code above will send all users who successfully log in to the Resource with the ID of 12.
Redirecting All Users Back to the Page They Came From
If you have a system that uses a snippet or plugin to protect pages by forwarding users to the Login page if they are not logged in, it’s logical to send them back to the page they were trying to access after they log in successfully. This is just as easy as sending them all to the same place. You just need to set the
With the code above, every user will be sent back to the page they came from once they’ve logged in. This assumes that they came from a page at your site and the server has set the referer properly.
More Complicated Redirection
What if you want to send different users to different landing pages based on personal characteristics. group membership, or some other criterion? You might think that you could put a tag inside the login tag, like this:
The custom snippet would look up the user and set the target page appropriately. You’ve probably already realized why this won’t work. Until the user logs in, MODX has no way of knowing who he or she is. The only way this technique could work is if the information that determines where the user goes has nothing to do with who the user is. For example, you could use this technique if you want to send users to one landing page during business hours and another landing page when the business is closed. You could also use it to set the landing page based on the season.
We’ll see how to do those two operations in the next article. In a later article, we’ll see how to redirect the users based on specific user characteristics.