Created
September 8, 2020 15:05
-
-
Save anders-lundgren/66ab09d13daba3608d80d7f743b4d146 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
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({ | |
}), | |
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