Skip to content

Instantly share code, notes, and snippets.

@pckilgore
Created March 5, 2020 00:22
Show Gist options
  • Save pckilgore/69ca7ece6744d983b462b075e45807f8 to your computer and use it in GitHub Desktop.
Save pckilgore/69ca7ece6744d983b462b075e45807f8 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
// 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