Skip to content

Instantly share code, notes, and snippets.

@snikch
Last active October 27, 2020 01:45
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 snikch/4f746cb1f1a3e31c71534c7dd76c2e0a to your computer and use it in GitHub Desktop.
Save snikch/4f746cb1f1a3e31c71534c7dd76c2e0a 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 login = {
id: 'login',
initial: 'loading',
states: {
loading:{on: {LOADED: 'details'}},
details: {on: {SUBMIT: 'submitting'}},
submitting: {
on: {
ERROR: 'errored',
INVALID: 'invalid',
SUCCESS: 'success',
UNVERIFIED: 'unverified'
}
},
errored: {
on: {RETRY: 'submitting'}
},
invalid: {
on: {RETRY: 'submitting'}
},
success: {
type: 'final'
},
unverified: {
on: {
RESEND_EMAIL: "#resendActivationEmail"
}
}
},
on: {
REGISTER: 'register',
FORGOT_PASSWORD: 'forgotPassword'
}
}
const register = {
id: 'register',
initial: 'loading',
states: {
loading:{on: {LOADED: 'details'}},
details: {on: {SUBMIT: 'submitting'}},
submitting: {
on: {
ERROR: 'errored',
INVALID: 'invalid',
SUCCESS: 'success'
}
},
errored: {
on: {RETRY: 'submitting'}
},
invalid: {
on: {RETRY: 'submitting'}
},
success: {
on: {
RESEND_EMAIL: "#resendActivationEmail"
}
}
},
on: {
LOGIN: 'login'
}
}
const forgotPassword = {
id: 'forgotPassword',
initial: 'loading',
states: {
loading:{on: {LOADED: 'details'}},
details: {
on: {
SUBMIT: 'submitting'
}
},
submitting: {
on: {
ERROR: 'errored',
INVALID: 'invalid',
SUCCESS: 'success'
}
},
errored: {
on: {RETRY: 'submitting'}
},
invalid: {
on: {RETRY: 'submitting'}
},
success: {
type: 'final'
}
},
on: {
LOGIN: 'login'
},
on: {
REGISTER: 'register',
LOGIN: 'login'
}
}
const deviceFlow = {
id: 'deviceFlow',
initial: 'enterCode',
context: {
error: ""
},
states: {
enterCode: {
on: {
SUBMIT: 'submitting'
}
},
submitting: {
on: {
ERROR: {
target: 'enterCode',
actions: assign({
error: (context, event) => event.error
})
},
INVALID: {
target: 'enterCode',
actions: assign({
error: (context, event) => event.error
})
},
SUCCESS: '#authorize'
}
}
}
}
const authorize = {
id: 'authorize',
initial: 'loading',
context: {
error: ""
},
states: {
loading: {
always: [
{
target: '#login',
cond: 'isUserLoggedIn',
actions: assign({
nextAction: (context, event) => 'authorize' // need to pass auth code somehow
})
}
],
on: {
LOADED: 'prompt',
ERROR: {
target: 'errored',
actions: assign({
error: (context, event) => event.error
})
},
INVALID: {
target: 'invalid',
actions: assign({
error: (context, event) => event.error
})
}
}
},
invalid: {
type: 'final'
},
errored: {
on: {
RETRY: 'loading'
}
},
prompt:{
on: {
GRANT: 'granted',
DENY: 'denied'
}
},
granted: {
type: 'final'
},
denied: {
type: 'final'
}
}
}
const resendActivationEmail = {
id: 'resendActivationEmail',
initial: 'ignore_this_flow_Phil',
context: {
error: ""
},
states: {
ignore_this_flow_Phil: {
},
sending: {
on: {
SUCCESS: 'success',
ERROR: {
target: 'errored',
actions: assign({
error: (context, event) => event.error
})
}
}
},
errored: {
on: {
RETRY: 'sending'
}
},
success: {
type: 'final'
}
},
on: {
LOGIN: 'login'
}
}
const resetPassword = {
id: 'resetPassword',
initial: 'loading',
states: {
loading: {
on: {
ERROR: 'loadingErrored',
INVALID: 'codeInvalid',
SUCCESS: 'details'
}
},
loadingErrored: {
on: {
RETRY: 'loading'
}
},
codeInvalid: {
type: 'final'
},
details: {
on: {
SUBMIT: 'submitting'
}
},
submitting: {
on: {
ERROR: 'submittingErrored',
INVALID: 'detailsInvalid',
SUCCESS: 'success'
}
},
submittingErrored: {
on: {
RETRY: 'submitting'
}
},
detailsInvalid: {
on: {
RETRY: 'submitting'
}
},
success: {
on: {
LOGIN: '#login'
}
}
}
}
const fetchMachine = Machine({
id: 'auth',
initial: 'login',
context: {
nextUrl: ''
},
states: {
login,
register,
forgotPassword,
deviceFlow,
authorize,
resendActivationEmail,
resetPassword
},
on: {
DEVICE_FLOW: 'deviceFlow',
LOGIN: 'login',
RESET_PASSWORD: 'resetPassword'
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment