Skip to content

Instantly share code, notes, and snippets.

@ferdinandsalis
Created July 9, 2020 10:42
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 ferdinandsalis/73ffd5d48d82c6f30fc966207d5a3447 to your computer and use it in GitHub Desktop.
Save ferdinandsalis/73ffd5d48d82c6f30fc966207d5a3447 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
const machine = Machine(
{
id: 'auth',
initial: 'loading',
context: null,
states: {
loading: {
id: 'loading',
initial: 'start',
states: {
start: {
invoke: {
src: 'retrieveTokenFromStorage',
onDone: {
target: 'validation',
},
onError: {
target: '#loggedOut',
},
},
},
validation: {
invoke: {
src: 'validateToken',
onDone: {
target: '#loggedIn',
actions: ['assignClaims', 'assignToken'],
},
onError: {
target: '#failure',
actions: ['clearToken', 'clearClaims', 'assignError'],
},
},
},
},
},
loggedOut: {
id: 'loggedOut',
on: {
LOGIN: {
target: '#loading.validation',
},
},
},
failure: {
id: 'failure',
exit: 'clearError',
on: {
LOGIN: {
target: '#loading.validation',
},
},
},
loggedIn: {
id: 'loggedIn',
on: {
LOGOUT: {
target: '#loggedOut',
actions: 'clearToken',
},
},
initial: 'idle',
states: {
idle: {
on: {
REFRESH: {
target: 'refreshing',
},
},
entry: 'sendRefresh',
},
refreshing: {
invoke: {
src: 'refreshToken',
onDone: {
target: 'validation',
actions: 'clearError',
},
onError: {
target: '#loggedOut',
actions: ['clearToken', 'assignError'],
},
},
},
validation: {
invoke: {
src: 'validateToken',
onDone: {
target: 'idle',
actions: ['assignToken', 'assignClaims', 'clearError'],
},
onError: {
target: '#failure',
actions: ['clearToken', 'clearClaims', 'assignError'],
},
},
},
},
},
},
}
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment