Skip to content

Commit

Permalink
moved request clone() to nextjs package
Browse files Browse the repository at this point in the history
  • Loading branch information
amorey committed Nov 21, 2024
1 parent c5d975f commit 83a0bee
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 7 deletions.
2 changes: 1 addition & 1 deletion packages/nextjs/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export function createCsrfProtect(opts?: Partial<NextConfigOptions>): NextCsrfPr
return async (request, response) => {
// execute protect function
const token = await _csrfProtect({
request,
request: request.clone(),
url: request.nextUrl,
getCookie: (name) => request.cookies.get(name)?.value,
setCookie: (cookie) => response.cookies.set(cookie),
Expand Down
9 changes: 3 additions & 6 deletions shared/src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,26 +73,23 @@ export async function getTokenString(request: Request, tokenOpts: TokenOptions =
// check request body
const contentType = request.headers.get('content-type') || 'text/plain';

// clone request so reading doesn't alter original
const requestCopy = request.clone();

// url-encoded or multipart/form-data
if (contentType === 'application/x-www-form-urlencoded' || contentType.startsWith('multipart/form-data')) {
const formData = await requestCopy.formData();
const formData = await request.formData();
const formDataVal = getTokenValueFromFormData(formData, tokenOpts);
if (typeof formDataVal === 'string') return formDataVal;
return '';
}

// json-encoded
if (contentType === 'application/json' || contentType === 'application/ld+json') {
const json = await requestCopy.json() as any;
const json = await request.json() as any;
const jsonVal = json[fieldName];
if (typeof jsonVal === 'string') return jsonVal;
return '';
}

const rawVal = await requestCopy.text();
const rawVal = await request.text();

// non-form server actions
if (contentType.startsWith('text/plain')) {
Expand Down

0 comments on commit 83a0bee

Please sign in to comment.