Skip to content

Instantly share code, notes, and snippets.

@DanCouper
Last active January 16, 2020 14:04
Show Gist options
  • Select an option

  • Save DanCouper/1e3981630f790e1f0956ca24d37b0874 to your computer and use it in GitHub Desktop.

Select an option

Save DanCouper/1e3981630f790e1f0956ca24d37b0874 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"
}
// export const bootMachine = Machine<BootMachineContext, BootMachineEvent, BootMachineSchema>({
const bootMachine = Machine<({
id: "boot",
initial: "fetchingAssets",
states: {
fetchingAssets: {
on: {
"": "fetchingSecurityPreferences",
}
},
fetchingSecurityPreferences: {
on: {
SECURITY_PREF_FETCH_SUCCESS: {
target: "checkingActiveSession",
actions: sendParent({type: "SET_USER_SECURITY_PREFERENCE", preference: TheoUserSecurityPreference })
},
SECURITY_PREF_FETCH_FAILURE: {
target: "checkingActiveSession",
actions: sendParent("DISPLAY_SECURITY_SETUP_HINT"),
},
}
},
checkingActiveSession: {
on: {
ACTIVE_SESSION: {
target: "appReady",
actions: sendParent("ACTIVE_SESSION")
},
NO_ACTIVE_SESSION: {
target: "appReady",
actions: sendParent("NO_ACTIVE_SESSION")
}
}
},
signalBootComplete: {
on: {
"": {
target: "appReady",
actions: sendParent("BOOT_COMPLETE")
}
}
},
appReady: {
type: "final"
}
}
});
// export interface AuthMachineContext {
// isAppReady: boolean;
// shouldDisplaySecurityHint: boolean;
// userSecurityPreference: TheoUserSecurityPreference | null;
// userSessionIsActive: boolean;
// };
// export type AuthMachineEvent =
// | { type: "SECURITY_PREF_FETCH_SUCCESS", preference: TheoUserSecurityPreference }
// | { type: "SECURITY_PREF_FETCH_FAILURE" }
// | { type: "ACTIVE_SESSION" }
// | { type: "NO_ACTIVE_SESSION" }
// | { type: "BOOT_COMPLETE" };
// interface AuthMachineSchema {
// states: {
// booting: {},
// authorising: {}
// loggingIn: {},
// additionalSecurity: {},
// authenticationSuccessful: {}
// }
// };
// export const authMachine = Machine<AuthMachineContext, AuthMachineEvent, AuthMachineSchema>({
const authMachine = Machine({
id: "auth",
initial: "booting",
context: {
isAppReady: false,
shouldDisplaySecurityHint: false,
userSecurityPreference: null,
userSessionIsActive: false,
},
states: {
booting: {
invoke: {
id: "boot",
src: bootMachine
},
on: {
SECURITY_PREF_FETCH_SUCCESS: {
actions: assign({ userSecurityPreference: (_context, event) => event.preference })
},
SECURITY_PREF_FETCH_FAILURE: {
actions: assign({ userSecurityPreference: "NONE", displaySetSecurityHint: true })
},
ACTIVE_SESSION: {
actions: assign({ userSessionIsActive: true })
},
NO_ACTIVE_SESSION: {
actions: assign({ userSessionIsActive: false })
},
BOOT_COMPLETE: {
target: "authorising",
actions: assign({ isAppReady: true })
}
},
},
authorising: {
on: {
"": [
// { target: "loggingIn", cond: (context: AuthMachineContext) => context.isAppReady && !context.userSessionIsActive },
{ target: "loggingIn", cond: (context) => context.isAppReady && !context.userSessionIsActive },
// { target: "additionalSecurity", cond: (context: AuthMachineContext) => context.isAppReady && context.userSessionIsActive && (context.userSecurityPreference === TheoUserSecurityPreference.PIN || context.userSecurityPreference === TheoUserSecurityPreference.BIOMETRIC) },
{ target: "additionalSecurity", cond: (context) => context.isAppReady && context.userSessionIsActive && (context.userSecurityPreference === TheoUserSecurityPreference.PIN || context.userSecurityPreference === TheoUserSecurityPreference.BIOMETRIC) },
// { target: "securityHint", cond: (context: AuthMachineContext) => context.isAppReady && context.userSessionIsActive && context.shouldDisplaySecurityHint }
{ target: "securityHint", cond: (context) => context.isAppReady && context.userSessionIsActive && context.shouldDisplaySecurityHint }
]
}
},
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