Add support for Discord OAuth token exchange alongside existing Challonge integration

This commit is contained in:
2026-01-29 21:30:47 +00:00
parent d62106abf5
commit 2b34c0ccf5

View File

@@ -40,18 +40,10 @@ app.use('/gamemaster', gamemasterRouter);
/** /**
* Exchange authorization code for access token * Exchange authorization code for access token
* POST /oauth/token * POST /oauth/token
* Supports multiple providers: Challonge, Discord
*/ */
app.post('/oauth/token', async (req, res) => { app.post('/oauth/token', async (req, res) => {
if (!config.challonge.configured) { const { code, provider = 'challonge' } = req.body;
logger.warn('OAuth token request received but Challonge not configured');
return res.status(503).json({
error: 'Challonge OAuth not configured',
message:
'Set CHALLONGE_CLIENT_ID and CHALLONGE_CLIENT_SECRET environment variables'
});
}
const { code } = req.body;
if (!code) { if (!code) {
logger.warn('OAuth token request missing authorization code'); logger.warn('OAuth token request missing authorization code');
@@ -59,7 +51,60 @@ app.post('/oauth/token', async (req, res) => {
} }
try { try {
logger.debug('Exchanging authorization code for access token'); // Handle Discord OAuth
if (provider === 'discord') {
const clientId = process.env.VITE_DISCORD_CLIENT_ID;
const clientSecret = process.env.DISCORD_CLIENT_SECRET;
const redirectUri = process.env.VITE_DISCORD_REDIRECT_URI;
if (!clientId || !clientSecret) {
logger.warn('Discord OAuth not configured', {
hasClientId: !!clientId,
hasClientSecret: !!clientSecret
});
return res.status(503).json({
error: 'Discord OAuth not configured',
message: 'Set VITE_DISCORD_CLIENT_ID and DISCORD_CLIENT_SECRET environment variables'
});
}
logger.debug('Exchanging Discord authorization code for access token');
const response = await fetch('https://discord.com/api/oauth2/token', {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
body: new URLSearchParams({
client_id: clientId,
client_secret: clientSecret,
grant_type: 'authorization_code',
code: code,
redirect_uri: redirectUri
})
});
const data = await response.json();
if (!response.ok) {
logger.error('Discord token exchange failed', { status: response.status, data });
return res.status(response.status).json(data);
}
logger.info('Discord token exchange successful');
return res.json(data);
}
// Handle Challonge OAuth (default)
if (!config.challonge.configured) {
logger.warn('OAuth token request received but Challonge not configured');
return res.status(503).json({
error: 'Challonge OAuth not configured',
message:
'Set CHALLONGE_CLIENT_ID and CHALLONGE_CLIENT_SECRET environment variables'
});
}
logger.debug('Exchanging Challonge 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: {
@@ -77,14 +122,14 @@ app.post('/oauth/token', async (req, res) => {
const data = await response.json(); const data = await response.json();
if (!response.ok) { if (!response.ok) {
logger.error('Token exchange failed', { status: response.status, data }); logger.error('Challonge token exchange failed', { status: response.status, data });
return res.status(response.status).json(data); return res.status(response.status).json(data);
} }
logger.info('Token exchange successful'); logger.info('Challonge token exchange successful');
res.json(data); res.json(data);
} catch (error) { } catch (error) {
logger.error('Token exchange error', { error: error.message }); logger.error('Token exchange error', { provider, 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