Skip to content

Instantly share code, notes, and snippets.

@xomaczar
Last active May 27, 2021 22:23
Show Gist options
  • Save xomaczar/32713df9946fc650dba9c23fcb3671bf to your computer and use it in GitHub Desktop.
Save xomaczar/32713df9946fc650dba9c23fcb3671bf to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
const jwtTokenStates = {
new: {
onEntry: [
'start activity service',
't = jwt.exp - jwt.iat - cfg.delta',
'setInterval(t, VERIFY)'
],
onExit: [
'cancel interval',
'stop activity service'
],
on: {
LOGOUT: 'expired',
VERIFY: 'pending_renew',
},
},
pending_renew: {
on: {
'': [
{ target: 'renew', cond: 'isUserRecentlyActive' },
{ target: 'last-ditch' }
],
},
},
'last-ditch': {
onEntry: [
'start countdownTask'
],
onExit: [
'stop countdownTask'
],
on: {
COUNTDOWN_COMPLETE: {
target: 'expired',
},
RENEW: {
target: 'renew',
},
LOGOUT: {
target: 'expired',
},
}
},
renew: {
onEntry: 'refreshToken',
on: {
SUCCESS: 'new',
ERROR: 'expired'
}
},
expired: {
onEntry: ['session.invalidate', 'app.reload'],
actions: assign({
validToken: ({ context }) => (context.validToken = false),
}),
},
};
const aMachine = Machine({
id: 'jwt-token-lifetime',
initial: 'new',
context: {
userIsActive: true
// lastUserActivityDateTime: Date.now(),
},
states: jwtTokenStates,
},
{
actions: {
},
guards: {
isUserRecentlyActive(context) {
// check the context lastUserActivity which was continuesly being set
// by the @service userAcitivity
return context.userIsActive;
}
},
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment