Code donation for posting on linkedin or twitter a message that the user has logged into your app

Hi, here’s a code donation for another rule that I’ve migrated to an action. With that I have now migrated all my rules to actions and disabled all my rules in time for November.

/**
* Handler that will be called during the execution of a PostLogin flow.
*
* @param {Event} event - Details about the user and the context in which they are logging in.
* @param {PostLoginAPI} api - Interface whose methods can be used to change the behavior of the login.
*/
exports.onExecutePostLogin = async (event, api) => {
  if(event.connection.strategy!=='twitter'&&event.connection.strategy!=='linkedin')return;

  let _=require('lodash');

  const ManagementClient = require('auth0').ManagementClient;
  const management = new ManagementClient({
    domain:'<domain>',
    clientId:'<clientId>',
    clientSecret:'<clientSecret>'
  });

  let socialIdentity=null;
  try {
    let user = await management.users.get({ id: event.user.user_id });
    socialIdentity = _.find(user.data.identities,identity=>identity.connection===event.connection.strategy);
    if(!socialIdentity)throw new Error(JSON.stringify(user));
  } catch (error) {
    console.error('Error fetching social identity', error);
    return;
  }
  
  let refs=({
  	'<client name 1>':[{
      comment:'<comment 1>',
      link:'<link 1>'
    }]
  })[event.client.name]||[];
  
  function shuffleArray(array) {
    for(let i=array.length-1;i>0;--i){
        let j=Math.floor(Math.random()*(i+1));
        let temp=array[i];
        array[i]=array[j];
        array[j]=temp;
    }
  }
  
  shuffleArray(refs);
  
  let url='';
  switch(event.connection.strategy){
    case 'twitter':
      url='https://api.x.com/2/tweets';
      break;
    case 'linkedin':
      url='https://api.linkedin.com/v2/shares';
      break;
  }
  
  let request=require('request');
  for(let i=0;i<refs.length;++i){
    let body='';
    let comment;
    comment=refs[i].comment+': '+refs[i].link;
    let headers={
      'Authorization':''
    };
    switch(event.connection.strategy){
      case 'linkedin':
        headers['Authorization']='Bearer '+socialIdentity.access_token;
        body=JSON.stringify({
         "content": {
          "contentEntities": [
              {
                  "entityLocation": refs[i].link,
              }
          ],
          "title": refs[i].comment
          },
          "distribution": {
          "linkedInDistributionTarget": {}
          },
          "owner": "urn:li:person:"+socialIdentity.user_id,
          "subject": comment,
          "text": {
            "text": comment
          }
        });
        headers['X-Restli-Protocol-Version']='2.0.0';
        await new Promise((resolve,reject)=>request({
          url,
          body,
          method:'POST',
          headers
        },(/** @type {any} */ err,/** @type {{ statusCode: number; }} */ resp,/** @type {any} */ body)=>{
          if(err||resp.statusCode!==200){
            // api.access.deny(JSON.stringify(err)+' '+JSON.stringify(body));
            console.error(JSON.stringify(err)+' '+JSON.stringify(body));
          }
          resolve(body);
        }));
        break;
      case 'twitter':
        const {TwitterApi} = require('twitter-api-v2');
        const twitterClient=new TwitterApi({
          appKey:'<appKey>',
          appSecret:'<appSecret>',
          accessToken:socialIdentity.access_token,
          accessSecret:socialIdentity.access_token_secret
        });
        try{
          await twitterClient.v2.tweet(comment.substring(0,280));
        }catch(error){
            //api.access.deny(JSON.stringify(error));
            console.error(JSON.stringify(error));
        }
        break;
    }
  }
};

Hello @customautosys,

Welcome back to the Auth0 community!

Thank you for sharing your solution with the community.

Thanks,
Tudor.