Authentication Tokens
An authentication token is a string the server hands to a client after a successful login. The client sends that string back with later requests to prove who it is, so the user does not have to type a password on every call. Tokens are the usual way single-page apps built with React talk to an Express API, because the API can stay stateless and does not need to remember a session for each visitor.
Sessions compared with tokens
In classic session login, the server stores a session record and gives the browser a cookie holding only a random session ID. Each request, the server looks up that ID to find the user. With a token approach, the useful information travels inside the token itself, and the server only has to check that the token is genuine and not expired. That makes it easier to run several API servers, or to serve a mobile app and a web app from the same backend.
Inside a JSON Web Token
The most common token format is the JSON Web Token, or JWT. It has three parts separated by dots: a header, a payload and a signature. The header and payload are JSON objects encoded with Base64URL. The payload carries "claims" such as the user ID (sub), an issued-at time (iat) and an expiry time (exp). The signature is produced from the first two parts and a secret key known only to the server.
When a token comes back, the server recomputes the signature. If anyone changed even one character of the payload, the signatures will not match and the token is rejected.
const jwt = require('jsonwebtoken');
// After checking the password
const token = jwt.sign(
{ sub: user.id, role: user.role },
process.env.JWT_SECRET,
{ expiresIn: '1h' }
);
// Later, when a request arrives
try {
const claims = jwt.verify(token, process.env.JWT_SECRET);
console.log('Request from user', claims.sub);
} catch (err) {
console.log('Invalid or expired token');
}
Signed is not the same as secret
A frequent misunderstanding is believing a JWT hides its contents. It does not. Base64URL is an encoding, not encryption, so anyone holding the token can paste it into a decoder and read the payload. Never put passwords, card numbers or other private details in a token. Include only what the server needs to identify the user and make permission decisions.
Another mistake is hard-coding the signing secret in source code. Keep it in an environment variable, make it long and random, and never ship it to the browser.
Expiry and logging out
Because the server does not store tokens, it cannot simply delete one to log a user out. The usual answer is a short expiry, often minutes to an hour, so a stolen token stops working soon. Many apps pair a short-lived access token with a longer-lived refresh token, kept in an httpOnly cookie, that the client uses to obtain new access tokens. Logging out then means discarding the access token on the client and revoking the refresh token on the server.
What to keep in mind
- A token is proof of a past login that the client presents on each request.
- JWTs are signed to prevent tampering, but their payload is readable.
- Keep the secret on the server and give tokens a sensible expiry.
Ready to master real-world web development?
Learn HTML, CSS3, Bootstrap and JavaScript hands-on with mentor-led sessions.