Skip to content

Commit

Permalink
Use context.formData if available over request.formData() (#14)
Browse files Browse the repository at this point in the history
* Use context.formData if available over request.formData()

* Add docs
  • Loading branch information
sergiodxa authored Sep 8, 2022
1 parent da72ed7 commit 711b39e
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 1 deletion.
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,21 @@ export const action: ActionFunction = async ({ request, context }) => {
});
};
```

## Passing a pre-read FormData object

Because you may want to do validations or read valeus from the FormData before calling `authenticate`, the FormStrategy allows you to pass a FormData object as part of the optional context.

```ts
export const action: ActionFunction = async ({ request, context }) => {
let formData = await request.formData();
return await authenticator.authenticate("form", request, {
// use formData here
successRedirect: formData.get("redirectTo"),
failureRedirect: "/login",
context: { formData }, // pass pre-read formData here
});
};
```

This way, you don't need to clone the request yourself.
10 changes: 9 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export class FormStrategy<User> extends Strategy<
sessionStorage: SessionStorage,
options: AuthenticateOptions
): Promise<User> {
let form = await request.formData();
let form = await this.readFormData(request, options);

let user: User;
try {
Expand All @@ -39,4 +39,12 @@ export class FormStrategy<User> extends Strategy<

return this.success(user, request, sessionStorage, options);
}

private async readFormData(request: Request, options: AuthenticateOptions) {
if (options.context?.formData instanceof FormData) {
return options.context.formData;
}

return await request.formData();
}
}
40 changes: 40 additions & 0 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,44 @@ describe(FormStrategy, () => {

expect(verify).toBeCalledWith({ form: body, context });
});

test("should prefer context.formData over request.formData()", async () => {
let body = new FormData();
body.set("email", "[email protected]");

let request = new Request("", { body, method: "POST" });

let context = { formData: body };

let strategy = new FormStrategy<string>(async ({ form }) => {
return form.get("email") as string;
});

expect(
strategy.authenticate(request, sessionStorage, {
sessionKey: "user",
context,
})
).resolves.toBe("[email protected]");
});

test("ignore context.formData if it's not an FormData object", async () => {
let body = new FormData();
body.set("email", "[email protected]");

let request = new Request("", { body, method: "POST" });

let context = { formData: { email: "[email protected]" } };

let strategy = new FormStrategy<string>(async ({ form }) => {
return form.get("email") as string;
});

expect(
strategy.authenticate(request, sessionStorage, {
sessionKey: "user",
context,
})
).resolves.toBe("[email protected]");
});
});

0 comments on commit 711b39e

Please sign in to comment.