Skip to content

Instantly share code, notes, and snippets.

@jimmynotjim
Created January 30, 2023 21:07
Show Gist options
  • Select an option

  • Save jimmynotjim/7196cd6675235c9b4c396e11b628a7b1 to your computer and use it in GitHub Desktop.

Select an option

Save jimmynotjim/7196cd6675235c9b4c396e11b628a7b1 to your computer and use it in GitHub Desktop.

Revisions

  1. jimmynotjim created this gist Jan 30, 2023.
    140 changes: 140 additions & 0 deletions machine.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,140 @@
    // Available variables:
    // - Machine
    // - interpret
    // - assign
    // - send
    // - sendParent
    // - spawn
    // - raise
    // - actions
    // - XState (all XState exports)

    const challengeEmailState = {
    initial: "confirmEmailVerification",
    context: {
    retries: 0
    },
    states: {
    confirmEmailVerification: {
    on: {
    SUBMIT: "requestEmailChallenge"
    }
    },
    requestEmailChallenge: {
    on: {
    RESOLVE: "enterEmailChallenge",
    REJECT: "confirmEmailVerification"
    }
    },
    enterEmailChallenge: {
    on: {
    SUBMIT: "verifyEmailChallenge"
    }
    },
    verifyEmailChallenge: {
    on: {
    RESOLVE: "verifyEmailSuccess",
    REJECT: "verifyEmailFailure"
    }
    },
    verifyEmailSuccess: {
    type: "final"
    },
    verifyEmailFailure: {
    on: {
    RETRY: {
    target: "enterEmailChallenge",
    actions: assign({
    retries: (context, event) => context.retries + 1
    })
    }
    }
    }
    }
    }

    const challengePhoneState = {
    initial: "confirmPhoneVerification",
    context: {
    retries: 0
    },
    states: {
    confirmPhoneVerification: {
    on: {
    SUBMIT: "requestPhoneChallenge"
    }
    },
    requestPhoneChallenge: {
    on: {
    RESOLVE: "enterPhoneChallenge",
    REJECT: "confirmPhoneVerification"
    }
    },
    enterPhoneChallenge: {
    on: {
    SUBMIT: "verifyPhoneChallenge",
    CHANGE_NUMBER: "resetPhoneNumber"
    }
    },
    verifyPhoneChallenge: {
    on: {
    RESOLVE: "verifyPhoneSuccess",
    REJECT: "verifyPhoneFailure"
    }
    },
    verifyPhoneSuccess: {
    type: "final"
    },
    verifyPhoneFailure: {
    on: {
    RETRY: {
    target: "enterPhoneChallenge",
    actions: assign({
    retries: (context, event) => context.retries + 1
    })
    }
    }
    },
    resetPhoneNumber: {
    type: "final"
    }
    }
    }

    const mfaMachine = Machine({
    id: "MFA_VERIFY",
    initial: "unverified",
    context: {
    retries: 0
    },
    states: {
    unverified: {
    on: {
    BEGIN: "challengeMethod"
    }
    },
    challengeMethod: {
    on:{
    EMAIL: "challengeEmail",
    PHONE: "challengePhone",
    }
    },
    challengeEmail: {
    on: {
    CONTINUE: "verified",
    },
    ...challengeEmailState
    },
    challengePhone: {
    on: {
    CONTINUE: "verified",
    RESET: "challengeEmail"
    },
    ...challengePhoneState
    },
    verified: {
    type: "final"
    }
    }
    });