I added some error handling to my code now every request I send to twitch is returning 400 invalid authorization code
.The authorization code always has a value that ‘looks’ legit, but still no dice. If anyone can take a look I would really appreciate it. here is the relevant code
in my server.js:
if (!req.query.code) throw new Error('Error authorization code is null');
try {
var response = await twitchOAuth.getToken(req.query.code);
twitchId = twitchOAuth.decodeToken(response);
res.cookie('twitchId', twitchId);
res.redirect(baseUrl);
} catch (err) {
log.info(err, 'twitch');
res.redirect('/error');
}
});
//this is where the error is happening:
const getToken = async(code) => {
try {
if (!code) throw new Error('parameter code is null');
const response = await fetch(`https://id.twitch.tv/oauth2/token?client_id=${clientId}&client_secret=${clientSecret}&code=${code}&grant_type=authorization_code&redirect_uri=${redirectUri}/twitchcallback`, {
method: 'POST',
});
return await response.json();
} catch (err) {
throw new Error(`Error getting token: ${JSON.stringify(err)}`);
}
};
//the method failing because of the error:
const decodeToken = (response) => {
try {
if (!response.id_token) throw new Error(' Parameter response.id_token is null');
var decoded = jwt.decode(response.id_token, { complete: true });
return decoded.payload.sub; //this is the twitchId
} catch (err) {
throw new Error(`Error decoding token: ${JSON.stringify(err)}`);
}
};