fix(api): sending duplicated responses for bot events (improved code as well)

This commit is contained in:
Carl-Gerhard Lindesvärd
2024-11-30 21:40:25 +01:00
parent d80754a6fd
commit afff099c5b
6 changed files with 101 additions and 144 deletions

View File

@@ -32,9 +32,11 @@ export class SdkAuthError extends Error {
}
}
type ClientWithProjectId = Client & { projectId: string };
export async function validateSdkRequest(
headers: RawRequestDefaultExpression['headers'],
): Promise<Client> {
): Promise<ClientWithProjectId> {
const clientIdNew = headers['openpanel-client-id'] as string;
const clientIdOld = headers['mixan-client-id'] as string;
const clientSecretNew = headers['openpanel-client-secret'] as string;
@@ -57,13 +59,11 @@ export async function validateSdkRequest(
throw createError('Ingestion: Missing client id');
}
const client = await db.client
.findUnique({
where: {
id: clientId,
},
})
.catch(() => null);
const client = await db.client.findUnique({
where: {
id: clientId,
},
});
if (!client) {
throw createError('Ingestion: Invalid client id');
@@ -91,17 +91,17 @@ export async function validateSdkRequest(
});
if (domainAllowed) {
return client;
return client as ClientWithProjectId;
}
if (client.cors === '*' && origin) {
return client;
return client as ClientWithProjectId;
}
}
if (client.secret && clientSecret) {
if (await verifyPassword(clientSecret, client.secret)) {
return client;
return client as ClientWithProjectId;
}
}