summaryrefslogtreecommitdiff
path: root/api/db/_addAuthenticator.ts
diff options
context:
space:
mode:
Diffstat (limited to 'api/db/_addAuthenticator.ts')
-rw-r--r--api/db/_addAuthenticator.ts32
1 files changed, 32 insertions, 0 deletions
diff --git a/api/db/_addAuthenticator.ts b/api/db/_addAuthenticator.ts
new file mode 100644
index 0000000..ad76037
--- /dev/null
+++ b/api/db/_addAuthenticator.ts
@@ -0,0 +1,32 @@
+import type Authenticator from '../types/Authenticator';
+
+import { sql } from '@vercel/postgres';
+import { AUTHENTICATORS } from './_tables';
+
+export default async function addAuthenticator(authenticator: Authenticator) {
+ try {
+ await sql.query(`
+ INSERT INTO ${AUTHENTICATORS.name} (
+ ${AUTHENTICATORS.fields.id},
+ ${AUTHENTICATORS.fields.backedUp},
+ ${AUTHENTICATORS.fields.counter},
+ ${AUTHENTICATORS.fields.deviceType},
+ ${AUTHENTICATORS.fields.publicKey}
+ ${AUTHENTICATORS.fields.transports},
+ ${AUTHENTICATORS.fields.type},
+ ${AUTHENTICATORS.fields.userId}
+ ) VALUES (
+ ${authenticator.id},
+ ${authenticator.backedUp},
+ ${authenticator.counter},
+ ${authenticator.deviceType},
+ ${authenticator.publicKey}
+ { ${authenticator.transports.join(', ')} },
+ ${authenticator.type},
+ ${authenticator.userId}
+ );
+ `);
+ } catch (err) {
+ throw new Error(`Failed to insert authenticator into database: ${JSON.stringify(authenticator)}.`, err);
+ }
+} \ No newline at end of file