Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Light account challenge #217

Merged
merged 2 commits into from
Sep 27, 2024
Merged
Show file tree
Hide file tree
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Unreleased

- fixed: Correctly handle challenge errors during light account creation.

## 3.22.2 (2024-09-26)

- fixed: Spinner showing on pin dots before biometric is read
Expand Down
14 changes: 7 additions & 7 deletions src/components/modals/ChallengeModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ interface Props {
* and the user successfully solves the challenge.
*/
export async function retryOnChallenge<T, C>(opts: {
task: (challengeId?: string) => Promise<T>
onCancel: () => C
onSuccess?: (challengeId: string) => void
cancelValue: C
saveChallenge?: (challengeId: string) => void
task: (challengeId: string | undefined) => Promise<T>
}): Promise<T | C> {
const { task, onCancel, onSuccess } = opts
const { cancelValue, saveChallenge, task } = opts

return await task().catch(async error => {
return await task(undefined).catch(async error => {
const challengeError = asMaybeChallengeError?.(error)
if (challengeError != null) {
const result = await Airship.show<boolean | undefined>(bridge => (
Expand All @@ -37,9 +37,9 @@ export async function retryOnChallenge<T, C>(opts: {
challengeUri={challengeError.challengeUri}
/>
))
if (result == null) return onCancel()
if (result == null) return cancelValue
if (result) {
onSuccess?.(challengeError.challengeId)
saveChallenge?.(challengeError.challengeId)
return await task(challengeError.challengeId)
}
}
Expand Down
4 changes: 1 addition & 3 deletions src/components/scenes/OtpErrorScene.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ export function OtpErrorScene(props: Props) {
inModal.current = true
const handleSubmit = async (otpKey: string): Promise<boolean | string> =>
await retryOnChallenge({
cancelValue: lstrings.failed_captcha_error,
async task(challengeId) {
const account = await attemptLogin(
context,
Expand All @@ -117,9 +118,6 @@ export function OtpErrorScene(props: Props) {
)
dispatch(completeLogin(account))
return true
},
onCancel() {
return lstrings.failed_captcha_error
}
}).catch(error => {
// Translate known errors:
Expand Down
4 changes: 1 addition & 3 deletions src/components/scenes/PasswordLoginScene.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ export const PasswordLoginScene = (props: Props) => {

setSpinner(true)
retryOnChallenge({
cancelValue: undefined,
async task(challengeId) {
Keyboard.dismiss()
const account = await attemptLogin(
Expand All @@ -203,9 +204,6 @@ export const PasswordLoginScene = (props: Props) => {
onPerfEvent({ name: 'passwordLoginEnd' })
onLogEvent('Pasword_Login')
await dispatch(completeLogin(account))
},
onCancel() {
return undefined
}
})
.catch(async error => {
Expand Down
8 changes: 4 additions & 4 deletions src/components/scenes/newAccount/NewAccountUsernameScene.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -176,12 +176,12 @@ export const ChangeUsernameComponent = (props: Props) => {

setIsFetchingAvailability(true)
retryOnChallenge({
cancelValue: undefined,
saveChallenge(challengeId) {
dispatch({ type: 'CREATE_CHALLENGE', data: challengeId })
},
async task(challengeId = lastChallengeId) {
return await context.usernameAvailable(text, { challengeId })
},
onCancel() {},
onSuccess(challengeId) {
dispatch({ type: 'CREATE_CHALLENGE', data: challengeId })
}
}).then(
isAvailable => {
Expand Down
9 changes: 5 additions & 4 deletions src/hooks/useCreateAccount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import { useImports } from './useImports'
export const useCreateAccountHandler = () => {
const { context, accountOptions } = useImports()
const dispatch = useDispatch()
const challengeId = useSelector(state => state.createChallengeId) ?? undefined
const savedChallengeId =
useSelector(state => state.createChallengeId) ?? undefined

const handleCreateAccount = useHandler(
async (createAccountParams: {
Expand All @@ -19,7 +20,8 @@ export const useCreateAccountHandler = () => {
const { username, password, pin } = createAccountParams

return await retryOnChallenge({
async task() {
cancelValue: undefined,
async task(challengeId = savedChallengeId) {
const account = await context.createAccount({
...accountOptions,
challengeId,
Expand All @@ -36,8 +38,7 @@ export const useCreateAccountHandler = () => {
dispatch(loadTouchState())

return account
},
onCancel() {}
}
})
}
)
Expand Down
Loading