2

I’m creating a new community without self registration. My goal is to have users who already have a specified profile be able to login using LinkedIn as an Authentication provider. I’ve got everything working, except I can’t figure out how to pass a custom message if user successfully authenticates to LinkedIn, but is then turned away because the controller doesn’t let them in. Right now they just land back on the signin page with no error.

So, how can I pass a message from the registrationHandler back to the user? I've tried throwing an exception or sending an apex pages.message.

I’ve setup the LinkedIn auth provider using the following controller:

    global User createUser(Id portalId, Auth.UserData data){

    Profile p = [SELECT Id FROM profile WHERE name='Foo Partner'];
    List<User> userWithEmailMatch = [SELECT Id FROM User WHERE Email=:data.email AND ProfileId=:p.Id];
    if (userWithEmailMatch.size()>0){
        return userWithEmailMatch[0];
    }

    throw new LinkedInSSOException('This is a private system');
    throw new Auth.AuthProviderPluginException('My custom error');

    // ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.FATAL,'my error msg');
    // ApexPages.addMessage(myMsg);
    return null;
    }

      global void updateUser(Id userId, Id portalId, Auth.UserData data){
        //not updating anything, just letting user log back in
      }

      class LinkedInSSOException extends Exception{

      }
}

and the signin controller:

    global String username {get; set;}
    global String password {get; set;}
    public String orgId {get; set;}
    public String siteURL  {get; set;}
    public String startURL {get; set;}
    public List<AuthProvider> authProviders {get;set;}

    public signInController() {
        orgId = UserInfo.getOrganizationId();
        siteURL  = Site.getBaseUrl();
        startURL = System.currentPageReference().getParameters().get('startURL');
        if (startURL == null) startURL = '/';
        authProviders = [SELECT Id,DeveloperName,FriendlyName,ProviderType FROM AuthProvider];
    }
    global PageReference login() {
        return Site.login(username, password, startUrl);
    }

    global String getEncodedSiteUrl() {
        return EncodingUtil.urlEncode(siteURL, 'UTF-8');
    }

    global String getEncodedStartUrl() {
        return EncodingUtil.urlEncode(startURL, 'UTF-8');
    }
}

and the signin VF page:

<apex:page id="loginPage" showHeader="false" showChat="false" sidebar="false" standardStylesheets="false" controller="signInController">
<head>
    <meta charset="utf-8"/>
    <meta http-equiv="X-UA-Compatible" content="IE=edge"/>
    <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
    <title>Sign In</title>

    <apex:stylesheet value="{!URLFOR($Resource.Bootstrap,'/css/bootstrap.min.css')}"/>


</head>

  <body>

    <div class="container">

      <apex:form id="loginForm" forceSSL="true" styleClass="form-signin">
        <h2 class="form-signin-heading">foo</h2>
        <p>If this is your first time here please click the reset password link.</p>
        <p>You also may login using LinkedIn</p>
        <p>If you experience any issues please reach us at <a href="mailto:[email protected]">[email protected]</a></p>

        <apex:pageMessages showDetail="false" id="error"/>
        <apex:pageMessages/>

        <apex:inputText required="true" id="login-email" styleClass="form-control un" value="{!username}" label="Username"/>
        <apex:inputSecret id="login-password" styleClass="form-control pw" value="{!password}" label="Pasword"/>


        <apex:commandButton action="{!login}" value="Sign in" id="login-submit" styleClass="btn btn-lg btn-primary btn-block"/>
        <br/>
        <div id="login-option">
            <span class="form-signin-forgot"><a href="./resetPassword">Reset Password!</a></span>
        </div>
          <br/>
          <br/>  
        <a href="/foo/services/auth/sso/LinkedIn?startURL=%2Ffoo%2Fs%2F">login with linky in</a>          


      </apex:form>


    </div> 
  </body>

  <script src="https://code.jquery.com/ui/1.12.0/jquery-ui.min.js" ></script>
<script>
$(document).ready(function() {
    $('.un').attr('placeholder','{!$Label.site.username}');
    $('.pw').attr('placeholder','{!$Label.site.password}');
});
</script>   



</apex:page>

1 Answer 1

1

Answering my own question, the exception shows up as a ErrorDescription query string on the error page. Custom error page can be defined in Auth Provider setup.

4
  • can you please share an example to pass the error description query string to a custom error page? Thank you
    – learner
    Sep 12, 2022 at 16:55
  • you would use javascript to grab the error, as detailed in this question link and display it.
    – gneek
    Sep 16, 2022 at 15:49
  • Thank you @gneek. Is it possible to display the error on the Default experience cloud page by replacing the generic error message "We can’t log you in because of an authentication error"?
    – learner
    Sep 20, 2022 at 11:11
  • I'm not sure. The way I did it was to create a page accessible by the site's guest user and set that to be the custom error page in Auth Provider setup.
    – gneek
    Sep 20, 2022 at 14:38

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .