Skip to content

Commit

Permalink
fix: handle recovery brute force protection
Browse files Browse the repository at this point in the history
  • Loading branch information
jonas-jonas committed Nov 26, 2024
1 parent 2acabd5 commit 9e3320f
Showing 1 changed file with 42 additions and 7 deletions.
49 changes: 42 additions & 7 deletions packages/elements-react/src/util/onSubmitRecovery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,21 @@
// SPDX-License-Identifier: Apache-2.0

import {
ContinueWith,
FlowType,
GenericError,
handleContinueWith,
handleFlowError,
instanceOfContinueWithRecoveryUi,
OnRedirectHandler,
RecoveryFlow,
recoveryUrl,
UpdateRecoveryFlowBody,
} from "@ory/client-fetch"
import { frontendClient } from "./client"
import { OryClientConfiguration } from "./clientConfiguration"
import { OryFlowContainer } from "./flowContainer"
import { OnSubmitHandlerProps } from "./submitHandler"
import { frontendClient } from "./client"

/**
* Use this method to submit a recovery flow. This method is used in the `onSubmit` handler of the recovery form.
Expand Down Expand Up @@ -63,14 +68,44 @@ export async function onSubmitRecovery(
onRestartFlow: () => {
onRedirect(recoveryUrl(config), true)
},
onValidationError: (body: RecoveryFlow) => {
setFlowContainer({
flow: body,
flowType: FlowType.Recovery,
config,
})
onValidationError: (body: RecoveryFlow | { error: GenericError }) => {
if ("error" in body) {
handleContinueWithRecoveryUIError(body.error, config, onRedirect)
return
} else {
setFlowContainer({
flow: body,
flowType: FlowType.Recovery,
config,
})
}
},
onRedirect,
}),
)
}

function handleContinueWithRecoveryUIError(
error: GenericError,
config: OryClientConfiguration,
onRedirect: OnRedirectHandler,
) {
if (
"continue_with" in error.details &&
Array.isArray(error.details.continue_with)
) {
const continueWithRecovery = (
error.details.continue_with as ContinueWith[]
).find(instanceOfContinueWithRecoveryUi)
if (continueWithRecovery?.action === "show_recovery_ui") {
onRedirect(
config.project.recovery_ui_url +
"?flow=" +
continueWithRecovery?.flow.id,
false,
)
return
}
}
onRedirect(recoveryUrl(config), true)
}

0 comments on commit 9e3320f

Please sign in to comment.