import { api } from '../core/api.js'; import { h, mount } from '../core/dom.js'; import { button, field, input, notice, toast } from '../core/ui.js'; /** Sign-in page: Discord OAuth2, or the bootstrap token when nothing is configured yet. */ const ERRORS = { 'oauth-not-configured': 'La connexion Discord n’est pas configurée. Renseigne `DISCORD_CLIENT_SECRET`, puis redémarre le bot.', 'state-mismatch': 'La session de connexion a expiré. Réessaie.', 'exchange-failed': 'Discord a refusé l’échange du code : la redirection déclarée dans le portail ne correspond pas à celle ci-dessous.', 'not-a-member': 'Ton compte Discord n’est pas membre du serveur FWS.', 'insufficient-level': 'Ton compte n’a pas le rôle Staff, Dev ou Owner sur le serveur.', 'missing-code': 'Discord n’a pas renvoyé de code d’autorisation.', access_denied: 'Connexion annulée.', }; export function render(host, { state }) { const params = new URLSearchParams(window.location.hash.split('?')[1] ?? ''); const failure = params.get('error'); const config = state.config ?? {}; const tokenInput = input({ type: 'password', placeholder: 'Jeton d’amorçage', autocomplete: 'off' }); const submitSetupToken = async () => { try { await api.post('/api/auth/setup', { token: tokenInput.value.trim() }); window.location.hash = '#/setup'; window.location.reload(); } catch (error) { toast(error.message, 'error'); } }; mount( host, h( 'div', { class: 'centered' }, h( 'div', { class: 'panel' }, h('img', { src: 'favicon.svg', alt: '', width: 56, height: 56, style: { margin: '0 auto' } }), h('h1', { text: 'FWS-Bot' }), h('p', { class: 'muted', text: 'Administration du bot, de l’organisation et des déploiements.' }), failure && notice(ERRORS[failure] ?? `Connexion refusée (${failure}).`, 'error'), config.oauthConfigured ? h('a', { class: 'btn btn-primary', href: '/auth/login', text: 'Se connecter avec Discord' }) : notice( 'La connexion Discord n’est pas encore disponible. Utilise le jeton d’amorçage pour ouvrir l’assistant de configuration.', 'warning', ), // The redirect is derived from the address in use, so it is the exact // value to paste in the portal — port included. config.redirectUri && h( 'details', { class: 'muted', style: { textAlign: 'left', width: '100%' } }, h('summary', { text: 'Redirection OAuth2 à déclarer dans le Developer Portal' }), h('pre', {}, h('code', { text: config.redirectUri })), h('p', { text: 'Developer Portal → ton application → OAuth2 → Redirects. Elle doit être identique au caractère près, port compris.', }), ), // Always reachable: when Discord sign-in is broken or not set up yet, // this is the only way back into the configuration, and hiding it left // no visible way out. h( 'details', { class: 'stack', style: { textAlign: 'left', width: '100%' }, open: !config.oauthConfigured }, h('summary', { text: 'Se connecter avec un jeton de configuration' }), field('Jeton d’amorçage', tokenInput, { help: 'Affiché par `npm run setup`, ou défini dans `WEB_SETUP_TOKEN`. Il ne donne accès qu’à la configuration, jamais au reste du site.', }), !config.setupAvailable && notice( 'Aucun jeton n’est configuré pour l’instant. Renseigne `WEB_SETUP_TOKEN` dans le .env puis redémarre le bot, ou lance `npm run setup` pour en obtenir un temporaire.', 'info', ), button('Ouvrir l’assistant de configuration', submitSetupToken, { tone: 'ghost', disabled: !config.setupAvailable, }), ), ), ), ); tokenInput.addEventListener('keydown', (event) => { if (event.key === 'Enter') submitSetupToken(); }); }