Sub is returning undefined

Hi, I am able to use Custom Social Connections but it returns undefined for the “sub” field in the profile which is undesirable. I am not sure why it is undefined.

I have tried to write a rule to replace the sub code with something more useful but it doesn’t seem to work:

function (user, context, callback) {
try{
if(user.sub.startsWith(‘oauth2|LegalOne|’))user.sub=user.sub.replace(‘undefined’,user.preferred_username);
for(let i=0;i<user.identities.length;++i){
if(user.identities[i].sub.startsWith(‘oauth2|LegalOne|’))user.identities[i].sub=user.identities[i].sub.replace(‘undefined’,user.identities[i].preferred_username);
}
}catch(e){}
callback(null,user,context);
}