Thanks for your contribution @saltuka
Did you ever discover that there is an Auth0 extension called Custom Social Connections? I just became aware of it and installed it in my dashboard. There it is possible to edit the social connections.
There is a Fetch User Profile Script where the user access token of Twitch is available, so that I do not need the auth code. The following script is the default setup.
I wonder what request
method is. Would it be possible to change this code so that I am able to make another request, for example using axios or whatever, I could use the accessToken to call the Twitch user API and add data via the app_metadata.
Do you know more about it? How to execute multiple requests in this script?
I hope the Auth0 community support will be here as well.
function fetchUserProfile(accessToken, context, callback) {
request.get({
url: 'https://api.twitch.tv/helix/users',
headers: {
'Authorization': `Bearer ${accessToken}`,
'Client-ID': context.options.client_id
}
},
(err, resp, body) => {
if (err) {
return callback(err);
}
if (resp.statusCode !== 200) {
return callback(new Error(`[Response code: ${resp.statusCode}] ${body}`));
}
let bodyParsed;
try {
let twitchData = JSON.parse(body);
twitchData = twitchData.data[0];
bodyParsed = {
username: twitchData.display_name,
user_id: twitchData.id,
picture: twitchData.profile_image_url || '',
email: twitchData.email || '',
nickname: twitchData.username || '',
// Set custom data
app_metadata: {}
};
} catch (jsonError) {
return callback(new Error(body));
}
return callback(null, bodyParsed);
});
}
UPDATE
I haven’t found any documentation on this, but I just tried a few things and found a solution. I wish this was better documented.
I just used another request
method inside to call another API endpoint.
function fetchUserProfile(accessToken, context, callback) {
request.get({
url: 'https://api.twitch.tv/helix/users',
headers: {
'Authorization': `Bearer ${accessToken}`,
'Client-ID': context.options.client_id
}
},
(err, resp, body) => {
if (err) {
return callback(err);
}
if (resp.statusCode !== 200) {
return callback(new Error(`[Response code: ${resp.statusCode}] ${body}`));
}
let bodyParsed;
// Additional request to fetch more data using user access token
request.get({
url: 'https://api.twitch.tv/helix/channels/followers?broadcaster_id=XXX',
headers: {
'Authorization': `Bearer ${accessToken}`,
'Client-ID': context.options.client_id
}
},
(err1, resp1, body1) => {
try {
let twitchData = JSON.parse(body);
twitchData = twitchData.data[0];
bodyParsed = {
username: twitchData.display_name,
user_id: twitchData.id,
picture: twitchData.profile_image_url || '',
email: twitchData.email || '',
nickname: twitchData.username || '',
app_metadata: {
response: JSON.stringify(body1)
}
};
} catch (jsonError) {
return callback(new Error(body));
}
return callback(null, bodyParsed);
}
);
});
}