This is my endpoint
router.post("/users/github/:code", function(req, res) {
var state;
fetch('https://github.com/login/oauth/access_token/', {
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify({
client_id: 'xxx',
client_secret: 'xxx',
code: req.params.code
})
}).then(function(res) {
return res.json();
}).then(function(body) {
if (body.error !== 'bad_verification_code') {
const token = createToken(body.access_token);
var access_token = body.access_token;
fetch('https://api.github.com/orgs/xxx/members?access_token=' + access_token, {
method: 'GET',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json'
}
}).then(res => res.json())
.then(res => {
fetch('https://api.github.com/user?access_token=' + access_token, {
method: 'GET',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json'
},
}).then(res => res.json())
.then(user => {
var username = user.login;
var insert;
for (insert = 0; insert < res.length; insert++) {
if (username == res[insert].login) {
state = true;
}
};
console.log(state)
if (state) {
res.json({
success: true,
access_token: body.access_token,
jwt: token
});
} else {
res.json({
success: false
});
}
});
});
} else {
res.json({
success: false
});
}
});
});
And this is the part that is throwing the error
if(state){
res.json({success: true , access_token: body.access_token, jwt: token});
}else{
res.json({success: false});
}
Why could this be happening?