Skip to content

Commit

Permalink
Fix lint error in create-custom-token
Browse files Browse the repository at this point in the history
  • Loading branch information
bouzuya committed May 30, 2023
1 parent 2d7ff2a commit b338d27
Showing 1 changed file with 77 additions and 70 deletions.
147 changes: 77 additions & 70 deletions functions/src/functions/create-custom-token.ts
Expand Up @@ -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,
};
});
}

0 comments on commit b338d27

Please sign in to comment.