-
-
Notifications
You must be signed in to change notification settings - Fork 775
Stop using Response errors when validating API Keys #1498
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,8 @@ | ||
import { z } from "zod"; | ||
import { ApiAuthenticationResult, authenticateApiRequest } from "../apiAuth.server"; | ||
import { | ||
ApiAuthenticationResultSuccess, | ||
authenticateApiRequestWithFailure, | ||
} from "../apiAuth.server"; | ||
import { ActionFunctionArgs, json, LoaderFunctionArgs } from "@remix-run/server-runtime"; | ||
import { fromZodError } from "zod-validation-error"; | ||
import { apiCors } from "~/utils/apiCors"; | ||
|
@@ -48,7 +51,7 @@ type ApiKeyHandlerFunction< | |
? z.infer<TSearchParamsSchema> | ||
: undefined; | ||
headers: THeadersSchema extends z.AnyZodObject ? z.infer<THeadersSchema> : undefined; | ||
authentication: ApiAuthenticationResult; | ||
authentication: ApiAuthenticationResultSuccess; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Update all usages of Changing the |
||
request: Request; | ||
}) => Promise<Response>; | ||
|
||
|
@@ -75,7 +78,7 @@ export function createLoaderApiRoute< | |
} | ||
|
||
try { | ||
const authenticationResult = await authenticateApiRequest(request, { allowJWT }); | ||
const authenticationResult = await authenticateApiRequestWithFailure(request, { allowJWT }); | ||
|
||
if (!authenticationResult) { | ||
return await wrapResponse( | ||
|
@@ -85,6 +88,14 @@ export function createLoaderApiRoute< | |
); | ||
} | ||
|
||
if (!authenticationResult.ok) { | ||
return await wrapResponse( | ||
request, | ||
json({ error: authenticationResult.error }, { status: 401 }), | ||
corsStrategy !== "none" | ||
); | ||
} | ||
ericallam marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
let parsedParams: any = undefined; | ||
if (paramsSchema) { | ||
const parsed = paramsSchema.safeParse(params); | ||
|
@@ -352,7 +363,7 @@ type ApiKeyActionHandlerFunction< | |
: undefined; | ||
headers: THeadersSchema extends z.AnyZodObject ? z.infer<THeadersSchema> : undefined; | ||
body: TBodySchema extends z.AnyZodObject ? z.infer<TBodySchema> : undefined; | ||
authentication: ApiAuthenticationResult; | ||
authentication: ApiAuthenticationResultSuccess; | ||
ericallam marked this conversation as resolved.
Show resolved
Hide resolved
|
||
request: Request; | ||
}) => Promise<Response>; | ||
|
||
|
@@ -396,7 +407,7 @@ export function createActionApiRoute< | |
|
||
async function action({ request, params }: ActionFunctionArgs) { | ||
try { | ||
const authenticationResult = await authenticateApiRequest(request, { allowJWT }); | ||
const authenticationResult = await authenticateApiRequestWithFailure(request, { allowJWT }); | ||
|
||
if (!authenticationResult) { | ||
return await wrapResponse( | ||
|
@@ -406,6 +417,14 @@ export function createActionApiRoute< | |
); | ||
} | ||
|
||
if (!authenticationResult.ok) { | ||
return await wrapResponse( | ||
request, | ||
json({ error: authenticationResult.error }, { status: 401 }), | ||
corsStrategy !== "none" | ||
); | ||
} | ||
ericallam marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
if (maxContentLength) { | ||
const contentLength = request.headers.get("content-length"); | ||
|
||
|
Uh oh!
There was an error while loading. Please reload this page.