function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
David Benkoel 3David Benkoel 3 

Site.login() does not work in Developer Console

Hi,
In the Developer Console I'm executing this simple snippet:
PageReference page = Site.login(username, password, url);
System.debug(page);
Despite I double checked that an active user exist with the username and password I'm passing to Site.login(), this method always returns null.

This basically means that the login process failed.

Is there a reason for that? I can't get the point.

Thank you
Sai PraveenSai Praveen (Salesforce Developers) 
Hi David,

Site.login() is designed to be used from a force.com Site, from a controller on a Site page. 
You would generally create a controller, link to a public page, and somewhere in that controller have

Pagereference p = Site.login(un, pw [, startUrl] );
 
This should then perform the login action.
 
The startUrl should not have the un and pw inside, and generally I'd leave this out completely.
 
Also once the login action is performed, nothing else on that controller will execute, as the context then gets transferred to the newly logged-in user.
 
When you create a new force.com Site, salesforce will create this login controller automatically as an example for you, so you should see how it works in a bit more detail if you create a new force.com Site.

Let me know if you face any issues.

If this solution helps, Please mark it as best answer.

Thanks,
David Benkoel 3David Benkoel 3
Hi Sai, 

You're right and that's indeed what I did meanwhile.
I created a POC with this kind of design, and it works like a charm.

By running the same POC on two instance, a regular sandbox that I use for development, and a pre-prod sandbox as well, I figured out that the Site.login() method does not work on my regular sandbox.

It seems really weird to me and I'm going to open a Support Case for that.

Maybe you're aware of something that could explain this issue?