Skip to content

Instantly share code, notes, and snippets.

@rhodler
Created July 21, 2020 21:14
Show Gist options
  • Save rhodler/c43d01c96fbe615310e821055e7dbb9d to your computer and use it in GitHub Desktop.
Save rhodler/c43d01c96fbe615310e821055e7dbb9d to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
const login = (context, event) => new Promise((resolve, reject)=>{
const { email, password } = event;
if(email !== '[email protected]' || password !== 'azerty'){
return reject({ error: 'Le mot de passe ou l\'email est incorrect !' })
}
return resolve({ email, password });
});
const authMachine = Machine({
id: 'signIn',
initial: 'disconnected',
context:{
user: null,
error: ''
},
on: {
LOGIN: {
target: 'authentication.started'
}
},
states: {
authentication:{
states:{
started: {
invoke: {
id: 'login',
src: login,
onDone: {
target: 'success',
actions: assign({ user: (_, event) => event.data })
},
onError: {
target: 'failure',
actions: assign({ error: (_, event) => event.data.error })
}
}
},
success: {},
failure: {}
}
},
disconnected: {}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment