blob: 1286b90c9658cf4b57b0618e1c2545ff2c9d3929 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
import { sql } from '@vercel/postgres';
import { AUTHENTICATORS } from './_tables';
import Authenticator from '../types/Authenticator';
export default async function getAuthenticators<
T extends keyof Authenticator
>(userId: string, fields: T[]): Promise<Pick<Authenticator, T>[]> {
const query = `
SELECT ${fields.join(', ')}
FROM ${AUTHENTICATORS.name}
WHERE ${AUTHENTICATORS.fields.userId} = '${userId}';
`;
try {
const result = await sql.query(query);
return result.rows.map((row) => {
const authenticator: Partial<Pick<Authenticator, T>> = {};
for (const field in fields) {
// Assume type conversion is already done by node-postgres
authenticator[field] = row[AUTHENTICATORS.fields[field]];
}
return authenticator as Pick<Authenticator, T>;
});
} catch (err) {
throw new Error(`Failed to get authenticators for user with ID ${userId} from database. Query was ${query.replace(/\s+/g, ' ')}. ${err}`);
}
}
|