Last active
December 21, 2020 14:30
-
-
Save herschel666/969285b15bd5300d966e4cc9f91dab51 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
This file contains hidden or 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 LOGGED_IN = 'LOGGED_IN'; | |
const LOGGED_OUT = 'LOGGED_OUT'; | |
const SOFT_LOGGED_IN = 'SOFT_LOGGED_IN'; | |
const res = { locals: {} }; | |
const fetchMachine = Machine( | |
{ | |
id: 'authentication', | |
initial: 'idle', | |
context: { | |
allowLoggedOut: true, | |
allowSoftLoggedIn: false, | |
cookieValue: null, | |
id: null, | |
loginState: null, | |
language: null, | |
}, | |
states: { | |
idle: { | |
on: { | |
HAS_COOKIE: { | |
target: 'fetchLoginState', | |
action: 'storeCookie', | |
}, | |
NO_COOKIE: [ | |
{ | |
target: 'redirectToLoginApp', | |
cond: 'loggedInRequired', | |
}, | |
{ | |
target: 'proceed', | |
cond: 'loggedOutAllowed', | |
}, | |
], | |
}, | |
}, | |
fetchLoginState: { | |
invoke: { | |
id: 'get-login-state', | |
src: 'fetchLoginState', | |
onDone: [ | |
{ | |
target: '.retrievedLoginState', | |
action: 'storeUserData', | |
}, | |
], | |
}, | |
states: { | |
retrievedLoginState: { | |
on: { | |
'': [ | |
{ | |
target: '.loggedIn', | |
cond: 'isLoggedIn', | |
}, | |
{ | |
target: '.validateSoftLoggedIn', | |
cond: 'isSoftLoggedIn', | |
}, | |
{ | |
target: '#authentication.retry', | |
cond: 'isLoggedOut', | |
}, | |
], | |
}, | |
states: { | |
loggedIn: [ | |
{ | |
target: '#authentication.proceed', | |
action: 'assignUserDataToResponse', | |
}, | |
], | |
validateSoftLoggedIn: [ | |
{ | |
cond: 'softLoggedInAllowed', | |
target: '#authentication.proceed', | |
action: 'assignUserDataToResponse', | |
}, | |
{ | |
cond: 'softLoggedInForbidden', | |
target: '#authentication.redirectToLoginApp', | |
}, | |
], | |
}, | |
}, | |
}, | |
}, | |
redirectToLoginApp: { | |
final: true, | |
}, | |
proceed: { | |
final: true, | |
}, | |
retry: { | |
final: true, | |
}, | |
}, | |
}, | |
{ | |
guards: { | |
loggedInRequired: (context) => context.allowLoggedOut === false, | |
loggedOutAllowed: (context) => context.allowLoggedOut === true, | |
softLoggedInForbidden: (context) => context.allowSoftLoggedIn === false, | |
isLoggedIn: (context) => context.loginState === LOGGED_IN, | |
isLoggedOut: (context) => context.loginState === LOGGED_OUT, | |
isSoftLoggedIn: (context) => context.loginState === SOFT_LOGGED_IN, | |
}, | |
actions: { | |
storeCookie: assign({ cookieValue: (_, event) => event.cookieValue }), | |
storeUserData: assign({ | |
id: (_, event) => event.id, | |
loginState: (_, event) => event.loginState, | |
language: (_, event) => event.language, | |
}), | |
assignUserDataToResponse: (context) => | |
Object.assign(res.locals, { | |
id: context.id, | |
loginState: context.loginState, | |
authenticated: context.loginState === LOGGED_IN, | |
language: context.language, | |
}), | |
}, | |
services: { | |
fetchLoginState: { | |
src: (context) => { | |
// fetch login state with `context.cookieValue`... | |
// return `<loginState>`, `<language>` & `<id>`... | |
}, | |
}, | |
}, | |
} | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment