mirror of
https://github.com/formbricks/formbricks.git
synced 2026-01-08 00:40:09 -06:00
Co-authored-by: Dhruwang <dhruwangjariwala18@gmail.com> Co-authored-by: pandeymangg <anshuman.pandey9999@gmail.com>
35 lines
1.3 KiB
TypeScript
35 lines
1.3 KiB
TypeScript
import { hashApiKey } from "@/modules/api/v2/management/lib/utils";
|
|
import { ApiErrorResponseV2 } from "@/modules/api/v2/types/api-error";
|
|
import { getApiKeyWithPermissions } from "@/modules/organization/settings/api-keys/lib/api-key";
|
|
import { TAuthenticationApiKey } from "@formbricks/types/auth";
|
|
import { Result, err, ok } from "@formbricks/types/error-handlers";
|
|
|
|
export const authenticateRequest = async (
|
|
request: Request
|
|
): Promise<Result<TAuthenticationApiKey, ApiErrorResponseV2>> => {
|
|
const apiKey = request.headers.get("x-api-key");
|
|
if (!apiKey) return err({ type: "unauthorized" });
|
|
|
|
const apiKeyData = await getApiKeyWithPermissions(apiKey);
|
|
|
|
if (!apiKeyData) return err({ type: "unauthorized" });
|
|
|
|
const hashedApiKey = hashApiKey(apiKey);
|
|
|
|
const authentication: TAuthenticationApiKey = {
|
|
type: "apiKey",
|
|
environmentPermissions: apiKeyData.apiKeyEnvironments.map((env) => ({
|
|
environmentId: env.environmentId,
|
|
environmentType: env.environment.type,
|
|
permission: env.permission,
|
|
projectId: env.environment.projectId,
|
|
projectName: env.environment.project.name,
|
|
})),
|
|
hashedApiKey,
|
|
apiKeyId: apiKeyData.id,
|
|
organizationId: apiKeyData.organizationId,
|
|
organizationAccess: apiKeyData.organizationAccess,
|
|
};
|
|
return ok(authentication);
|
|
};
|