// Discord OAuth callback handler // User a /discord-connect endpointon megy → Discord auth → vissza a /discord-callback-re // Itt lekérjük a Discord user ID-t és mentjük a Memberstack member metadata-ba const { getStore } = require('@netlify/blobs'); function getStoreSafe(name) { const siteID = process.env.NETLIFY_SITE_ID; const token = process.env.NETLIFY_BLOBS_TOKEN; if (siteID && token) return getStore({ name, siteID, token, consistency: 'strong' }); return getStore(name); } exports.handler = async (event) => { const { code, state } = event.queryStringParameters || {}; if (!code) { return { statusCode: 400, headers: { 'Content-Type': 'text/html; charset=utf-8' }, body: '

Hiba

Hiányzó OAuth kód. Vissza a profilra

' }; } const clientId = process.env.DISCORD_CLIENT_ID; const clientSecret = process.env.DISCORD_CLIENT_SECRET; const redirectUri = (process.env.URL || 'https://betsmart.bet') + '/.netlify/functions/discord-callback'; if (!clientId || !clientSecret) { return { statusCode: 500, body: 'Discord OAuth nincs konfigurálva. Add hozzá a DISCORD_CLIENT_ID és DISCORD_CLIENT_SECRET env változókat.' }; } try { // 1. Code → access token const tokenRes = await fetch('https://discord.com/api/v10/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 }) }); if (!tokenRes.ok) { const t = await tokenRes.text(); console.error('Token exchange failed:', t); return { statusCode: 500, body: 'OAuth token hiba' }; } const tokenData = await tokenRes.json(); // 2. Access token → user info const userRes = await fetch('https://discord.com/api/v10/users/@me', { headers: { 'Authorization': 'Bearer ' + tokenData.access_token } }); if (!userRes.ok) { return { statusCode: 500, body: 'Discord user fetch hiba' }; } const discordUser = await userRes.json(); // 3. Mentés Blobs-ba: a 'state' a Memberstack member ID (a /discord-connect adta át) const memberId = state; if (memberId) { const store = getStoreSafe('subscriptions'); const existing = (await store.get('sub:' + memberId, { type: 'json' })) || {}; existing.discordId = discordUser.id; existing.discordUsername = discordUser.username; existing.discordLinkedAt = new Date().toISOString(); await store.setJSON('sub:' + memberId, existing); // Ha van aktív Pro/Elite előfizetés, rögtön adjuk meg a VIP role-t if (existing.status === 'active' || existing.status === 'trialing') { const guildId = process.env.DISCORD_GUILD_ID; const vipRoleId = process.env.DISCORD_VIP_ROLE_ID; const botToken = process.env.DISCORD_BOT_TOKEN; if (guildId && vipRoleId && botToken) { // 1. lépés: a user csatlakozzon a szerverhez (ha még nem) await fetch(`https://discord.com/api/v10/guilds/${guildId}/members/${discordUser.id}`, { method: 'PUT', headers: { 'Authorization': 'Bot ' + botToken, 'Content-Type': 'application/json' }, body: JSON.stringify({ access_token: tokenData.access_token }) }); // 2. lépés: VIP role await fetch(`https://discord.com/api/v10/guilds/${guildId}/members/${discordUser.id}/roles/${vipRoleId}`, { method: 'PUT', headers: { 'Authorization': 'Bot ' + botToken } }); } } } // Sikeres - visszairányítjuk a profil oldalra return { statusCode: 302, headers: { 'Location': '/profil.html?discord=connected' }, body: '' }; } catch (err) { console.error('Discord callback error:', err); return { statusCode: 500, headers: { 'Content-Type': 'text/html; charset=utf-8' }, body: `

Hiba

${err.message}

Vissza` }; } };