/** * Auth session smoke tests (run: node server/scripts/test-auth-session.mjs) */ const API = 'http://127.0.0.1:3001/api'; async function login() { const res = await fetch(`${API}/auth/token`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ email: 'superadmin@meoo.local', password: 'SuperAdmin@123' }), }); const body = await res.json(); if (!res.ok) throw new Error(body.error_description || 'login failed'); return body; } async function getUser(token) { const res = await fetch(`${API}/auth/user`, { headers: { Authorization: `Bearer ${token}` }, }); return { status: res.status, body: await res.json().catch(() => ({})) }; } async function main() { console.log('1. Login...'); const session = await login(); console.log(' OK expires_in=', session.expires_in, 'token_len=', session.access_token?.length); console.log('2. Auth user with token...'); const u1 = await getUser(session.access_token); console.log(' status=', u1.status, 'email=', u1.body?.email); console.log('3. Invalid token...'); const u2 = await getUser('invalid.token.here'); console.log(' status=', u2.status, 'error=', u2.body?.error); console.log('4. JWT payload exp check...'); const payload = JSON.parse(Buffer.from(session.access_token.split('.')[1], 'base64url').toString()); const ttlSec = payload.exp - payload.iat; console.log(' ttl_hours=', (ttlSec / 3600).toFixed(2)); console.log('\nAll backend auth checks done.'); } main().catch((e) => { console.error('FAIL:', e.message); process.exit(1); });