Skip to content

Instantly share code, notes, and snippets.

@damooo
Last active December 29, 2019 07:54
Show Gist options
  • Save damooo/d14401e51f0ee59934b65bd6a32d69b1 to your computer and use it in GitHub Desktop.
Save damooo/d14401e51f0ee59934b65bd6a32d69b1 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
const AuthFSM = Machine({
id: 'AuthMachine',
initial: 'init',
context: {},
states: {
init: {
on: {
INIT_AUTH: 'authorized',
INIT_UNAUTH: 'unauthorized',
},
},
unauthorized: {
on: {
CMD_AUTH: {
target: 'authorizing',
actions: ['initAuthFlow'],
},
SYNC_AUTH: {
target: 'authorized',
},
},
},
authorizing: {
on: {
CMD_AUTH: {
actions: ['clearPrevAuthFlow', 'initAuthFlow'],
},
SYNC_AUTHORIZED: {
target: 'authorized',
},
AUTH_SUCCESS: {
target: 'authorized',
actions: ['notifyAuthorized'],
},
AUTH_FAILURE: {
target: 'unauthorized',
},
},
},
authorized: {
on: {
CMD_UNAUTH: {
target: 'unauthorizing',
actions: ['initUnAuthFlow'],
},
SYNC_REFRESHED: {
},
SYNC_UNAUTHORIZED: {
target: 'unauthorized',
},
EXPIRE_THRESHOULD_REACHED: {
actions: ['refresh'],
},
REFRESH_SUCCESS: { // expiry preventive refresh.
actions: ['notifyRefreshed'],
},
EXPIRED: {
target: 'expired',
},
},
},
unauthorizing: {
on: {
CMD_UNAUTH: {
actions: ['clearPrevUnAuthFlow', 'initUnAuthFlow'],
},
SYNC_UNAUTHORIZED: {
target: 'unauthorized',
actions: ['clearPrevUnAuthFlow'],
},
UNAUTH_SUCCESS: {
target: 'unauthorized',
actions: ['notifyUnAuthorized'],
},
UNAUTH_FAILURE: {
target: 'authorized', // NOTE!! possible that he may came here from 'expired' state instead
},
},
},
expired: {
entry: ['initRefreshFlow'],
on: {
CMD_UNAUTH: {
target: 'unauthorizing',
actions: ['initUnAuthFlow'],
},
SYNC_UNAUTHORIZED: {
target: 'unauthorized',
},
SYNC_REFRESHED: {
target: 'authorized',
},
CMD_REFRESH: {
actions: ['initRefreshFlow'], // is an inPage op.
},
REFRESH_SUCCESS: {
target: 'authorized',
actions: ['notifyRefreshed'],
},
REFRESH_FAILURE: {
actions: ['initRefreshFlow'],
},
CLEAR: {
target: 'unauthorized',
actions: ['clearOldSession'],
},
},
},
},
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment