diff --git a/code/websites/pokedex.online/src/views/ChallongeTest.vue b/code/websites/pokedex.online/src/views/ChallongeTest.vue index 1771ec0..2c2c945 100644 --- a/code/websites/pokedex.online/src/views/ChallongeTest.vue +++ b/code/websites/pokedex.online/src/views/ChallongeTest.vue @@ -420,30 +420,51 @@ const client = computed(() => { return createChallongeV1Client(apiKey.value); } else { // v2.1 supports OAuth, client credentials, and API key - // Priority: OAuth/Client Credentials > API Key - // Note: Client credentials are OAuth tokens with APPLICATION scope permissions - if (isClientCredsAuthenticated.value && clientCredsToken.value) { - // Use client credentials token (supports APPLICATION scope) - console.log('🔐 Using Client Credentials token'); - return createChallongeV2Client( - { token: clientCredsToken.value, type: AuthType.OAUTH }, - { debug: true } - ); - } else if (isAuthenticated.value && accessToken.value) { - // Use OAuth token if authenticated (USER scope) - console.log('🔐 Using OAuth token'); - return createChallongeV2Client( - { token: accessToken.value, type: AuthType.OAUTH }, - { debug: true } - ); - } else if (apiKey.value) { - // Fall back to API key (USER scope only) - console.log('🔑 Using API Key'); + // Smart priority based on scope selection: + // - APPLICATION scope: prefer client credentials (they're designed for this) + // - USER scope: prefer OAuth user tokens or API key (client creds don't work well here) + + if (tournamentScope.value === ScopeType.APPLICATION) { + // APPLICATION scope - prefer client credentials + if (isClientCredsAuthenticated.value && clientCredsToken.value) { + console.log('🔐 Using Client Credentials token for APPLICATION scope'); + return createChallongeV2Client( + { token: clientCredsToken.value, type: AuthType.OAUTH }, + { debug: debugMode.value } + ); + } else if (isAuthenticated.value && accessToken.value) { + console.log('🔐 Using OAuth user token for APPLICATION scope'); + return createChallongeV2Client( + { token: accessToken.value, type: AuthType.OAUTH }, + { debug: debugMode.value } + ); + } + } else { + // USER scope - prefer OAuth user tokens or API key (client creds may not work) + if (isAuthenticated.value && accessToken.value) { + console.log('🔐 Using OAuth user token for USER scope'); + return createChallongeV2Client( + { token: accessToken.value, type: AuthType.OAUTH }, + { debug: debugMode.value } + ); + } else if (apiKey.value) { + console.log('🔑 Using API Key for USER scope'); + return createChallongeV2Client( + { token: apiKey.value, type: AuthType.API_KEY }, + { debug: debugMode.value } + ); + } + } + + // Fallback: try API key + if (apiKey.value) { + console.log('🔑 Using API Key (fallback)'); return createChallongeV2Client( { token: apiKey.value, type: AuthType.API_KEY }, - { debug: true } + { debug: debugMode.value } ); } + return null; } });