Choose the option that best describes a fundamental difference between session-based and token-based authentication methods.
Think about where the user state is kept in each method.
Session-based authentication keeps user session data on the server, linked by a session ID stored in a cookie. Token-based authentication stores all user info inside the token itself, which the client holds and sends with requests.
In token-based authentication, what is the typical behavior when the token expires?
Consider who controls token renewal in token-based auth.
When a token expires, the client must usually log in again or use a refresh token to get a new token. The server does not renew tokens automatically without client request.
Examine the Node.js Express code below. Why does the user get logged out immediately after login?
app.post('/login', (req, res) => { const user = authenticate(req.body.username, req.body.password); if (user) { req.session.user = user; res.redirect('/dashboard'); } else { res.status(401).send('Login failed'); } }); app.get('/dashboard', (req, res) => { if (!req.session.user) { res.redirect('/login'); } else { res.send(`Welcome ${req.session.user.name}`); } });
Check if the app uses session middleware properly.
If the app does not use session middleware like express-session, req.session will be undefined or not persist data, causing the user to appear logged out.
Choose the code snippet that correctly verifies a JWT token using the jsonwebtoken library.
Look for the correct method name and parameters for verifying a JWT.
The jsonwebtoken library uses jwt.verify(token, secret, callback) to verify tokens. jwt.decode does not verify signature, and jwt.check does not exist.
Given the following Express middleware, what response does the server send if the request has no Authorization header?
function authMiddleware(req, res, next) {
const authHeader = req.headers['authorization'];
if (!authHeader) {
return res.status(401).json({ error: 'No token provided' });
}
const token = authHeader.split(' ')[1];
jwt.verify(token, 'secret', (err, user) => {
if (err) return res.status(403).json({ error: 'Invalid token' });
req.user = user;
next();
});
}Check the condition when Authorization header is missing.
If the Authorization header is missing, the middleware returns a 401 status with JSON error 'No token provided' and does not call next().