Login Sequence Diagram

A user types an email and password and presses Log in. Four parts of the system take turns: the browser, the web application, a separate authentication service and the user database. This page shows that conversation as a UML sequence diagram, with the happy path and the failed-login path side by side in one alt fragment.

Read it top to bottom: time flows downward, and each arrow is one message.


1. The Diagram

Login: success and failure paths browser : Browser web : WebApp auth : AuthService db : UserDB alt [password valid] [invalid password or unknown email] POST /login {email, password} authenticate(email, password) find user by email user row (pw_hash) verify Argon2id hash ok(userId, sessionToken) 302 + Set-Cookie sid (HttpOnly) increment failed_attempts ok 401 invalid credentials 401 + generic error message
  1. The browser sends POST /login with the email and password over HTTPS. The web app becomes active (the tall bar on its lifeline).
  2. The web app does not check passwords itself; it calls authenticate() on the auth service.
  3. The auth service loads the user row by email and gets back the stored password hash.
  4. It verifies the password against the hash with a slow hash such as Argon2id. This is a self call, so the arrow loops back to the same lifeline.
  5. The alt box splits the story. Valid: the auth service returns a session token and the web app answers 302 with an HttpOnly, Secure session cookie.
  6. Invalid: the auth service bumps failed_attempts, returns 401, and the web app shows one generic message. It never says whether the email or the password was wrong, so attackers cannot probe for accounts.

2. How to Read It

Notation used name : Type lifeline = one participant; time runs downward bar = activation (participant is busy) synchronous call reply / return self call alt [condition] [else] alt fragment: exactly one branch runs

A solid line with a filled arrowhead is a synchronous call: the sender waits. A dashed line with an open arrowhead is the reply. Colours on this site are only emphasis: green for the success branch, red for the error branch; UML itself has no colours.

3. Variant: Login with MFA and Lockout

Variant: TOTP second factor browser : Browser web : WebApp auth : AuthService POST /login {email, password} authenticate(email, password) mfa_required(challengeId) 200 show 6-digit code form POST /login/mfa {code} verifyTotp(challengeId, code) alt [code valid] ok(sessionToken) 302 + Set-Cookie sid [5th wrong code] 423 locked (15 min)

Adding multi-factor authentication turns one round trip into two. After the password check, the auth service replies mfa_required with a challenge id; the browser then posts the six-digit TOTP code. Only a valid code produces the session cookie. After five wrong codes the account is locked for 15 minutes (HTTP 423 Locked). The user DB is left out here to keep the diagram focused; leaving out detail that does not matter to the reader is normal and good practice.

4. Pros and Cons

Pros

Cons

5. When to Use It

Use it when

Skip it when

6. As Diagrams-as-Code

The main diagram written for Mermaid:

sequenceDiagram
    participant B as Browser
    participant W as WebApp
    participant A as AuthService
    participant D as UserDB
    B->>W: POST /login {email, password}
    activate W
    W->>A: authenticate(email, password)
    A->>D: find user by email
    D-->>A: user row (pw_hash)
    A->>A: verify Argon2id hash
    alt password valid
        A-->>W: ok(userId, sessionToken)
        W-->>B: 302 + Set-Cookie sid (HttpOnly)
    else invalid password or unknown email
        A->>D: increment failed_attempts
        D-->>A: ok
        A-->>W: 401 invalid credentials
        W-->>B: 401 + generic error message
    end
    deactivate W

Paste it into any Markdown viewer that renders Mermaid (GitHub, GitLab, Obsidian). PlantUML uses almost the same arrows inside @startuml ... @enduml.