Skip to content

Instantly share code, notes, and snippets.

@DanCouper
Created January 16, 2020 16:33
Show Gist options
  • Save DanCouper/9d9a55824a01f243d81bdd162120d93b to your computer and use it in GitHub Desktop.
Save DanCouper/9d9a55824a01f243d81bdd162120d93b to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
const TheoUserSecurityPreference = {
PIN: "PIN",
BIOMETRIC: "BIOMETRIC",
NONE: "NONE"
};
// services
async function cacheStaticImages() {
console.log("Images cached")
return;
}
async function loadFonts() {
console.log("Fonts loaded");
return;
}
const Api = {
async getAdditionalSecurityPreference() {
console.log("User preference fetched");
return await TheoUserSecurityPreference.NONE;
},
async currentAuthenticatedUser() {
console.log("User loaded");
return;
},
}
const SplashScreen = {
async preventAutoHide() {
console.log("Splash screen kep open");
return;
},
async hide() {
console.log("Splash screen hidden");
return;
}
};
const BootMachine = Machine({
id: "boot",
initial: "init",
context: {
isAppReady: false,
shouldDisplaySecurityHint: false,
userSecurityPreference: null,
userSessionIsActive: false,
},
states: {
init: {
invoke: {
src: SplashScreen.preventAutoHide,
onDone: "cachingAssets",
},
},
cachingAssets: {
invoke: {
src: cacheStaticImages,
onDone: "loadingFonts",
onError: "bootError"
}
},
loadingFonts: {
invoke: {
src: loadFonts,
onDone: "fetchingSecurityPreferences",
onError: "bootError",
}
},
fetchingSecurityPreferences: {
invoke: {
src: Api.getAdditionalSecurityPreference,
onDone: {
actions: assign({ userSecurityPreference: (context, event) => event.data })
},
onError: {
actions: assign({ shouldDisplaySecurityHint: true })
}
},
onDone: "checkingForActiveSession",
},
checkingForActiveSession: {
invoke: {
src: Api.currentAuthenticatedUser,
onDone: {
actions: assign({ userSessionIsActive: true }),
},
onError: {
actions: assign({ userSessionIsActive: false }),
},
},
onDone: "bootComplete",
},
bootComplete: {
invoke: {
src: SplashScreen.hide,
onDone: "authDelegation",
},
},
bootError: {
type: "final"
},
authDelegation: {
on: {
"": [
{ target: "loggingIn", cond: (context) => context.isAppReady && !context.userSessionIsActive },
{ target: "additionalSecurity", cond: (context) => context.isAppReady && context.userSessionIsActive && (context.userSecurityPreference === TheoUserSecurityPreference.PIN || context.userSecurityPreference === TheoUserSecurityPreference.BIOMETRIC) },
{ target: "securityHint", cond: (context) => context.isAppReady && context.userSessionIsActive && context.shouldDisplaySecurityHint },
{ target: "authenticationSuccessful", cond: (context) => (context.userSessionIsActive && context.userSecurityPreference === Theo.userSecurityPreference.NONE ) }
]
}
},
loggingIn: {
on: { "": "authenticationSuccessful" }
},
additionalSecurity: {
on: { "": "authenticationSuccessful" }
},
securityHint: {
on: { "": "authenticationSuccessful" }
},
authenticationSuccessful: {
type: "final"
}
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment