34 lines
1.1 KiB
TypeScript

import SQL from 'sql-template-strings';
import type { AuthenticatorRow } from '~/server/express/user.ts';
import { filterObjectKeys } from '~/src/helpers.ts';
import type { Authenticator } from '~/src/user.ts';
export default defineEventHandler(async (event) => {
const { isGranted } = await useAuthentication(event);
if (!isGranted('community') && !isGranted('*')) {
throw createError({
status: 401,
statusMessage: 'Unauthorised',
});
}
const db = useDatabase();
const id = getRouterParam(event, 'id');
return (await db.all<AuthenticatorRow>(SQL`
SELECT * FROM authenticators
WHERE userId = ${id}
ORDER BY id DESC
`)).map((authenticatorRow): Omit<Authenticator, 'userId'> => {
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,
};
});
});