My company Is still using a portal for their customer login, and I was recently tasked with creating a VF page to replace the bland default login page. I have most of the page set-up, but I cannot get the page to redirect back to itself on a failed login; instead, it goes to the default login page. I'm using the code I found in other question pages for determining if a login succeeded or failed, by checking if the pageReference returned by a login is null:

    //set reference and attempt login
    PageReference portalPage = new PageReference(startUrl);
    PageReference p = Site.login(username, password, startUrl);

    //if login==false return to custom login
    if (p == null) return Site.login(username, password, null);
    //else enter portal
    else return portalPage;

This code, however, always redirects to custom login pagen (p is always null). In experimentation, I simply returned portalPage, and it makes it so that it only redirects to the default login (or the portal itself on successful login. As such, I am convinced that the "p == null" bit is not successfully determining the login status, though it seems to work for everyone else. If anyone could offer some advice as to how I might go about fixing this, I would much appreciate it.

For reference, I used the exact same method I found here: https://developer.salesforce.com/forums/ForumsMain?id=906F000000096YkIAI

  • An Edit: I apologize, I was missing something that made it not quite the same as the other examples. I've fixed that problem, but now it tells me that the page reference is always null. Thanks for your help.
    – Cong Hui
    Oct 1, 2014 at 15:43

2 Answers 2


Please use rerender and apex:pagemessages component on the page you designed. So that we can know what's the exact issue is. Did you try checking debug logs.

  • The debug logs show nothing that seems related to the problem.
    – Cong Hui
    Oct 1, 2014 at 13:26
  • How about page message, did you give a try? Oct 1, 2014 at 13:30
  • What should I be displaying on the pageMessage? The page is currently setup to show missing username/password and bad login error messages.
    – Cong Hui
    Oct 1, 2014 at 13:40
  • Use <apex:pageMessages/> tag in Visualforce page. So that you can capture server side error messages. salesforce.com/us/developer/docs/pages/Content/… Oct 1, 2014 at 15:07
  • All it shows are the default erros "Enter a value in the User Name field", "Enter a value in the Password field." and "Your login attempt has failed. Make sure the username and password are correct." Also, attempt to show content of the PageReference sends me to an Insufficient Privileges error page.
    – Cong Hui
    Oct 1, 2014 at 15:12

Alright, this problem was due to a certain level of stupidity on my part.
I was testing the code through a site on a separate Developer account from Production.
When the code was moved inside the production, it worked fine, so it was an access privilege problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.