Skip to content

Instantly share code, notes, and snippets.

@brookslybrand
Created February 12, 2020 21:18
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 brookslybrand/b6580bb9f00a5f38c5fd8f19e9dc8daf to your computer and use it in GitHub Desktop.
Save brookslybrand/b6580bb9f00a5f38c5fd8f19e9dc8daf to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
// this machine expects an authenticate service to be passed in
// and handles where a user is in the authentication process.
// `logedIn` is marked as final state because once logged in
// the auth-context will be updated by firebase and so the user
// will automatically be navigated away from this component
const authenticationMachine = Machine(
{
id: 'authentication',
initial: 'loggedOut',
context: {
errorMessage: ''
},
states: {
loggedOut: {
on: {
LOGIN: 'authenticating'
},
initial: 'unchecked',
states: {
unchecked: {},
error: {}
}
},
authenticating: {
entry: 'resetError',
invoke: {
src: 'authenticate',
onDone: {
target: 'loggedIn',
internal: false
},
onError: {
target: 'loggedOut.error',
internal: false,
actions: 'setError'
}
}
},
loggedIn: {
type: 'final'
}
}
},
{
actions: {
setError: assign({
errorMessage: (context, event) => event.data
}),
resetError: assign({ errorMessage: '' })
},
services: {
authenticate: () =>
Promise.reject('authenticate must be passed in as an option')
}
}
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment