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