Sites login method using javascript remoting

I'm not sure if I should post, as this only 50% of an answer. Well, that is, I assumed your question is, how do i login using Site.login() over javascript remote. Which i'm unable to do. But i got passed getting the null references, and receive the same url from the Site.login() method as when not using javascript remote, but it doesn't seem to let me login.

"What kind of pageReference does a "failed login" return?" => returns null, that's pretty easy to test.

"What do I do with that object in the javascript handler? How do I get the user to the startURL that it should be returning?" => as you probably noticed, System.currentPageReference().getParameters().get('startURL') was giving a null reference in a remote method, I dumped it in the viewstate, and got it from there.

Following code will return the same output from Site.login() that I get when i succesfully login through a non remote js flow. Not sure why it's not working with remote js, hopefully it sets you in the right direction.

edit: I'm not entirely sure how to post & align code here properly :s

<apex:component controller="SiteLoginController" id="loginComponent"> < s c r i p t>
function login(){
            var pw = document.getElementById('password').value;
            var usr= document.getElementById('username').value;
            var url = document.getElementById('startUrl').value;
            SiteLoginController.remotelogin(usr, pw,url,loginCallback);
}function loginCallback(result, event) {
            if(event.type == 'exception') {
                alert(event.message);
            } else {
                window.location = result;
            }}< / s c r i p t><apex:form id="loginForm" forceSSL="true">
<apex:outputPanel layout="block">
  <apex:pageMessages id="error"/>
  <apex:panelGrid columns="2" style="margin-top:1em;">

    <!--  <apex:inputText id="username" value="{!username}"/> -->
   <input type="text" id="username" />

    <apex:outputLabel value="{!$Label.site.password}" for="password"/>


  <!--   <apex:inputSecret id="password" value="{!password}"/>-->
  <input type="password" id="password" />


    <apex:outputText value=""/>

    <input type="hidden" id="startUrl" value="{!startUrl}" escape="false" style="display:none;"/>

   <!--  <apex:commandButton action="{!login}" value="{!$Label.site.login_button}" id="loginButton"/>-->
    <a  onclick="login()">LOG IN</a>

    <apex:outputText value=""/>
    <apex:outputText value=""/>
    <apex:outputText value=""/>
    <apex:panelGroup id="theLinks">
      <apex:outputLink value="{!$Page.ForgotPassword}"> {!$Label.site.forgot_your_password_q}</apex:outputLink>
      <apex:outputText value=" | " rendered="{!$Site.RegistrationEnabled}" />
      <apex:outputLink value="{!$Page.SiteRegister}" rendered="{!$Site.RegistrationEnabled}">{!$Label.site.new_user_q}</apex:outputLink>
    </apex:panelGroup>
  </apex:panelGrid> 
</apex:outputPanel>

global with sharing class SiteLoginController {

global String username {get; set;}
global String password {get; set;}
global string startUrl{get;set;}
global SiteLoginController(){
     startUrl = System.currentPageReference().getParameters().get('startURL');
}

global PageReference login() {
    //normal implementation
    startUrl = System.currentPageReference().getParameters().get('startURL');
    PageReference result =  Site.login(username, password, startUrl);
    system.debug(result);
    return result;
}


@RemoteAction
global static string remotelogin(string username, string password,string startUrL){
    //remote implementation
    system.debug('------------------>remotelogin()');
    //String startUrl = System.currentPageReference().getParameters().get('startURL'); //null reference 
    //system.debug('-------------------->'+startUrl);

    Pagereference result =  Site.login(username, password,startUrL);

    if(result == null){
        return 'NULL';
    }else{
        system.debug(result);
        system.debug(result.geturl());
        return  result.geturl(); 
    }
}}

Got it to work shane... handle it as a string not a pagereference and then decode the url return to remove the amp; dude