Cookie, Storage
Cookie, Storage
Here are the interview questions along with their answers for Cookies, Local Storage, Session Storage, and Tokens (JWTs):
9. What are the benefits and drawbacks of using Session Storage over Local Storage?
Answer: Session Storage is ideal for storing data that only needs to persist for the duration of the browser session. It offers a
lightweight solution but lacks persistence across sessions, unlike Local Storage, which can store data indefinitely.
10. When would you invalidate a JWT, and how would you manage that on the server-side?
Answer: A JWT would need to be invalidated when the user logs out, the token expires, or there is suspicion of token theft. On the
server side, you can manage this by maintaining a token blacklist or using short token expiration times with refresh tokens.
1. You're tasked with building a login system. How would you decide whether to store session information in cookies or use
JWTs?
Answer: If the system requires scalability and doesn't want to store session state on the server, using JWTs would be better since
they are stateless. If there's a need to secure the token from client-side scripts, store the JWT in a cookie with the HttpOnly flag.
2. A user complains that they are logged out every time they close their browser. Explain how Session Storage could be the
reason behind this issue.
Answer: Session Storage only persists data until the browser tab or window is closed, which could cause the user to lose session
data. Switching to Local Storage or cookies could solve this issue.
3. In a Single Page Application (SPA), where would you store user session data, and why?
Answer: Typically, a JWT is used and stored in Local Storage for ease of access by the SPA's JavaScript. However, for security
reasons, storing the token in a HttpOnly cookie is recommended to avoid XSS vulnerabilities.
4. Imagine you are developing a web app that requires persistent user data across sessions. What would you use—Local Storage
or cookies? Why?
Answer: Local Storage is suitable for storing non-sensitive data persistently, as it isn't sent with every HTTP request like cookies.
However, cookies would be better for server-side validation of the session state if the data is sensitive or used for authentication.
5. How would you implement secure authentication in a cross-origin setup using cookies?
Answer: For secure authentication in a cross-origin setup, ensure that the cookies have the SameSite=None , Secure , and
HttpOnly flags set. Also, configure CORS (Cross-Origin Resource Sharing) to allow credentials to be sent across origins.
These Q&A cover different levels of understanding and help prepare you for technical discussions in interviews. Let me know if you'd like
further details on any topic!