Node.js Developer interview question · Most asked

How would you implement authentication in a Node.js API?

Summary

Choose sessions (server-side store + HttpOnly cookie) for browser apps or tokens (short-lived JWT access tokens + rotating refresh tokens) for APIs/mobile. Hash passwords with bcrypt/argon2, verify tokens with pinned algorithms, and enforce authorization per route and per resource.

Longer Answer

Libraries: Passport (strategies), Auth.js, Lucia-style session patterns, or identity providers (Auth0, Cognito, Keycloak). Rate-limit login, support MFA, and never store tokens in localStorage if XSS is a concern.

Key Points

  • Sessions for browsers, tokens for APIs
  • bcrypt/argon2 hashing
  • Pin JWT algorithms, short expiry
  • Authorize per resource

More most-asked Node.js Developer questions

← All Node.js Developer questions