Skip to content

Instantly share code, notes, and snippets.

@jeroencoumans
Created March 3, 2020 14:13
Show Gist options
  • Save jeroencoumans/bc4c32ff58aad70b9fc0ae4084d0dd6a to your computer and use it in GitHub Desktop.
Save jeroencoumans/bc4c32ff58aad70b9fc0ae4084d0dd6a 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: 'kitt-data-auth',
initial: 'not_authenticated',
context: {},
states: {
unknown: {
on: {
// Transitions are tested one at a time.
// The first valid transition will be taken.
'': [
{ target: 'authenticated', cond: 'isAuthenticated' },
{ target: 'authenticating', cond: 'hasApiKeyOrOracle' },
{ target: 'redirecting', cond: 'hasLoginUrl' },
{ target: 'not_authenticated.user_not_found', cond: 'hasUserNotFound' },
{ target: 'not_authenticated' }
]
}
},
not_authenticated: {
initial: 'loading',
states: {
loading: {
invoke: {
src: 'fetchConfig',
onDone: {
target: 'show_login',
actions: ['assignConfig']
},
onError: {
target: 'show_error',
actions: ['onFetchFailed']
},
}
},
show_login: { type: 'final' },
show_error: { type: 'final' },
user_not_found: { type: 'final' },
},
on: {
LOGIN: {
target: 'redirecting',
actions: [ 'saveLoginUrl' ]
},
RETRY: 'not_authenticated'
}
},
redirecting: {
entry: 'redirectToLogin',
type: 'final'
},
authenticating: {
entry: ['cleanUrl'],
initial: 'loading',
states: {
loading: {
invoke: {
src: 'fetchApiKey',
onDone: [
{
target: 'success',
cond: 'hasResult',
actions: ['onSessionSuccess']
},
{
target: 'error',
cond: 'hasError',
actions: ['onFetchError']
}
],
onError: {
target: 'error',
actions: ['onFetchFailed']
}
}
},
success: { type: 'final' },
error: { type: 'final' },
},
},
authenticated: {
on: {
SESSION_EXPIRES: 'not_authenticated',
LOGOUT: 'not_authenticated',
}
}
},
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment