Application State Machine — Mermaid State Diagram
State diagram documenting a web application's state machine with loading, authenticated, and error states.
What this diagram shows
This Mermaid state diagram is a signed-in web app. Initializing either finds no session and opens the unauthenticated composite state, or finds a session and moves to Loading. Loading becomes Authenticated if the session is valid, or Unauthenticated if it has expired. Inside Unauthenticated the user can submit the login form, fail and stay on the form, get sent to MFA, or open forgot-password and return. Authenticated contains Dashboard, Profile, and Settings. Logout and an expired token leave for Unauthenticated. An API failure enters Error, which can retry back to Authenticated or give up and log the user out.
Use it when UI state and server session are getting mixed in component flags. The composite states are the two modes of the shell. Profile and Settings are inside Authenticated, so they cannot appear on the logged-out tree. MFA is inside Unauthenticated, which means a half-finished login is not yet a session.
Error is outside both composites. The retry edge returns to Authenticated, so this template treats the failure as happening to a signed-in user. A failed login stays in the form via the invalid-credentials edge and does not use Error.
Mermaid syntax in this template
stateDiagram-v2 with two composite states. The top-level [*] enters Initializing. Inner [*] nodes start each composite.
Initializing --> Unauthenticated : No session foundThe boot branch. The other edge is Loading when a session cookie exists.
Submitting --> MFA : MFA requiredMFA is a state inside the logged-out composite. Verify code returns to Submitting.
Authenticated --> SessionExpired : Token expiredSessionExpired then redirects to Unauthenticated. It is not the same node as Logout.
Error --> Authenticated : Retry successThe recovery edge. Unrecoverable leaves Error for Unauthenticated.
Variations and ways to customize it
Treat MFA as signed-in
If the server already issued a limited session before MFA, move the MFA state inside Authenticated and send a failed code to logout. The template keeps MFA logged out.
state Authenticated {
[*] --> MFA
MFA --> Dashboard : Code accepted
}Add a locked account
Invalid credentials currently returns to the form forever. A lockout state after several failures should not offer Submit again.
Submitting --> Locked : Too many attempts
Locked --> ForgotPassword : Reset requiredSeparate network errors from auth errors
Error currently covers API failure for a signed-in user. A second state for offline lets you retry without the unrecoverable logout edge.
Diagram type
State DiagramTags
Frequently asked questions
When does the app show the login form?
If initializing finds no session, or if loading decides the session expired, or after logout, or when an error is unrecoverable. The form lives inside the Unauthenticated composite state.
Is MFA a logged-in state?
No. MFA is inside Unauthenticated, reached from Submitting, and a successful code returns to Submitting. The transition into Authenticated is Login success on the outer state.
How are expired tokens different from logout?
Logout goes directly from Authenticated to Unauthenticated. Token expiry goes to SessionExpired and then redirects to login. The extra state is where you can show a specific message.
What does the Error state recover to?
Retry success returns to Authenticated. Unrecoverable returns to Unauthenticated. A bad password does not use Error; it stays on the login form.
Related templates and guides
All Documentation templatesError Handling Strategy
Flowchart documenting an application's error handling strategy with retry logic, fallbacks, and alerting.
REST API Architecture
Flowchart documenting a REST API architecture with middleware layers, controllers, services, and data access.
Technology Stack Overview
Mind map documenting a full-stack technology stack with frontend, backend, DevOps, and monitoring layers.
Mermaid state diagram syntax
Composite states for the logged-out form and the signed-in shell.
Getting started with Mermaid
How stateDiagram-v2 differs from a flowchart of the same screens.