# Sessions vs JWTs: Authentication Design for Interviews

"How will users stay logged in?" is a common sub-question. Both main approaches are fine; the trade-offs are what interviewers want.

## Server-side sessions

After login the server creates a random session ID, stores `session_id -> user, expiry` in a store such as Redis, and sets it as a cookie (`HttpOnly; Secure; SameSite=Lax`).

- Revocation is easy: delete the session.
- Every request needs a lookup, which a fast shared store handles well.
- Works naturally with browsers.

## JWTs (signed tokens)

The server issues a token containing claims (`sub`, `exp`, scopes) signed with a private key. Any service with the public key can verify it without a database call.

- Good for many services and third-party APIs; verification is local and fast.
- Hard to revoke before expiry. The common fix: short-lived **access tokens** (5-15 minutes) plus a long-lived **refresh token** stored server-side that can be revoked.
- Tokens carry data, so keep them small and never put secrets inside (they are signed, not encrypted).

## OAuth 2.0 and OIDC in one paragraph

For "log in with Google" or third-party access, use the **authorization code flow with PKCE**: the app redirects to the identity provider, gets a one-time code back, and exchanges it for tokens. OIDC adds an ID token describing the user.

## Security checklist to mention

- Hash passwords with bcrypt, scrypt or Argon2.
- Rate-limit login attempts; offer MFA.
- Protect cookies against CSRF (SameSite plus tokens for unsafe methods).
- Rotate signing keys, with a key ID (`kid`) header so old tokens can still be verified during rotation.

Disclosure: I work at Linkjob AI (https://www.linkjob.ai/), an AI tool for job interviews.