Skip to content
Open
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
33 changes: 24 additions & 9 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,18 +183,12 @@ export class EmailLinkStrategy<User> extends Strategy<
sessionStorage: SessionStorage,
options: AuthenticateOptions
): Promise<User> {
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) {
Expand All @@ -204,7 +198,7 @@ export class EmailLinkStrategy<User> 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') {
Expand Down Expand Up @@ -451,4 +445,25 @@ export class EmailLinkStrategy<User> extends Strategy<
})
return { emailAddress, form: formData }
}

private async resolveFormData(
options: AuthenticateOptions,
request: Request
): Promise<FormData> {
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

}
}