/** * Test-Script: prüft was der PB_SUPERUSER_TOKEN tatsächlich darf. * Aufruf: npx tsx scripts/test-token.ts */ import { readFileSync } from 'node:fs' const env = readFileSync('.env', 'utf8').split('\n') let lastKey = '' for (const line of env) { const m = line.match(/^([A-Z_]+)=(.*)$/) if (m) { process.env[m[1]] = m[2] lastKey = m[1] } else if (lastKey && line.length > 0) { process.env[lastKey] = (process.env[lastKey] ?? '') + line } } const TOKEN = process.env.PB_SUPERUSER_TOKEN ?? '' const API = 'https://api.stammtisch-hersbruck.de' // JWT-Payload dekodieren const [, payloadB64] = TOKEN.split('.') if (payloadB64) { const payload = JSON.parse(Buffer.from(payloadB64, 'base64').toString('utf8')) console.log('Token-Payload:', payload) console.log('Token-Länge:', TOKEN.length) } async function tryEndpoint(label: string, path: string, init?: RequestInit) { const res = await fetch(`${API}${path}`, { ...init, headers: { Authorization: TOKEN, 'Content-Type': 'application/json', ...(init?.headers ?? {}) }, }) const text = await res.text() console.log(`\n${label}: ${res.status}`) try { console.log(JSON.stringify(JSON.parse(text), null, 2).slice(0, 200)) } catch { console.log(text.slice(0, 200)) } } await tryEndpoint('GET /api/collections', '/api/collections?perPage=1') await tryEndpoint('GET /api/collections/_superusers', '/api/collections/_superusers') await tryEndpoint('GET /api/collections/events', '/api/collections/events') await tryEndpoint('POST /api/collections (Probe)', '/api/collections', { method: 'POST', body: JSON.stringify({ name: '__probe_temp__', type: 'base', fields: [{ name: 'foo', type: 'text' }], }), })