Authentication and API security

Choose safer token storage for browser applications

Compare browser token-storage options and reduce exposure from cross-site scripting and token theft. JavaScript-accessible storage is readable by injected scripts. HttpOnly cookies reduce direct script access but require careful SameSite, CSRF and cross-origin design; no storage choice removes the need to prevent XSS. Security controls depend on client type, data sensitivity and deployment context; verify provider and standards guidance before production changes.

Threat model and trust boundaries

JavaScript-accessible storage is readable by injected scripts. HttpOnly cookies reduce direct script access but require careful SameSite, CSRF and cross-origin design; no storage choice removes the need to prevent XSS.

  • Identify actors, assets and trust boundaries
  • Choose controls that address the stated threat
  • Test failure behavior and recovery paths

Implementation approach

Prefer a backend-for-frontend when it fits the architecture, keeping provider tokens server-side and issuing a protected session cookie. If a browser-only client is necessary, minimize token lifetime and scope and avoid persistent storage for long-lived credentials.

  • Confirm the responsible owner
  • Apply the change to a representative case
  • Document decisions and exceptions

Practical example

A marketing dashboard moves refresh tokens out of localStorage into a server session; its cookie is Secure, HttpOnly and SameSite-configured, and state-changing routes have CSRF protections.

  • Reproduce the starting situation
  • Apply the described approach
  • Check the user-facing result

Verification and follow-up

Threat-model XSS, CSRF, device sharing and logout; verify token expiry and revocation and ensure error telemetry does not capture authorization headers.

  • Test expected and failure cases
  • Compare with the recorded baseline
  • Agree the next review date and owner

Questions to keep in view

When should a team use this authentication and api security guide?

Compare browser token-storage options and reduce exposure from cross-site scripting and token theft.

What should be checked before rollout?

Threat-model XSS, CSRF, device sharing and logout; verify token expiry and revocation and ensure error telemetry does not capture authorization headers.