Find your content:

Search form

You are here

Sites login method using javascript remoting


I'm looking for a way to access the site.login() method using javascript remoting for a mobile/html5 site.

The method returns a pageReference in theory, but I'm only getting back a null object.

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?

What kind of pageReference does a "failed login" return?

Attribution to: Shane McLaughlin

Possible Suggestion/Solution #1

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

Attribution to: Barry

Possible Suggestion/Solution #2

Controller...remote method to call the normal site login method

global static string RemoteLogin(string email, string password, string url){
    PageReference result = new PageReference('temp');
    try {
        result = Site.login(email, password, url);
    }catch (exception e){
        return e.getMessage();
    if (result!=null) {
        return result.getURL(); 
    } else {
        return 'login error';

js for your page. The ga push link method is used to maintain google analytics tracking when we go from a custom domain to the domain (see this question) but you could use window.location or other redirect of your choice to get to the url that's returned. I'm parsing out all the & to just be &

function Login(){
      var pagename = '{!$CurrentPage.Name}';
      if (pagename == 'SiteLogin') pagename='homepage';
      ControllerName.RemoteLogin($j("#email").val(),$j("#password").val(), '/'+pagename, function(result2, event){
        if (result2!='login error'){
            var decoded = result2.replace('amp;', '').replace('amp;', '').replace('amp;', '').replace('amp;', '').replace('amp;', '').replace('amp;', '').replace('amp;', '').replace('amp;', '');                                    
            _gaq.push(['_link', decoded]);
        } else{


Attribution to: Shane McLaughlin

Possible Suggestion/Solution #3

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') {
            } 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="{!$}" 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="{!$}" 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}"> {!$}</apex:outputLink>
      <apex:outputText value=" | " rendered="{!$Site.RegistrationEnabled}" />
      <apex:outputLink value="{!$Page.SiteRegister}" rendered="{!$Site.RegistrationEnabled}">{!$}</apex:outputLink>

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);
    return result;

global static string remotelogin(string username, string password,string startUrL){
    //remote implementation
    //String startUrl = System.currentPageReference().getParameters().get('startURL'); //null reference 

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

    if(result == null){
        return 'NULL';
        return  result.geturl(); 

Attribution to: Samuel De Rycke
This content is remixed from stackoverflow or stackexchange. Please visit

My Block Status

My Block Content