Skip to content

Instantly share code, notes, and snippets.

@felippenardi
Created July 23, 2019 22:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save felippenardi/37c5017b31dcb85bd5a1ab7949483bed to your computer and use it in GitHub Desktop.
Save felippenardi/37c5017b31dcb85bd5a1ab7949483bed to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
const twoFactorMachine = {
two_factor: {
id: 'two_factor',
onEntry: ['checkTwoFactorSettings'],
initial: 'unknown',
states: {
unknown: {
on: {
'': [
{ target: 'setup', cond: 'needsToSetupTwoFactor' },
{
target: 'validate_sms',
cond: 'hasSmsTwoFactor',
},
{ target: 'validate_app' },
],
},
},
setup: {
id: 'two_factor_setup',
initial: 'options_screen',
states: {
options_screen: {
on: {
CHOOSE_SMS: 'add_phone',
CHOOSE_APP: 'get_app',
},
},
add_phone: {
on: {
SMS_SET_UP: '#validate_sms',
},
},
qrcode: {
on: {
APP_SET_UP: '#validate_app',
},
},
get_app: {
on: {
CONTINUE: 'qrcode',
},
},
},
},
validate_app: {
id: 'validate_app',
on: {
APP_VERIFICATION_DONE: '#redirecting',
},
},
validate_sms: {
id: 'validate_sms',
on: {
SMS_VERIFICATION_DONE: '#redirecting',
},
},
},
},
}
const twoFactorGuards = {
needsToSetupTwoFactor: ({ userHasTwoFactor } = {}) => null,
hasSmsTwoFactor: () => null,
}
const twoFactorActions = {
checkTwoFactorSettings: assign(() => {
return {
userHasTwoFactor: null,
twoFactorType: null,
}
}),
}
const loginMachine = Machine(
{
id: 'loginMachine',
initial: 'unknown',
states: {
unknown: {
on: {
'': [
{
target: 'logged_user_redirect',
cond: 'isUserLoggedIn',
},
{
target: 'oauth_redirecting',
cond: 'hasOnlyOauth',
},
{
target: 'saml_redirecting',
cond: 'hasOnlySaml',
},
{
target: 'identification.identified_users',
cond: 'isUserIdentified',
},
{ target: 'identification' },
],
},
},
logged_user_redirect: {
},
saml_redirecting: {
id: 'saml_redirecting',
},
oauth_redirecting: {
id: 'oauth_redirecting',
on: {
OAUTH_LOGIN_FAILURE: '#identification',
},
},
user_managing: {
id: 'user_managing',
on: {
EDITING_DONE: [
{
target: '#unidentified_user',
cond: 'hasNoIdentifiedUsers',
},
{ target: '#identified_users' },
],
},
},
identification: {
id: 'identification',
on: {
OAUTH_LOGIN: '#oauth_redirecting',
SAML_LOGIN: '#saml_redirecting',
TOKEN_PREFERENCE: 'token_login',
PASSWORD_PREFERENCE: 'password_login',
FIRST_LOGIN: 'default_login',
},
initial: 'unidentified_user',
states: {
identified_users: {
id: 'identified_users',
on: {
NOT_ME: '#unidentified_user',
MANAGE_USERS: '#user_managing',
},
},
unidentified_user: {
id: 'unidentified_user',
on: {},
},
},
},
password_login: {
on: {
FORGOT_PASSWORD: 'default_login',
BACK: 'identification',
PASSWORD_EXPIRED: 'update_expired_password',
LOGGED_WITH_PASSWORD: 'redirecting',
TWO_FACTOR_REQUIRED: 'two_factor',
},
},
token_login: {
on: {
CHANGE_EMAIL: 'identification',
LOGGED_WITH_TOKEN: 'redirecting',
SWITCH_TO_PASSWORD: '#set_password',
},
},
default_login: {
id: 'default_login',
initial: 'token_confirmation',
states: {
token_confirmation: {
on: {
TOKEN_CONFIRMED: 'choose_action',
CHANGE_EMAIL: '#identification',
},
},
choose_action: {
on: {
CHOOSE_CONTINUE_WITH_TOKEN: '#redirecting',
CHOOSE_CONTINUE_WITH_PASSWORD: '#set_password',
},
},
},
},
...twoFactorMachine,
update_expired_password: {
on: {
PASSWORD_UPDATED: 'redirecting',
TWO_FACTOR_REQUIRED: 'two_factor',
},
},
set_password: {
id: 'set_password',
on: {
PASSWORD_SET: {
target: '#redirecting',
},
},
},
redirecting: {
id: 'redirecting',
type: 'final',
},
},
},
{
actions: {
...twoFactorActions,
},
guards: {
...twoFactorGuards,
isUserLoggedIn: ({ isUserAuthenticated } = {}) => isUserAuthenticated,
isUserIdentified: ({ isUserIdentified } = {}) => isUserIdentified,
hasOnlyOauth: ({
identityProviders: {
samlProviders = [],
googleOAuth,
password,
accessKey,
} = {},
} = {}) => {
return (
!password && !accessKey && samlProviders.length === 0 && googleOAuth
)
},
hasOnlySaml: ({
identityProviders: {
samlProviders = [],
googleOAuth,
password,
accessKey,
} = {},
} = {}) => {
return (
!password && !accessKey && samlProviders.length > 0 && !googleOAuth
)
},
hasNoIdentifiedUsers: () =>
null,
},
}
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment