<< back to Guides
π Deep Dive into Authentication
π§ What Is Authentication?
Authentication is the process of verifying who a user is before granting access to a system.
It answers: βIs this user who they claim to be?β
(Authorization comes after authentication.)
π§ Cookie-Based Authentication
How It Works:
- User logs in β credentials are verified.
- Server creates a session and stores it in memory/store.
- A session ID is returned to the client as a cookie.
- Browser sends cookie with each request.
- Server retrieves session and authenticates the user.
β Pros:
- Simple and secure when using HTTPS.
- Built-in browser support.
β οΈ Cons:
- Requires session storage on the server.
- CSRF protection needed.
- Harder to scale in distributed systems.
# Laravel session-based login (simplified)
Auth::attempt([
'email' => $request->email,
'password' => $request->password,
]);
π Token-Based Authentication (Stateless)
JSON Web Tokens (JWT)
JWTs are self-contained tokens that carry user info and are signed.
How It Works:
- User logs in β server returns a JWT.
- Client stores it (e.g. localStorage or HTTP-only cookie).
- Token is sent in every request (usually
Authorization
header). - Server verifies the signature and grants access.
JWT Structure:
A JWT consists of three parts separated by dots:
<HEADER>.<PAYLOAD>.<SIGNATURE>
Each part is Base64URL-encoded:
- Header: algorithm & token type
- Payload: user claims (e.g. user ID, expiration)
- Signature: used to verify integrity
// Example decoded payload
{
"sub": "1234567890",
"name": "John Doe",
"iat": 1516239022
}
β Pros:
- Stateless and scalable
- Cross-domain capable
- Works well for APIs and SPAs
β οΈ Cons:
- Cannot revoke easily unless using a token blacklist
- Susceptible to XSS if stored in
localStorage
instead of secure cookies
JWT Examples:
// Java JWT verification (using jjwt)
Claims claims = Jwts.parser()
.setSigningKey(secretKey)
.parseClaimsJws(token)
.getBody();
// Laravel - creating a JWT (with tymondesigns/jwt-auth)
$token = Auth::attempt(['email' => $email, 'password' => $password]);
// Send token back to client
π PASETO (Platform-Agnostic SEcurity TOkens)
A modern alternative to JWT with safer cryptographic defaults.
Features:
- No "none" algorithm confusion
- Simpler and safer cryptographic design
- 2 token formats: local (symmetric) & public (asymmetric)
PASETO Example:
// Node.js with paseto package
const { V2 } = require('paseto');
const token = await V2.encrypt({ userId: 1 }, secretKey);
β Pros:
- Secure defaults, no algorithm confusion
- Easier to audit
β οΈ Cons:
- Not as widely adopted yet
- Limited library support in some languages
π§Ύ Other Auth Methods
π OAuth2 + OpenID Connect
- For third-party login (Google, GitHub, etc.)
- Common for mobile/web apps needing federated identity
π SAML
- XML-based auth, mainly used in enterprise SSO
- Heavyweight and harder to debug than modern alternatives
π API Keys
- Simple and easy to manage
- No user identity, good for service-to-service, but less secure if not scoped and rate-limited
ποΈ Session vs JWT Comparison
Feature | Sessions | JWT |
---|---|---|
Storage | Server-side | Client-side |
Stateless | β | β |
Revocable | β | β (unless tracked) |
CSRF Protection | β | Optional |
Susceptible to XSS | β if cookie not secure | β if stored in localStorage |
π§ Choosing the Right Auth Method
Use Case | Recommended Auth |
---|---|
Web App (monolith) | Cookies + Session |
SPA / Public API | JWT or OAuth2 |
Microservices | JWT or PASETO (internal tokens) |
Enterprise / SSO | SAML or OpenID Connect |
Backend-for-Frontend (BFF) | JWT or Session in secure cookie |
π Best Practices
- Always use HTTPS
- Store JWTs in HTTP-only cookies if possible
- Keep token lifetime short and rotate refresh tokens
- Avoid placing sensitive info in payloads
- Use secure, audited libraries (e.g.,
paseto
,jjwt
, etc.) - Avoid black-box implementations of crypto logic
- Prefer PASETO if you're concerned with JWT complexity