Session-based authentication stores user login info on the server. When you log in, the server creates a session and saves it. Your browser gets a session ID cookie to identify you on future requests.
Click to reveal answer
beginner
How does token-based authentication work?
Token-based authentication gives you a token (like JWT) after login. You keep this token and send it with each request. The server checks the token to verify who you are, without storing session data.
Click to reveal answer
intermediate
What is a key difference between session-based and token-based auth regarding server storage?
Session-based auth stores user sessions on the server, while token-based auth does not store user info on the server. Tokens carry the info themselves.
Click to reveal answer
intermediate
Why might token-based authentication be better for mobile apps?
Token-based auth works well for mobile apps because tokens are easy to store and send with requests. It also works well across different domains and servers without needing shared session storage.
Click to reveal answer
intermediate
What is a common security concern with token-based authentication?
Tokens can be stolen if not stored securely, leading to unauthorized access. It's important to protect tokens, use HTTPS, and set token expiration.
Click to reveal answer
In session-based authentication, where is the user session stored?
AOn the server
BIn the user's browser local storage
CInside the token
DOn a third-party service
✗ Incorrect
Session-based authentication stores user sessions on the server to track logged-in users.
What does a token usually contain in token-based authentication?
AServer session ID
BDatabase credentials
CBrowser cookies
DUser info and claims
✗ Incorrect
Tokens like JWT contain user info and claims to identify the user without server storage.
Which authentication method requires the server to keep track of active sessions?
AToken-based authentication
BSession-based authentication
CBoth methods
DNeither method
✗ Incorrect
Session-based authentication requires the server to store session data for each logged-in user.
Why is token-based authentication often preferred for APIs?
ABecause tokens are stored on the server
BBecause it uses cookies automatically
CBecause tokens can be sent easily with each request
DBecause it requires less client-side code
✗ Incorrect
Tokens are sent with each request, making token-based auth ideal for stateless APIs.
What is a common way to protect tokens from theft?
AUse HTTPS and set token expiration
BStore tokens in plain text files
CSend tokens in URL query parameters
DShare tokens publicly
✗ Incorrect
Using HTTPS encrypts data in transit and token expiration limits token misuse if stolen.
Explain the main differences between session-based and token-based authentication.
Think about where user info is stored and how the server verifies identity.
You got /5 concepts.
Describe a scenario where token-based authentication is more suitable than session-based authentication.
Consider apps that need to work across different devices or servers.
You got /5 concepts.
Practice
(1/5)
1. Which of the following best describes session-based authentication in Node.js?
easy
A. User data is stored on the server and linked to the client via cookies.
B. User credentials are sent with every request without storing data on the server.
C. A signed token is stored on the server and sent to the client.
D. Authentication is done only once without any session or token.
Solution
Step 1: Understand session-based auth storage
Session-based authentication stores user info on the server side, typically in memory or a database.
Step 2: Identify how client links to session
The client receives a cookie that holds a session ID to link requests to the stored session data.
Final Answer:
User data is stored on the server and linked to the client via cookies. -> Option A
Quick Check:
Session-based auth = server stores data + cookie link [OK]
Hint: Session auth stores data server-side, linked by cookies [OK]
Common Mistakes:
Thinking tokens are stored on the server in session auth
Confusing session ID with user credentials
Assuming session data is stored on the client
2. Which code snippet correctly sets a cookie for session-based authentication in Express.js?
easy
A. res.session('abc123');
B. res.setHeader('Authorization', 'Bearer abc123');
C. res.sendToken('abc123');
D. res.cookie('sessionId', 'abc123', { httpOnly: true });
Solution
Step 1: Identify Express.js method to set cookies
Express uses res.cookie() to set cookies on the client with options like httpOnly for security.
Step 2: Match correct syntax for session cookie
res.cookie('sessionId', 'abc123', { httpOnly: true }) correctly sets a cookie named 'sessionId'.
Final Answer:
res.cookie('sessionId', 'abc123', { httpOnly: true }); -> Option D
Quick Check:
Use res.cookie() to set session cookies [OK]
Hint: Use res.cookie() to set cookies in Express [OK]
Common Mistakes:
Using res.setHeader for cookies instead of Authorization header
Assuming res.sendToken is a valid Express method
Trying to set session with res.session which doesn't exist
3. Given this token-based auth code snippet, what will be the output if the token is valid?