diff --git a/functions/src/functions/create-custom-token.ts b/functions/src/functions/create-custom-token.ts index 0a3a257..0740e1a 100644 --- a/functions/src/functions/create-custom-token.ts +++ b/functions/src/functions/create-custom-token.ts @@ -13,77 +13,84 @@ export function buildCreateCustomToken( app: App, region: string ): functions.HttpsFunction { - return functions.region(region).https.onCall(async (data, _context) => { - if (typeof data !== "object" || data === null) { - throw new functions.https.HttpsError( - "invalid-argument", - "The function must be called with one argument" - ); - } - const deviceId = data.device_id; - const deviceSecret = data.device_secret; - if (typeof deviceId !== "string" || typeof deviceSecret !== "string") { - throw new functions.https.HttpsError( - "invalid-argument", - "The function must be called with one argument" - ); - } + return functions + .region(region) + .https.onCall(async (data: unknown, _context) => { + if ( + typeof data !== "object" || + data === null || + !("device_id" in data) || + !("device_secret" in data) + ) { + throw new functions.https.HttpsError( + "invalid-argument", + "The function must be called with one argument" + ); + } + const deviceId = data.device_id; + const deviceSecret = data.device_secret; + if (typeof deviceId !== "string" || typeof deviceSecret !== "string") { + throw new functions.https.HttpsError( + "invalid-argument", + "The function must be called with one argument" + ); + } - type Device = { - encryptedSecret: string; - id: string; - uid: string; - }; - const db = getFirestore(app); - const deviceSnapshot = await db - .collection("devices") - .doc(deviceId) - .withConverter({ - fromFirestore: (snapshot: QueryDocumentSnapshot): Device => { - return snapshot.data() as Device; - }, - toFirestore: (device: Device): DocumentData => { - return device; - }, - }) - .get(); - const device = deviceSnapshot.data(); - const uid = - device !== undefined && - device.id === deviceId && - (await compare(deviceSecret, device.encryptedSecret)) - ? device.uid - : uuidv4(); - const customToken = await getAuth(app).createCustomToken(uid); - await deviceSnapshot.ref.set({ - id: deviceId, - encryptedSecret: await hash(deviceSecret, 10), - uid, - }); + type Device = { + encryptedSecret: string; + id: string; + uid: string; + }; + const db = getFirestore(app); + const deviceSnapshot = await db + .collection("devices") + .doc(deviceId) + .withConverter({ + fromFirestore: (snapshot: QueryDocumentSnapshot): Device => { + return snapshot.data() as Device; + }, + toFirestore: (device: Device): DocumentData => { + return device; + }, + }) + .get(); + const device = deviceSnapshot.data(); + const uid = + device !== undefined && + device.id === deviceId && + (await compare(deviceSecret, device.encryptedSecret)) + ? device.uid + : uuidv4(); + const customToken = await getAuth(app).createCustomToken(uid); + await deviceSnapshot.ref.set({ + id: deviceId, + encryptedSecret: await hash(deviceSecret, 10), + uid, + }); - // create user document - type UserDocument = { - id: string; - account_ids: string[]; - }; - const userSnapshot = await db - .collection("users") - .doc(uid) - .withConverter({ - fromFirestore: (snapshot: QueryDocumentSnapshot): UserDocument => { - return snapshot.data() as UserDocument; - }, - toFirestore: (user: UserDocument): DocumentData => { - return user; - }, - }) - .get(); - if (!userSnapshot.exists) { - await userSnapshot.ref.create({ account_ids: [], id: uid }); - } + // create user document + type UserDocument = { + id: string; + account_ids: string[]; + }; + const userSnapshot = await db + .collection("users") + .doc(uid) + .withConverter({ + fromFirestore: (snapshot: QueryDocumentSnapshot): UserDocument => { + return snapshot.data() as UserDocument; + }, + toFirestore: (user: UserDocument): DocumentData => { + return user; + }, + }) + .get(); + if (!userSnapshot.exists) { + await userSnapshot.ref.create({ account_ids: [], id: uid }); + } - return { - custom_token: customToken, - }; - }); + return { + custom_token: customToken, + }; + }); }