⚙️ Improve OAuth token handling with enhanced logging and configuration usage

This commit is contained in:
2026-01-29 13:19:46 +00:00
parent 46808cf279
commit 05371a35f5

View File

@@ -42,7 +42,8 @@ app.use('/api/gamemaster', gamemasterRouter);
* POST /oauth/token * POST /oauth/token
*/ */
app.post('/oauth/token', async (req, res) => { app.post('/oauth/token', async (req, res) => {
if (!hasChallongeAuth) { if (!config.challonge.configured) {
logger.warn('OAuth token request received but Challonge not configured');
return res.status(503).json({ return res.status(503).json({
error: 'Challonge OAuth not configured', error: 'Challonge OAuth not configured',
message: message:
@@ -53,10 +54,12 @@ app.post('/oauth/token', async (req, res) => {
const { code } = req.body; const { code } = req.body;
if (!code) { if (!code) {
logger.warn('OAuth token request missing authorization code');
return res.status(400).json({ error: 'Missing authorization code' }); return res.status(400).json({ error: 'Missing authorization code' });
} }
try { try {
logger.debug('Exchanging authorization code for access token');
const response = await fetch('https://api.challonge.com/oauth/token', { const response = await fetch('https://api.challonge.com/oauth/token', {
method: 'POST', method: 'POST',
headers: { headers: {
@@ -64,24 +67,24 @@ app.post('/oauth/token', async (req, res) => {
}, },
body: new URLSearchParams({ body: new URLSearchParams({
grant_type: 'authorization_code', grant_type: 'authorization_code',
client_id: CLIENT_ID, client_id: config.challonge.clientId,
client_secret: CLIENT_SECRET, client_secret: config.challonge.clientSecret,
code: code, code: code,
redirect_uri: REDIRECT_URI redirect_uri: config.challonge.redirectUri
}) })
}); });
const data = await response.json(); const data = await response.json();
if (!response.ok) { if (!response.ok) {
console.error('Token exchange failed:', data); logger.error('Token exchange failed', { status: response.status, data });
return res.status(response.status).json(data); return res.status(response.status).json(data);
} }
console.log('Token exchange successful'); logger.info('Token exchange successful');
res.json(data); res.json(data);
} catch (error) { } catch (error) {
console.error('Token exchange error:', error); logger.error('Token exchange error', { error: error.message });
res.status(500).json({ res.status(500).json({
error: 'Token exchange failed', error: 'Token exchange failed',
message: error.message message: error.message