import SQL from 'sql-template-strings'; import { filterObjectKeys, PermissionAreas } from '#shared/helpers.ts'; import type { Authenticator } from '#shared/user.ts'; import type { AuthenticatorRow } from '~~/server/express/user.ts'; export default defineEventHandler(async (event) => { const { isGranted } = await useAuthentication(event); if (!isGranted(PermissionAreas.Community) && !isGranted(PermissionAreas.Superuser)) { throw createError({ status: 401, statusMessage: 'Unauthorised', }); } const db = useDatabase(); const id = getRouterParam(event, 'id'); return (await db.all(SQL` SELECT * FROM authenticators WHERE userId = ${id} ORDER BY id DESC `)).map((authenticatorRow): Omit => { const payload = JSON.parse(authenticatorRow.payload); return { id: authenticatorRow.id, type: authenticatorRow.type, payload: typeof payload === 'string' ? '' : filterObjectKeys(payload, ['id', 'email', 'name', 'instance', 'username']), validUntil: authenticatorRow.validUntil, }; }); });