Eight-Step User Authentication with JWT

This article explains an eight‑step JWT‑based user authentication flow, covering secure credential transmission, database verification, token creation, HttpOnly cookie handling, request validation, payload decoding, session versus JWT comparison, and cross‑domain single sign‑on configuration.

Top Architect
Top Architect
Top Architect
Eight-Step User Authentication with JWT

User authentication (Authentication) is the mechanism that allows users to log in and access a website without re‑entering credentials for a period of time.

First, the client sends the username and password via an HTTPS POST request to the server.

The server checks the credentials against the database.

When verification succeeds, the server creates a JWT that includes the user's id (or user_id) as a payload claim, Base64‑encodes the header and payload, signs them, and produces a token string such as lll.zzz.xxx.

The JWT is returned to the client as part of an HttpOnly cookie to prevent JavaScript access and mitigate XSS attacks.

For each subsequent request before the cookie expires, the client automatically sends the JWT cookie; the server extracts the token from the request.

The server validates the JWT by checking its signature, expiration time, and optionally its audience.

After successful validation, the server decodes the payload, reads the user_id, retrieves the corresponding user record from the database, and initializes any necessary ORM or business logic.

Compared with traditional session storage, which consumes server memory and often requires external key‑value stores, JWT stores state on the client, reducing server memory pressure and allowing additional claims such as roles or permissions.

For single sign‑on across multiple subdomains, sessions require synchronization, whereas JWT can be shared by setting the cookie's domain attribute to the top‑level domain (e.g., .taobao.com), enabling all subdomains to receive the token.

Set-Cookie: jwt=lll.zzz.xxx; HttpOnly; max-age=980000; domain=.taobao.com
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

AuthenticationJWTcookiesSingle Sign-On
Top Architect
Written by

Top Architect

Top Architect focuses on sharing practical architecture knowledge, covering enterprise, system, website, large‑scale distributed, and high‑availability architectures, plus architecture adjustments using internet technologies. We welcome idea‑driven, sharing‑oriented architects to exchange and learn together.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.