Skip to content

Instantly share code, notes, and snippets.

@larrybotha
Last active October 22, 2019 14:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save larrybotha/6df5870d979fc4173d5d06e5a3103f9a to your computer and use it in GitHub Desktop.
Save larrybotha/6df5870d979fc4173d5d06e5a3103f9a to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
// Luna Med auth machine
const fetchMachine = Machine({
id: 'auth',
initial: 'unknown',
states: {
unknown: {
entry: 'storeRequestUrl',
on: {
'': [
{
cond: 'hasFoundUserAndOrg',
target: ['cognitoAuthd.user.found', 'cognitoAuthd.org.selected'],
},
{
cond: 'isValidXeroCallback',
target: 'cognitoAuthd.user.found',
},
{
cond: 'hasFoundUser',
target: 'cognitoAuthd.user.found',
},
{
cond: 'isAlreadyCognitoAuthd',
target: 'cognitoAuthd',
},
{
target: 'notAuthd',
},
],
},
},
notAuthd: {
on: {
COGNITO_AUTH_SUCCESS: 'cognitoAuthd',
},
},
loggingOut: {
invoke: {
src: 'logOut',
onDone: {
actions: ['redirectToLoginRoute', 'removeStateFromLs', 'removeContextFromLs'],
target: 'notAuthd',
},
onError: 'cognitoAuthd',
},
},
cognitoAuthd: {
type: 'parallel',
on: {
COGNITO_LOGOUT: {
actions: ['removeStateFromLs', 'removeContextFromLs'],
target: 'loggingOut',
},
},
states: {
user: {
initial: 'idle',
states: {
idle: {
invoke: {
id: 'userService',
src: 'userMachine',
onDone: {
actions: ['storeUser', 'persistContextToLs'],
target: 'found',
},
},
},
found: {type: 'final'},
},
},
org: {
initial: 'notSelected',
on: {
ORG_SELECTED_IN_CHILD: {
actions: ['storeOrg', 'persistContextToLs'],
target: '.selected',
},
XERO_ORG_SELECTED_IN_CHILD: {
actions: ['storeOrg', 'persistContextToLs'],
target: '.selectedWithXero',
},
},
states: {
notSelected: {
invoke: {
id: 'orgService',
src: 'orgMachine',
},
},
selected: {
entry: ['redirectToRequestUrl', 'clearRequestUrl'],
on: {
ORG_CHANGE: 'notSelected',
ORG_CHANGE_WITH_XERO: {
target: 'notSelected',
},
},
},
selectedWithXero: {
exit: 'revokeXeroAccessToken',
on: {
ORG_CHANGE: 'notSelected',
ORG_CHANGE_WITH_XERO: {
target: 'notSelected',
},
XERO_ACCESS_TOKEN_REVOKE: 'selected',
},
},
},
},
},
},
},
}, {
guards: {
hasFoundUserAndOrg: () => true,
isValidXeroCallback: () => false,
hasFoundUser: () => true,
isAlreadyCognitoAuthd: () => true,
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment