Last active
May 7, 2020 14:17
-
-
Save DaveHudson/d13328c94683cd4a903f5531f372ab1b 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 challengeStates = { | |
initial: "noMFA", | |
states: { | |
noMFA: { | |
on: { | |
"": [{ target: "#amplifyAuth.signIn.roleCheck" }], | |
}, | |
}, | |
mfaSetup: {}, | |
softwareTokenMFA: {}, | |
smsMFA: {}, | |
newPasswordRequired: {}, | |
customChallenge: {}, | |
}, | |
}; | |
const signInStates = { | |
initial: "signInForm", | |
states: { | |
signInForm: { | |
on: { | |
ROUTESIGNUP: "#amplifyAuth.signUp", | |
ROUTEFORGOTPASSWORD: "#amplifyAuth.forgotPassword", | |
SUBMIT: "signInAPI", | |
}, | |
}, | |
signInAPI: { | |
invoke: { | |
src: "authSignIn", | |
onDone: { | |
target: "checkChallenge", | |
actions: ["setStatus"], | |
}, | |
onError: { | |
target: "#amplifyAuth.error", | |
actions: ["setError"], | |
}, | |
}, | |
}, | |
checkChallenge: { | |
...challengeStates, | |
}, | |
roleCheck: { | |
on: { | |
"": [ | |
{ target: "#amplifyAuth.roleRestricted", cond: "userNotAdmin" }, | |
{ target: "#amplifyAuth.authenticated", cond: "userIsAdmin" }, | |
], | |
}, | |
}, | |
}, | |
}; | |
const signUpStates = { | |
initial: "signUpForm", | |
states: { | |
signUpForm: { | |
on: { | |
ROUTESIGNIN: "#amplifyAuth.signIn", | |
ROUTEFORGOTPASSWORD: "#amplifyAuth.forgotPassword", | |
SUBMIT: "signUpAPI", | |
}, | |
}, | |
signUpAPI: { | |
invoke: { | |
src: "authSignUp", | |
onDone: { | |
target: "#amplifyAuth.confirmSignUp", | |
actions: ["setStatus"], | |
}, | |
onError: { | |
target: "#amplifyAuth.error", | |
actions: ["setError"], | |
}, | |
}, | |
}, | |
}, | |
}; | |
const confirmSignUpStates = { | |
initial: "confirmSignUpForm", | |
states: { | |
confirmSignUpForm: { | |
on: { | |
SUBMIT: "confirmSignUpAPI", | |
}, | |
}, | |
confirmSignUpAPI: { | |
invoke: { | |
src: "authConfirmSignUp", | |
onDone: { | |
target: "#amplifyAuth.signIn", | |
actions: ["setStatus"], | |
}, | |
onError: { | |
target: "#amplifyAuth.error", | |
actions: ["setError"], | |
}, | |
}, | |
}, | |
}, | |
}; | |
const forgotPasswordStates = { | |
initial: "forgotPasswordForm", | |
states: { | |
forgotPasswordForm: { | |
on: { | |
ROUTESIGNIN: "#amplifyAuth.signIn", | |
SUBMIT: "forgotPasswordAPI", | |
}, | |
}, | |
forgotPasswordAPI: { | |
invoke: { | |
src: "authForgotPassword", | |
onDone: { | |
target: "#amplifyAuth.newPassword", | |
actions: ["setStatus"], | |
}, | |
onError: { | |
target: "#amplifyAuth.error", | |
actions: ["setError"], | |
}, | |
}, | |
}, | |
}, | |
}; | |
const newPasswordStates = { | |
initial: "setNewPasswordForm", | |
states: { | |
setNewPasswordForm: { | |
on: { | |
SUBMIT: "setNewPasswordAPI", | |
}, | |
}, | |
setNewPasswordAPI: { | |
invoke: { | |
src: "authSetNewPassword", | |
onDone: { | |
target: "#amplifyAuth.signIn", | |
actions: ["setStatus"], | |
}, | |
onError: { | |
target: "#amplifyAuth.error", | |
actions: ["setError"], | |
}, | |
}, | |
}, | |
}, | |
}; | |
const signOutStates = { | |
initial: "signOutAPI", | |
states: { | |
signOutAPI: { | |
invoke: { | |
src: "authSignOut", | |
onDone: { | |
target: "#amplifyAuth.signIn", | |
actions: ["setStatus"], | |
}, | |
onError: { | |
target: "#amplifyAuth.error", | |
actions: ["setError"], | |
}, | |
}, | |
}, | |
}, | |
}; | |
const authMachine = Machine( | |
{ | |
id: "amplifyAuth", | |
initial: "signIn", | |
context: { | |
authState: "signIn", | |
user: { | |
data: { | |
isAdmin: true | |
} | |
}, | |
error: {}, | |
}, | |
states: { | |
signUp: { | |
...signUpStates, | |
}, | |
confirmSignUp: { | |
...confirmSignUpStates, | |
}, | |
forgotPassword: { | |
...forgotPasswordStates, | |
}, | |
newPassword: { | |
...newPasswordStates, | |
}, | |
signIn: { | |
...signInStates, | |
}, | |
error: {}, | |
roleRestricted: { | |
on: { | |
SIGNOUT: "#amplifyAuth.signOut", | |
}, | |
}, | |
authenticated: { | |
on: { | |
SIGNOUT: "#amplifyAuth.signOut", | |
}, | |
}, | |
signOut: { | |
...signOutStates, | |
}, | |
}, | |
}, | |
{ | |
guards: { | |
userNotAdmin: (context) => { | |
if (!context.user.data.isAdmin) { | |
return true; | |
} | |
}, | |
userIsAdmin: (context) => { | |
if (context.user.data.isAdmin) { | |
return true; | |
} | |
}, | |
}, | |
} | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment