Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Available variables:
- // - Machine
- // - interpret
- // - assign
- // - send
- // - sendParent
- // - spawn
- // - raise
- // - actions
- // - XState (all XState exports)
- const probeConfig = {
- id: 'probe',
- src: 'probeService',
- onDone: [
- { target: '#acs.authorized', cond: 'isAuthorized' },
- { target: '#acs.unauthorized' },
- ],
- onError: { target: '#acs.error', actions: ['setError'] },
- };
- const tokenConfig = Object.assign({}, probeConfig, { src: 'tokenService' });
- const config = {
- id: 'acs',
- initial: 'unknown',
- meta: {
- version: '0.0.0',
- },
- context: {
- error: null,
- },
- on: {
- login: [
- { target: 'exit', in: 'unknown' },
- { target: 'exit', in: 'unauthorized' },
- { target: 'exit', in: 'error' },
- ],
- logout: [{ target: 'exit', in: 'authorized' }],
- },
- states: {
- unknown: {
- initial: 'pending',
- states: {
- pending: {
- invoke: probeConfig,
- },
- },
- },
- authorized: {
- initial: 'idle',
- states: {
- idle: {
- on: { probe: 'pending' },
- },
- pending: {
- invoke: tokenConfig,
- },
- },
- },
- unauthorized: {
- initial: 'idle',
- states: {
- idle: {
- on: { probe: 'pending' },
- },
- pending: {
- invoke: tokenConfig,
- },
- },
- },
- exit: {
- type: 'final',
- entry: ['redirect'],
- },
- error: {
- entry: ['setError'],
- on: {
- probe: 'unknown.pending',
- },
- },
- },
- };
- const auth = Machine(config);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement