mirror of
https://github.com/formbricks/formbricks.git
synced 2025-12-30 18:30:32 -06:00
Co-authored-by: Johannes <johannes@formbricks.com> Co-authored-by: Matti Nannt <mail@matthiasnannt.com>
30 lines
945 B
TypeScript
30 lines
945 B
TypeScript
import "server-only";
|
|
import { ZId } from "@formbricks/types/environment";
|
|
import { cache } from "../cache";
|
|
import { hasUserEnvironmentAccess } from "../environment/auth";
|
|
import { validateInputs } from "../utils/validate";
|
|
import { apiKeyCache } from "./cache";
|
|
import { getApiKey } from "./service";
|
|
|
|
export const canUserAccessApiKey = (userId: string, apiKeyId: string): Promise<boolean> =>
|
|
cache(
|
|
async () => {
|
|
validateInputs([userId, ZId], [apiKeyId, ZId]);
|
|
|
|
try {
|
|
const apiKeyFromServer = await getApiKey(apiKeyId);
|
|
if (!apiKeyFromServer) return false;
|
|
|
|
const hasAccessToEnvironment = await hasUserEnvironmentAccess(userId, apiKeyFromServer.environmentId);
|
|
if (!hasAccessToEnvironment) return false;
|
|
|
|
return true;
|
|
} catch (error) {
|
|
throw error;
|
|
}
|
|
},
|
|
|
|
[`canUserAccessApiKey-${userId}-${apiKeyId}`],
|
|
{ tags: [apiKeyCache.tag.byId(apiKeyId)] }
|
|
)();
|