That sounds like exactly what the code already does as standard - when a Moodle page calls 'require_login' and the user is not already logged in, then the code saves the current URL (as $SESSION->wantsurl) and redirects to the login page. After login, the saved URL is returned to.
General developer forum
after login need redirect to previous url
This is good information. I am using the Joomdle bridge to connect my moodle and joomla user database and login for a Single Sign On. When a user logs in or creates an account, they do so on the joomla side and the bridge logs them into both sites. However, I want the user to be redirected to the url that moodle stored in $SESSION->wantsurl. Problem is I don't know how to allow the joomla login page(an external site from moodle) to use this variable so I can redirect to its location. Any ideas how to allow external sites to access the "$SESSION->wantsurl"? Thanks.