From 5db00319fb6d247448ee32692bc7020b22c0e59c Mon Sep 17 00:00:00 2001 From: Dave Pereira-Gurnell Date: Fri, 22 Mar 2024 14:07:28 +0000 Subject: [PATCH] Allow the user to pass formData to the options parameter of authenticate, similar to remix-auth-form --- src/index.ts | 33 ++++++++++++++++++++++++--------- 1 file changed, 24 insertions(+), 9 deletions(-) diff --git a/src/index.ts b/src/index.ts index aac8ba4..410d15a 100644 --- a/src/index.ts +++ b/src/index.ts @@ -183,18 +183,12 @@ export class EmailLinkStrategy extends Strategy< sessionStorage: SessionStorage, options: AuthenticateOptions ): Promise { + const formData = await this.resolveFormData(options, request) + const session = await sessionStorage.getSession( request.headers.get('Cookie') ) - const form = new URLSearchParams(await request.text()) - const formData = new FormData() - - // Convert the URLSearchParams to FormData - for (const [name, value] of form) { - formData.append(name, value) - } - // This should only be called in an action if it's used to start the login process if (request.method === 'POST') { if (!options.successRedirect) { @@ -204,7 +198,7 @@ export class EmailLinkStrategy extends Strategy< } // get the email address from the request body - const emailAddress = form.get(this.emailField) + const emailAddress = formData.get(this.emailField) // if it doesn't have an email address, if (!emailAddress || typeof emailAddress !== 'string') { @@ -451,4 +445,25 @@ export class EmailLinkStrategy extends Strategy< }) return { emailAddress, form: formData } } + + private async resolveFormData( + options: AuthenticateOptions, + request: Request + ): Promise { + const contextFormData = options.context?.formData + + if (contextFormData instanceof FormData) { + return contextFormData + } + const form = new URLSearchParams(await request.text()) + const formData = new FormData() + + // Convert the URLSearchParams to FormData + for (const [name, value] of form) { + formData.append(name, value) + } + + return formData + + } }