Tested in Joomla 3.2
The two scripts I posted in the question did only half of what I wanted. I.e. They only checked if the login credentials were valid, returned a message and did nothing else.
What I have done is modified hocoi's script to log me in as well. NOTE: This happens after you've authenticated and made sure the user has given the right details. Then, to login the user, just use the JFactory::getApplication()->login($credentials)
method like so:
$credentials = array();
$credentials['username'] = $username;
$credentials['password'] = $password1;
JFactory::getApplication()->login($credentials);
I also found this piece of code that adds the necessary header cookies once you've logged. This will be useful if you're planning to maintain the login.
if(!isset($_COOKIE['jsid'])){
$user = & JFactory::getUser();
$temp_session = $_SESSION; // backup all session data
session_write_close();
ini_set("session.save_handler","files"); // set session saved hadler on file
session_start();
$_SESSION = (array)$temp_session['__default']['user']; // data that another php file need to know
session_write_close();
ini_set("session.save_handler","user"); // put back session saved handler on database
$jd = new JSessionStorageDatabase();
$jd->register(); // set required parameters
session_start(); // restart //
$_SESSION = $temp_session; // restore last session data
$e = session_id();
setcookie("jsid", $e, time()+3600,'/');
}
As @Valentin's answer, you probably want to create a basic API for this. Remote login like this should really be a last resort. I'm using it as an entry point for my local app to get connected via an API key.