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
@RemoteAction
global static string RemoteLogin(string email, string password, string url){
PageReference result = new PageReference('temp');
try {
result = Site.login(email, password, url);
system.debug(result);
}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 secure.force.com 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{
$j("#login_submit").button('reset');
$j("#login_error").show();
}
});
}
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') {
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();
}
}}
Attribution to: Samuel De Rycke
This content is remixed from stackoverflow or stackexchange. Please visit https://salesforce.stackexchange.com/questions/195