AvanzaCast/packages/backend-api/scripts/get_session_token.js
Cesar Mendivil 8b458a3ddf feat: add initial LiveKit Meet integration with utility scripts, configs, and core components
- Add Next.js app structure with base configs, linting, and formatting
- Implement LiveKit Meet page, types, and utility functions
- Add Docker, Compose, and deployment scripts for backend and token server
- Provide E2E and smoke test scaffolding with Puppeteer and Playwright helpers
- Include CSS modules and global styles for UI
- Add postMessage and studio integration utilities
- Update package.json with dependencies and scripts for development and testing
2025-11-20 12:50:38 -07:00

93 lines
2.9 KiB
JavaScript

#!/usr/bin/env node
// ...existing code...
// packages/backend-api/scripts/get_session_token.js
const { PrismaClient } = require('@prisma/client');
function usage() {
console.log('Usage: node scripts/get_session_token.js --id <sessionId> [--db <DATABASE_URL>]');
console.log('You can also set DATABASE_URL env var.');
}
function parseArgs() {
const out = {};
const args = process.argv.slice(2);
for (let i = 0; i < args.length; i++) {
const a = args[i];
if (a === '--id' || a === '-i') {
out.id = args[i+1]; i++;
} else if (a === '--db' || a === '-d') {
out.db = args[i+1]; i++;
} else if (a === '--help' || a === '-h') {
out.help = true;
}
}
return out;
}
function base64UrlDecode(s) {
if (!s) return '';
s = s.replace(/-/g, '+').replace(/_/g, '/');
while (s.length % 4) s += '=';
try { return Buffer.from(s, 'base64').toString('utf8'); } catch (e) { return null; }
}
(async function main() {
try {
const argv = parseArgs();
if (argv.help) { usage(); process.exit(0); }
const id = argv.id || process.env.SESSION_ID;
const dbUrl = argv.db || process.env.DATABASE_URL;
if (!id) {
console.error('Error: session id is required (--id)');
usage(); process.exit(2);
}
if (!dbUrl) {
console.error('Error: DATABASE_URL not provided (use --db or set env DATABASE_URL)');
process.exit(2);
}
console.log('Connecting to database...');
const prisma = new PrismaClient({ datasources: { db: { url: dbUrl } } });
await prisma.$connect();
console.log(`Querying Session id=${id} ...`);
const s = await prisma.session.findUnique({ where: { id } });
if (!s) {
console.error('Session not found');
await prisma.$disconnect();
process.exit(3);
}
console.log('--- Session row ---');
console.log(`id: ${s.id}`);
console.log(`room: ${s.room}`);
console.log(`username: ${s.username}`);
console.log(`createdAt: ${s.createdAt}`);
console.log(`expiresAt: ${s.expiresAt}`);
const token = s.token || '';
console.log('\n--- Token (truncated) ---');
console.log(token.length > 200 ? token.slice(0, 80) + '...' + token.slice(-80) : token);
// decode
const parts = token.split('.');
if (parts.length >= 2) {
const header = base64UrlDecode(parts[0]);
const payload = base64UrlDecode(parts[1]);
console.log('\n--- Decoded header ---');
try { console.log(JSON.stringify(JSON.parse(header), null, 2)); } catch (e) { console.log(header); }
console.log('\n--- Decoded payload ---');
try { console.log(JSON.stringify(JSON.parse(payload), null, 2)); } catch (e) { console.log(payload); }
} else {
console.log('Token does not look like a JWT');
}
await prisma.$disconnect();
process.exit(0);
} catch (err) {
console.error('Error running script:', err);
process.exit(1);
}
})();