Skip to content

Instantly share code, notes, and snippets.

@JitendraZaa
Last active July 22, 2018 16:34
Show Gist options
  • Select an option

  • Save JitendraZaa/8f8c43c4abe25661ff2d70460ff1e181 to your computer and use it in GitHub Desktop.

Select an option

Save JitendraZaa/8f8c43c4abe25661ff2d70460ff1e181 to your computer and use it in GitHub Desktop.
Janrain Registration Handler Code
/**
* @Author : Jitendra Zaa
* @Date : Jul 22 2018
* @Desc : SSO registration handler class used by Janrain Auth Provider
* */
global class AutocreatedRegHandler1532138524514 implements Auth.RegistrationHandler{
/**
* Custom logic to extract Twitter, Yahoo and Facebook unique Identifier
* */
global String extractUniqueIdentifier(Auth.UserData data){
//For Yahoo
String identifier = data.email;
//Logic for Twitter - print attributeMap and decide logic
if(identifier == null){
if(data.attributeMap.get('profile') != null){
String[] profileContent = data.attributeMap.get('profile').split(',');
for(String s : profileContent){
if(s.contains('preferredUsername=')){
identifier = s.replace('preferredUsername=','').trim();
break;
}
}
}
}
//Logic for Facebook
if(identifier == null){
}
return identifier;
}
/**
* This method would be executed only first time to establish relation between social account and User
* Once relation establish, it would appear in related list of user as "third party Account linked"
* */
global User createUser(Id portalId, Auth.UserData data){
String identifier = extractUniqueIdentifier(data);
List<User_Socia_Account__c> lstU = [Select Id, User__c FROM User_Socia_Account__c Where Identifier__c =: identifier] ;
if(lstU.size() > 0){
return new User(Id=lstU[0].User__c) ;
}
return null;
}
/**
* This method would be executed if user already approved social media account previously
* */
global void updateUser(Id userId, Id portalId, Auth.UserData data){
//Logic here to perform any operation during user login after social
}
}
<!-- Visualforce page used on public site to render Janrain SSO Widgets -->
<apex:page standardStylesheets="false" showHeader="false">
<h2>
As compared to other Auth providers, Janrain works differntly. It gives login widget.
Code of login widget needs to be placed on HTML page and therefore need public page.
</h2>
<p>
<div id="janrainEngageEmbed"></div>
</p>
<script type="text/javascript">
(function() {
if (typeof window.janrain !== 'object') window.janrain = {};
if (typeof window.janrain.settings !== 'object') window.janrain.settings = {};
janrain.settings.tokenUrl = 'https://jitendra21-dev-ed.my.salesforce.com/services/authcallback/JainrainProvider?flowtype=sso';
function isReady() { janrain.ready = true; };
if (document.addEventListener) {
document.addEventListener("DOMContentLoaded", isReady, false);
} else {
window.attachEvent('onload', isReady);
}
var e = document.createElement('script');
e.type = 'text/javascript';
e.id = 'janrainAuthWidget';
if (document.location.protocol === 'https:') {
e.src = 'https://rpxnow.com/js/lib/sfdcilovesso/engage.js';
} else {
e.src = 'http://widget-cdn.rpxnow.com/js/lib/sfdcilovesso/engage.js';
}
var s = document.getElementsByTagName('script')[0];
s.parentNode.insertBefore(e, s);
})();
</script>
</apex:page>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment