Unsaved
Ready
Loading Monaco Editor...
Preview
Styling
Sequence Diagram
All Software Architecture templates

User Authentication Flow — Mermaid Sequence Diagram

Sequence diagram showing OAuth 2.0 login flow with JWT token exchange between client, auth server, and resource API.

What this diagram shows

This Mermaid sequence diagram follows one person from the Sign in with Google button through to a loaded dashboard. The browser sends GET /authorize to AuthServer, Google returns an authorization code, AuthServer exchanges that code for tokens, upserts the user, and sets an HTTP-only cookie plus a JWT. The browser then calls GET /api/me with Authorization: Bearer, and the API verifies the signature before it returns the profile JSON.

Use it when you are documenting a hosted-login integration for a web app and the audience needs the handoff between the browser, the auth server, and the resource API. It is the diagram to pull up in a security review when someone asks where the token is stored and which hop checks the signature. It assumes the authorization-code flow, not an implicit flow and not a first-party password form.

The participants are User, Browser, AuthServer, API, and DB. The DB hop is only the upsert of the user record. Token verification stays inside the API, which is the split you want if the resource server does not call the auth server on every request.

Mermaid syntax in this template

The diagram is a sequenceDiagram. Aliases are unnecessary because each participant name is already a readable label. Solid arrows are requests. Dashed arrows are responses.

  • participant API

    Declares a lifeline. Order of participant lines is left-to-right order on the diagram, so User stays on the left and DB stays on the right.

  • Browser->>AuthServer: GET /authorize

    ->> is a solid arrow with an arrowhead. Put the HTTP method and path in the message text so the diagram reads like a trace.

  • DB-->>AuthServer: User profile

    -->> is the dashed return. Use it for data coming back (the profile, the cookie, the JSON) and keep ->> for the call that started the hop.

  • AuthServer->>AuthServer: Exchange code

    A self-message draws a loop on one lifeline. That is how this template shows work that never leaves the auth server, such as swapping the code for tokens.

Variations and ways to customize it

Add a refresh-token hop

After the dashboard load, show the API rejecting an expired JWT and the browser silently exchanging the refresh cookie. A self-message on the browser is the wrong place; the refresh call goes back to AuthServer.

Browser->>API: GET /api/me
API-->>Browser: 401 token expired
Browser->>AuthServer: POST /token (refresh cookie)
AuthServer-->>Browser: New JWT

Show a denied consent screen

Wrap the callback in an alt block so reviewers can see the cancel path. The template currently only draws the success path, which hides the case where Google returns an error query parameter.

alt User grants consent
    Browser->>AuthServer: Authorization code callback
else User cancels
    Browser->>AuthServer: ?error=access_denied
    AuthServer-->>Browser: Redirect to /login?error=denied
end

Rename the provider

Replace Google in the button label and in the redirect message with the provider you actually use. The rest of the sequence stays valid for any OAuth authorization-code provider, including a first-party OIDC server.

Diagram type

Sequence Diagram

Tags

authOAuthJWTloginsecurity
Open in Editor

Frequently asked questions

Where is the JWT created in this Mermaid sequence diagram?

AuthServer creates it after exchanging the authorization code and upserting the user. The message back to the browser is Set HTTP-only cookie + JWT. The API later verifies that signature on GET /api/me and does not call AuthServer again.

Does this OAuth diagram include the refresh token?

No. It stops at the first successful profile load. Add a 401 from the API and a POST /token from the browser if you need to show refresh. Keep the refresh token in the HTTP-only cookie, which is already how the template delivers the first token.

How do I show a failed login on this Mermaid sequence diagram?

Add an alt/else around the callback. One branch is the authorization code. The other is error=access_denied or an invalid code. End the failure branch with a redirect to the login page so the diagram still has a terminal message.

Can I paste this sequence diagram into a README?

Yes. Copy the Mermaid source from the editor and wrap it in a ```mermaid fence. GitHub, GitLab, and VS Code markdown preview render sequenceDiagram blocks. The participant names and arrow text are the part worth customizing first.

Related templates and guides

All Software Architecture templates