frontend-interview-questions

1. How do you design an authentication flow in a React application using OAuth 2.0 with a third-party provider (e.g., Google, Auth0)?

Use an OAuth provider (like Auth0 or Google) with Authorization Code Flow + PKCE for SPAs.

Steps :

  1. Redirect user to provider’s login URL.
  2. Receive code in redirect URI.
  3. Exchange code for access + refresh tokens via secure backend or SDK.
  4. Store tokens securely in memory or httpOnly cookies.


2. What’s your approach to implementing JWT-based authentication in a React app, including token storage and refresh mechanisms?

  1. On login, receive access + refresh tokens.
  2. Store access token in memory.
  3. On 401, call refresh endpoint (if refresh token available).
  4. Auto-refresh via background timer or interceptor.


3. How do you handle multi-factor authentication (MFA) in a React frontend, and what UX considerations do you prioritize?

Flow :

  1. After successful username/password auth, check for MFA flag.
  2. Show MFA input (TOTP, SMS).
  3. On success, issue full access token.

UX priorities :


4. What’s your strategy for managing user sessions in a single-page application (SPA) with React?


5. How would you implement a role-based access control (RBAC) system in a React application using authentication data?

Use roles/permissions from the JWT or user metadata.

const hasPermission = (user, permission) =>
  user?.permissions?.includes(permission);

Conditionally render components or routes:

{
  hasPermission(user, "admin:view") && <AdminPanel />;
}

Can be enforced at route level using HOCs or custom route guards.


6. How do you design a logout mechanism in a React app that ensures all client-side state and tokens are securely cleared?

If using cookies, call a backend endpoint to clear server cookie.


7. What’s your approach to handling authentication in a server-side rendered (SSR) React application?


8. How do you integrate an authentication system with Redux or Zustand for global state management?


9. What’s your process for handling authentication errors (e.g., expired tokens, invalid credentials) in a React app?


10. How do you design a frontend authentication system to support cross-domain or cross-origin scenarios (e.g., micro-frontends)?


11. What’s the most challenging security vulnerability you’ve encountered in a frontend application, and how did you address it?

Encountered an XSS vulnerability where user input was injected into innerHTML without sanitization.

Fix : Used DOMPurify for client-side sanitization and refactored to never use dangerouslySetInnerHTML.


12. How do you protect a React application from Cross-Site Request Forgery (CSRF) attacks?


13. What’s your approach to securing API calls from a React frontend to prevent man-in-the-middle (MITM) attacks?


14. How do you design a React application to prevent sensitive data exposure in the browser (e.g., in the DOM or console)?


15. What’s your strategy for securing third-party scripts or libraries in a React application?


16. How do you implement input validation and sanitization in a React frontend to prevent injection attacks (e.g., XSS)?


17. What’s your approach to designing a Content Security Policy (CSP) for a React application?

Content-Security-Policy: default-src 'self'; script-src 'self' [<https://trusted.cdn.com>](<https://trusted.cdn.com/>); object-src 'none'; Use nonces for inline scripts and report-only mode during testing.


18. How do you handle security in a React application deployed in a zero-trust environment?


19. What’s your process for conducting a security audit of a React frontend before a major release?


20. How do you design a frontend system to comply with security standards like GDPR or HIPAA?