Examples
Auth Flow
Login, token refresh with countdown, constraint ordering, and session management.
Try it
Click “Sign In” to authenticate. Watch the token countdown and auto-refresh. Use “Force Expire” or adjust fail rates to explore error handling.
How it works
An authentication flow with token refresh, constraint ordering, and session management – all driven by Directive’s constraint–resolver pattern.
- Facts –
token,refreshToken,expiresAt,user,status, and a tickingnowfact updated every 1s - Derivations –
isExpiringSoonauto-tracksnowandexpiresAt, driving therefreshNeededconstraint reactively - Constraints –
refreshNeeded(priority 90) fires when the token is expiring soon.needsUser(priority 80) usesafter: ['refreshNeeded']to ensure the user profile is fetched with a fresh token - Resolvers –
loginhandles authentication,refreshTokenretries with exponential backoff,fetchUserloads the user profile - Effects –
logStatusChangerecords status transitions to the event timeline for observability
Summary
What: An authentication flow with login, automatic token refresh, user profile fetching, and logout – all with configurable failure rates and token lifetimes.
How: A ticking now fact drives isExpiringSoon, which triggers refreshNeeded automatically. The after ordering on needsUser ensures the user profile is always fetched with a valid token.
Why it works: Auth flows are full of timing-dependent, ordered operations. Directive’s constraint ordering (after) and auto-tracked derivations eliminate manual timers and race conditions.
Source code
// Source file "auth-flow.ts" not found

