Skip to content

Instantly share code, notes, and snippets.

@anders-lundgren
Created September 8, 2020 15:04
Show Gist options
  • Save anders-lundgren/89e76dbdcaf8022151e27133c667f2b7 to your computer and use it in GitHub Desktop.
Save anders-lundgren/89e76dbdcaf8022151e27133c667f2b7 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
Machine(
{
id: "authentication",
initial: "loggedOut",
context: {
email: "",
password: ""
},
states: {
loggedOut: {
initial: "noErrors",
on: {
SIGN_UP: [
// check if everything is valid
{
target: ".invalidEmail",
cond: "checkEmail"
},
{
target: ".invalidPassword",
cond: "checkPassword"
},
// if all worked out, go ahead and authenticate
{
target: "authenticating",
actions: "saveData"
}
]
},
states: {
noErrors: {},
invalidEmail: {},
invalidPassword: {},
authFailed: {}
}
},
authenticating: {
invoke: {
id: "authenticateUser",
src: "authenticateUser",
onDone: {
target: "loggedIn",
actions: "clearSignUp"
},
onError: {
target: "loggedOut.authFailed"
}
}
},
loggedIn: {
on: {
LOGOUT: "loggedOut"
}
}
}
},
{
actions: {
clearSignUp: assign({
email: "",
password: ""
}),
saveData: assign({
email: (_, e) => e.email,
password: (_, e) => e.password
})
},
guards: {
checkEmail: (_, e) => !emailReg.test(e.email),
checkPassword: (_, e) => !passwordReg.test(e.password)
},
services: {
authenticateUser: ctx => {
const { email, password } = ctx;
return mockAuthenticate(email, password);
}
}
}
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment