trivia: maybe fix the session token issue?

This commit is contained in:
Ayden Jahola 2024-09-09 15:17:03 +01:00
parent 6c3a6824a9
commit 82040aaba5
No known key found for this signature in database
GPG key ID: 71DD90AE4AE92742

View file

@ -54,14 +54,23 @@ const resetSessionToken = async () => {
if (!session) {
// If there's no session, create a new one as fallback
console.log("No session found, generating a new one.");
return await getSessionToken();
}
// Log the old token for comparison
console.log("Old token:", session.token);
// Reset the session token
const response = await axios.get(
`https://opentdb.com/api_token.php?command=reset&token=${session.token}`
);
const newToken = response.data.token;
if (response.data.response_code === 0) {
const newToken = response.data.token || session.token; // Sometimes the token might remain the same
// Log the new token to ensure it's correctly reset
console.log("New token:", newToken);
// Update token in the database
session.token = newToken;
@ -69,6 +78,13 @@ const resetSessionToken = async () => {
await session.save();
return newToken;
} else {
console.error(
"Failed to reset the token, response code:",
response.data.response_code
);
throw new Error("Unable to reset the session token.");
}
};
const fetchTriviaQuestion = async (categoryId, categoryName) => {
@ -93,9 +109,10 @@ const fetchTriviaQuestion = async (categoryId, categoryName) => {
const apiQuestion = response.data.results[0];
// Check if the token is exhausted (response code 4 indicates this)
if (response.data.response_code === 4) {
sessionToken = await resetSessionToken(); // Reset session token
// Retry fetching the question with the new token
if (response.data.response_code === 3) {
// Token not found
sessionToken = await getSessionToken(); // Create a new token
// Retry fetching the question
const retryResponse = await axios.get(
`https://opentdb.com/api.php?amount=1&category=${categoryId}&token=${sessionToken}`
);