Last active
January 16, 2020 20:46
-
-
Save pckilgore/81af3ecb03102ab2cb8bcc826fc17ad0 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Available variables: | |
// - Machine | |
// - interpret | |
// - assign | |
// - send | |
// - sendParent | |
// - spawn | |
// - raise | |
// - actions | |
// - XState (all XState exports) | |
const fetchMachine = Machine( | |
{ | |
id: "2fa", | |
initial: "loading", | |
context: { | |
cognito2faEnabled: undefined, | |
errorMessage: "" | |
}, | |
states: { | |
loading: { | |
invoke: { | |
src: "getCurrent2faStatus", | |
onDone: { | |
target: "unknown", | |
actions: assign({ | |
cognito2faEnabled: (_, e) => e.data | |
}) | |
}, | |
onError: { | |
target: "error" | |
} | |
} | |
}, | |
unknown: { | |
on: { | |
"": [ | |
{ target: "enabled", cond: ctx => ctx.cognito2faEnabled === true }, | |
{ target: "disabled", cond: ctx => ctx.cognito2faEnabled === false }, | |
{ target: "error" } | |
] | |
} | |
}, | |
enabled: { | |
on: { | |
DISABLE: "disable" | |
} | |
}, | |
disabled: { | |
on: { | |
VERIFY_PHONE: "checkVerifiedPhone" | |
} | |
}, | |
checkVerifiedPhone: { | |
on: { | |
UNVERIFIED: "sendCode", | |
VERIFIED: "enable", | |
ERROR: { | |
target: "error", | |
actions: assign({ errorMessage: "" }) | |
} | |
} | |
}, | |
sendCode: { | |
on: { | |
CODE_SENT: "getCode", | |
ERROR: { | |
target: "error", | |
actions: assign({ | |
errorMessage: (_, evt) => evt.value || "" | |
}) | |
} | |
} | |
}, | |
verifyCode: { | |
on: { | |
CODE_VALID: "phoneVerified", | |
CODE_INVALID: "getCode", | |
ERROR: "error" | |
} | |
}, | |
phoneVerified: { | |
on: { | |
"": "enable" | |
} | |
}, | |
disable: { | |
on: { | |
MFA_DISABLED: "disabled", | |
ERROR: "error" | |
} | |
}, | |
enable: { | |
on: { | |
ENABLED: "enabled", | |
ERROR: "error" | |
} | |
}, | |
getCode: { | |
invoke: { | |
src: "openModal" | |
}, | |
on: { | |
GOT_CODE: "verifyCode", | |
CANCEL: "loading" | |
} | |
}, | |
error: { | |
on: { | |
RETRY: "loading" | |
} | |
} | |
} | |
} | |
, { | |
services: { | |
"getCurrent2faStatus": () => Promise.resolve(true) | |
} | |
}); | |
function isEnabled(context){ | |
return context.cognito2faEnabled === true; | |
} | |
function isDisabled(context){ | |
return context.cognito2faEnabled === false; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment