You can develop a custom plugin binded to user events to trigger your custom code when the user is modified.
Here you can find a guide to create the plugin and the event to use:
Creating a Plugin for Joomla
User event: onUserBeforeSave
With this solution your code will be safe in future updates avoiding direct edits on Joomla code. There are also a lot of ...
This may no have been available back then, but now I use two ways of loading users in Joomla.
Method 1: Load any user as an instance of the User class
Loading a user this way will simply store it in the variable and you can then use it for other things. This does not affect the current session and cannot make you login as a user.
$foo = JFactory::...