mirror of
https://github.com/formbricks/formbricks.git
synced 2026-05-21 03:31:20 -05:00
939fedfca4
Signed-off-by: gulshank0 <gulshanbahadur002@gmail.com> Co-authored-by: Tiago Farto <tiago@formbricks.com> Co-authored-by: Johannes <72809645+jobenjada@users.noreply.github.com> Co-authored-by: pandeymangg <anshuman.pandey9999@gmail.com> Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com> Co-authored-by: Tiago <1585571+xernobyl@users.noreply.github.com> Co-authored-by: Cursor <cursoragent@cursor.com> Co-authored-by: Theodór Tómas <theodortomas@gmail.com> Co-authored-by: Anshuman Pandey <54475686+pandeymangg@users.noreply.github.com> Co-authored-by: Bhagya Amarasinghe <b.sithumini@yahoo.com> Co-authored-by: Chowdhury Tafsir Ahmed Siddiki <ctafsiras@gmail.com> Co-authored-by: neila <40727091+neila@users.noreply.github.com> Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> Co-authored-by: Harsh Bhat <90265455+harshsbhat@users.noreply.github.com> Co-authored-by: Harsh Bhat <harshbhat@Harshs-MacBook-Air.local> Co-authored-by: Johannes <johannes@formbricks.com> Co-authored-by: Balázs Úr <balazs@urbalazs.hu> Co-authored-by: Gulshan <gulshanbahadur002@gmail.com> Co-authored-by: Harsh Bhat <harsh121102@gmail.com> Co-authored-by: Javi Aguilar <122741+itsjavi@users.noreply.github.com> Co-authored-by: Johannes <jobenjada@users.noreply.github.com>
42 lines
1.3 KiB
TypeScript
42 lines
1.3 KiB
TypeScript
import { NextRequest } from "next/server";
|
|
import { TAuthenticationApiKey } from "@formbricks/types/auth";
|
|
import {
|
|
DatabaseError,
|
|
InvalidInputError,
|
|
ResourceNotFoundError,
|
|
UniqueConstraintError,
|
|
} from "@formbricks/types/errors";
|
|
import { responses } from "@/app/lib/api/response";
|
|
import {
|
|
type AuthenticateApiKeyOptions,
|
|
authenticateApiKeyFromHeaders,
|
|
} from "@/modules/api/lib/api-key-auth";
|
|
|
|
export const authenticateRequest = async (
|
|
request: NextRequest,
|
|
options: AuthenticateApiKeyOptions = {}
|
|
): Promise<TAuthenticationApiKey | null> => {
|
|
return await authenticateApiKeyFromHeaders(request.headers, options);
|
|
};
|
|
|
|
export const handleErrorResponse = (error: any): Response => {
|
|
switch (error.message) {
|
|
case "NotAuthenticated":
|
|
return responses.notAuthenticatedResponse();
|
|
case "Unauthorized":
|
|
return responses.unauthorizedResponse();
|
|
default:
|
|
if (error instanceof UniqueConstraintError) {
|
|
return responses.conflictResponse(error.message);
|
|
}
|
|
if (
|
|
error instanceof DatabaseError ||
|
|
error instanceof InvalidInputError ||
|
|
error instanceof ResourceNotFoundError
|
|
) {
|
|
return responses.badRequestResponse(error.message);
|
|
}
|
|
return responses.internalServerErrorResponse("Some error occurred");
|
|
}
|
|
};
|