Created
July 21, 2020 21:14
-
-
Save rhodler/c43d01c96fbe615310e821055e7dbb9d to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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