Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions lib/keys/createApiKeyHandler.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { NextRequest, NextResponse } from "next/server";
import { getCorsHeaders } from "@/lib/networking/getCorsHeaders";
import { validateCreateApiKeyBody } from "@/lib/keys/validateCreateApiKeyBody";
import { getAuthenticatedAccountId } from "@/lib/auth/getAuthenticatedAccountId";
import { validateAuthContext } from "@/lib/auth/validateAuthContext";
import { createKey } from "@/lib/keys/createKey";

/**
* Handler for creating a new API key for the authenticated account.
* Requires authentication via Bearer token in Authorization header.
* Supports both x-api-key header and Authorization Bearer token.
*
* Body parameters:
* - key_name (required): The name for the API key
Expand All @@ -16,11 +16,11 @@ import { createKey } from "@/lib/keys/createKey";
*/
export async function createApiKeyHandler(request: NextRequest): Promise<NextResponse> {
try {
const accountIdOrError = await getAuthenticatedAccountId(request);
if (accountIdOrError instanceof NextResponse) {
return accountIdOrError;
const authResult = await validateAuthContext(request);
if (authResult instanceof NextResponse) {
return authResult;
}
const accountId = accountIdOrError;
const { accountId } = authResult;

const body = await request.json();

Expand Down
12 changes: 6 additions & 6 deletions lib/keys/deleteApiKeyHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ import { NextRequest, NextResponse } from "next/server";
import { getCorsHeaders } from "@/lib/networking/getCorsHeaders";
import { validateDeleteApiKeyBody } from "@/lib/keys/validateDeleteApiKeyBody";
import { deleteApiKey } from "@/lib/supabase/account_api_keys/deleteApiKey";
import { getAuthenticatedAccountId } from "@/lib/auth/getAuthenticatedAccountId";
import { validateAuthContext } from "@/lib/auth/validateAuthContext";
import { getApiKeys } from "@/lib/supabase/account_api_keys/getApiKeys";

/**
* Handler for deleting an API key.
* Requires authentication via Bearer token in Authorization header.
* Supports both x-api-key header and Authorization Bearer token.
* Only allows deleting keys that belong to the authenticated account.
*
* Body parameters:
Expand All @@ -18,11 +18,11 @@ import { getApiKeys } from "@/lib/supabase/account_api_keys/getApiKeys";
*/
export async function deleteApiKeyHandler(request: NextRequest): Promise<NextResponse> {
try {
const accountIdOrError = await getAuthenticatedAccountId(request);
if (accountIdOrError instanceof NextResponse) {
return accountIdOrError;
const authResult = await validateAuthContext(request);
if (authResult instanceof NextResponse) {
return authResult;
}
const accountId = accountIdOrError;
const { accountId } = authResult;

const body = await request.json();

Expand Down
12 changes: 6 additions & 6 deletions lib/keys/getApiKeysHandler.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
import { NextRequest, NextResponse } from "next/server";
import { getCorsHeaders } from "@/lib/networking/getCorsHeaders";
import { getApiKeys } from "@/lib/supabase/account_api_keys/getApiKeys";
import { getAuthenticatedAccountId } from "@/lib/auth/getAuthenticatedAccountId";
import { validateAuthContext } from "@/lib/auth/validateAuthContext";

/**
* Handler for retrieving API keys for the authenticated account.
* Requires authentication via Bearer token in Authorization header.
* Supports both x-api-key header and Authorization Bearer token.
*
* @param request - The request object.
* @returns A NextResponse with the API keys.
*/
export async function getApiKeysHandler(request: NextRequest): Promise<NextResponse> {
try {
const accountIdOrError = await getAuthenticatedAccountId(request);
if (accountIdOrError instanceof NextResponse) {
return accountIdOrError;
const authResult = await validateAuthContext(request);
if (authResult instanceof NextResponse) {
return authResult;
}
const accountId = accountIdOrError;
const { accountId } = authResult;

const { data, error } = await getApiKeys({ accountId });

Expand Down
Loading