I have a very weird bug with onAuthorized. So i’ve got my live_config with a form that the broadcaster can submit (post call) and i have the onAuthorized with an ajax call (get). When i submit my form i don’t understand why but onAuthorized is called which trigger the “get” ajax even with preventDefault() and the submit event is not triggered. But the thing is, when i remove the get ajax call from onAuthorized, the post ajax call work perfectly. here is my partial code :
twitch.onAuthorized(function(auth) {
userId = auth.userId;
token = auth.token;
if(userId.charAt(0) === "A"){
client_logged = false;
twitch.actions.requestIdShare();
} else {
client_logged = true;
let getSucessCallback = function(data, status){
logSuccess(data, status);
if(data.err)
twitch.rig.log("error : ", data.err);
else {
if(data.data){
changePanel('currentPollInfo', availablePanel, data)
} else {
changePanel('createPoll', availablePanel)
}
}
};
$.ajax(createRequest("GET", "poll", getSucessCallback));
}
});
function createRequest(type, method, successCallback=null, data=null) {
let temp = {
type: type,
url: 'https://localhost:8081/' + method,
headers: { 'Authorization': 'Bearer ' + token },
beforeSend: function(xhr, settings){
changePanel('loader', availablePanel);
},
success: function(data, status, xhr){
successCallback(data, status);
},
error: logError
};
if(data)
temp.data = data;
return temp;
}
$(function() {
$('#form_vote').submit(function (evt) {
evt.preventDefault();
if(token){
let form_data = $(this).serializeArray();
let parameters = form_data.filter(function (elem) {
return elem.name !== "timer";
});
let timer = form_data.find(function (elem) {
return elem.name === "timer";
});
let timerFloat = parseFloat(timer.value);
if(!timerFloat){
console.log("error not a float");
} else{
let obj = {};
obj.parametersArray = parameters;
obj.timer = timerFloat;
let postSucessCallback = function(data, status){
logSuccess(data, status);
if(data.err)
twitch.rig.log("error : ", data.err);
else
changePanel('currentPollInfo', availablePanel, data)
};
$.ajax(createRequest("POST", "poll", postSucessCallback, obj));
}
} else {
twitch.rig.log("Not authorized");
}
});
});