From 8d567e06b402440ec4d641ad25b751d8eb13e59f Mon Sep 17 00:00:00 2001 From: mantikoros Date: Tue, 30 Aug 2022 12:12:41 -0500 Subject: [PATCH] save twitch credentials cloud function --- functions/src/index.ts | 1 + functions/src/save-twitch-credentials.ts | 24 ++++++++++++++++++++++++ 2 files changed, 25 insertions(+) create mode 100644 functions/src/save-twitch-credentials.ts diff --git a/functions/src/index.ts b/functions/src/index.ts index 6ede39a0..2ebb9b86 100644 --- a/functions/src/index.ts +++ b/functions/src/index.ts @@ -53,6 +53,7 @@ export * from './resolve-market' export * from './unsubscribe' export * from './stripe' export * from './mana-bonus-email' +export * from './save-twitch-credentials' import { health } from './health' import { transact } from './transact' diff --git a/functions/src/save-twitch-credentials.ts b/functions/src/save-twitch-credentials.ts new file mode 100644 index 00000000..1553aa0f --- /dev/null +++ b/functions/src/save-twitch-credentials.ts @@ -0,0 +1,24 @@ +import * as admin from 'firebase-admin' +import { z } from 'zod' + +import { APIError, newEndpoint, validate } from './api' + +const bodySchema = z.object({ + userId: z.string(), + twitchInfo: z.object({ + twitchName: z.string(), + controlToken: z.string(), + }), +}) + +const BOT_ID = 'BOT_ID' + +export const savetwitchcredentials = newEndpoint({}, async (req, auth) => { + if (auth.uid !== BOT_ID) throw new APIError(403, 'Invalid access credentials') + + const { userId, twitchInfo } = validate(bodySchema, req.body) + await firestore.doc(`private-users/${userId}`).update({ twitchInfo }) + return { success: true } +}) + +const firestore = admin.firestore()