Authentication (Sessions & JWT) Questions with Answers

18 practice questions on Authentication (Sessions & JWT) (Node.js & Backend APIs) with answers and step-by-step solutions. Try 10 samples below, then practise all 18 free in the AptiNest app.

Sample questions

Question 1Easy

In a Node API, how does authentication differ from authorisation?

  1. They are two names for the same check
  2. Authorisation always happens first, and authentication only afterwards
  3. Authentication checks who you are; authorisation, what you may do
  4. Authentication applies to APIs, authorisation to web pages

Answer: C. Authentication checks who you are; authorisation, what you may do

Explanation

- Logging in proves identity (authentication);
- permission checks on each action are authorisation.

Step-by-step solution

  1. Authentication proves who the caller is, for example by checking a token.
  2. Authorisation decides what that identity may do, for example whether it can delete an order.
  3. So authentication answers who, and authorisation answers what. The correct answer is C.

Why the other options are wrong

  • A. They are two different checks that are often used together.
  • B. Authentication comes first, because you need to know who the caller is before you check permissions.
  • D. Both apply to APIs and web pages alike.

Tip: Authentication: who are you? Authorisation: are you allowed to do this?

Question 2Easy

In classic session-based authentication, what does the browser typically store?

  1. The user's password in localStorage
  2. The full user record in a cookie
  3. A session ID in a cookie; the data stays on the server
  4. Nothing; the server tracks users by IP address

Answer: C. A session ID in a cookie; the data stays on the server

Explanation

The server keeps session data in memory or a store and looks it up by the ID sent in the cookie.

Step-by-step solution

  1. Session-based auth uses cookies to send a session ID.
  2. The server stores user data, not the browser.
  3. Browser only holds the session ID in a cookie.
  4. Server looks up session data using that ID.
  5. Final answer: A session ID in a cookie; the data stays on the server.

Why the other options are wrong

  • A. Storing a password in localStorage is unsafe, and classic session auth does not keep it there.
  • B. A full user record in a cookie exposes data and is not what session auth stores.
  • D. IP tracking is unreliable and session auth uses a session ID in a cookie, not the IP.

Tip: Remember: Browser stores session ID, not user data.

Question 3Easy

What does the Secure flag on a cookie do?

  1. Encrypts the cookie value
  2. Hides the cookie from JavaScript
  3. Sends the cookie only over HTTPS connections
  4. Makes the cookie valid for one request only

Answer: C. Sends the cookie only over HTTPS connections

Explanation

Secure keeps the cookie off plain HTTP so it cannot be sniffed in transit; it does not encrypt the value itself.

Step-by-step solution

  1. The Secure flag tells the browser to send the cookie only on HTTPS requests.
  2. It does not encrypt the value and does not hide it from JavaScript.
  3. The correct answer is 2: it is sent only over HTTPS.

Why the other options are wrong

  • A. Secure does not encrypt the value; HTTPS does the encryption in transit.
  • B. That is the job of HttpOnly.
  • D. One-request use is not a cookie attribute; that would be a different design.

Tip: Use Secure, HttpOnly and SameSite together for session cookies.

Question 4Easy

What are the three dot-separated parts of a JWT?

  1. Username, password, expiry
  2. Key, value, checksum
  3. Header, body, footer
  4. Header, payload, signature

Answer: D. Header, payload, signature

Explanation

A JWT is base64url(header).base64url(payload).signature; the signature covers the first two parts.

Step-by-step solution

  1. JWT has three parts separated by dots.
  2. First part is Header with metadata.
  3. Second part is Payload with data.
  4. Third part is Signature for security.
  5. Final answer: Header, payload, signature.

Why the other options are wrong

  • A. Username and password are not JWT parts; expiry is a claim inside the payload, not a separate part.
  • B. Key, value and checksum are not the parts; the third JWT part is a signature, not a checksum.
  • C. Footer is not a JWT term; the three parts are header, payload and signature.

Question 5Medium

What does the standard "exp" claim in a JWT hold?

  1. The number of times the token may be used
  2. The expiry time, in seconds since the Unix epoch
  3. The user's role or permission level
  4. The ID of the key used for signing

Answer: B. The expiry time, in seconds since the Unix epoch

Explanation

Verifiers reject a token once the current time passes exp. iat is the issued-at time and kid identifies the key.

Step-by-step solution

  1. The exp claim is a registered JWT claim.
  2. It holds the expiry time as a number of seconds since 1 January 1970 UTC.
  3. So the correct answer is 1: the expiry time in seconds since the Unix epoch.

Why the other options are wrong

  • A. Exp is a time, not a count of uses.
  • C. The role is usually a custom claim, not exp.
  • D. The key ID is in the JWT header as kid, not in exp.

Tip: JWT registered claims: exp is expiry, iat is issued at, sub is the subject.

Question 6Medium

What does the HttpOnly flag on a cookie do?

  1. Stops JavaScript from reading it via document.cookie
  2. Sends the cookie only over HTTPS
  3. Stops the cookie being sent cross-site
  4. Makes the cookie expire when the tab closes

Answer: A. Stops JavaScript from reading it via document.cookie

Explanation

HttpOnly limits the damage from XSS because injected scripts cannot read the session cookie. The Secure flag is what requires HTTPS.

Step-by-step solution

  1. HttpOnly hides the cookie from JavaScript.
  2. document.cookie cannot read it.

Why the other options are wrong

  • B. That's Secure.
  • C. That's SameSite.
  • D. That's a session cookie.

Tip: HttpOnly = no JS.

Question 7Medium

What does this script print?

const crypto = require('crypto');
console.log(crypto.createHmac('sha256', 'key').update('msg').digest('hex').length);
  1. 32
  2. 256
  3. 16
  4. 64

Answer: D. 64

Explanation

SHA-256 produces 32 bytes, and hex encoding uses two characters per byte.

Step-by-step solution

  1. HMAC-SHA256 produces a 256-bit digest.
  2. The hex encoding uses 4 bits per character, so 256 / 4 = 64 characters.
  3. The script prints 64. The correct answer is D (64).

Why the other options are wrong

  • A. 32 is the digest size in bytes, but the hex text has 2 characters per byte, so 64.
  • B. 256 is the bit length, but the hex string has 64 characters.
  • C. 16 is far too short; a hex digest of SHA-256 has 64 characters.

Tip: Hex text has 2 characters per byte. SHA-256 is 32 bytes, so 64 hex characters.

Question 8Medium

Why should you never put a user's password or other secrets in a JWT payload?

  1. Payloads larger than a few bytes are rejected
  2. The signature fails when secrets are present
  3. Secrets make the token expire immediately
  4. The payload is only encoded, so anyone can read it

Answer: D. The payload is only encoded, so anyone can read it

Explanation

A signed JWT is protected against tampering, not against reading. base64url decoding reveals the payload.

Step-by-step solution

  1. JWT payload is base64url encoded, not encrypted.
  2. Base64url decoding makes payload readable to anyone.
  3. Secrets in payload can be read by attackers.
  4. This exposes sensitive data like passwords.
  5. So secrets should never be in JWT payload.

Why the other options are wrong

  • A. Payload size is not limited to a few bytes, so size is not why secrets are kept out.
  • B. The signature is computed over the header and payload; secrets in the payload do not break it.
  • C. A secret in the payload does not change the expiry; expiry is its own claim.

Tip: Never store secrets in JWT payload as it is readable.

Question 9Hard

Why compare a received signature with crypto.timingSafeEqual() instead of ===?

  1. Its time doesn't depend on where the inputs first differ
  2. === cannot compare two Buffer objects
  3. It is faster on long strings
  4. It also checks the token's expiry

Answer: A. Its time doesn't depend on where the inputs first differ

Explanation

A normal comparison can stop at the first mismatching byte, and that timing difference can leak the correct value byte by byte.

Step-by-step solution

  1. Normal comparison stops at first differing byte, leaking timing info.
  2. TimingSafeEqual compares all bytes regardless of differences.
  3. This prevents attackers from guessing correct bytes gradually.
  4. It ensures consistent time taken for any input pair.
  5. Thus, it avoids timing-based side-channel attacks.
  6. Final answer: Its time doesn't depend on where the inputs first differ.

Why the other options are wrong

  • B. === compares Buffer objects by reference, so it is not a timing-safe byte check.
  • C. timingSafeEqual is not faster; its purpose is constant time, not speed on long strings.
  • D. timingSafeEqual does not check expiry; it only compares the bytes of the two values.

Tip: Use crypto.timingSafeEqual() for secure comparisons to avoid timing leaks.

Question 10Hard

Why pair a short-lived access token with a long-lived refresh token?

  1. Refresh tokens compress the access tokens so that they are smaller to send over the wire
  2. A leaked access token is useful only briefly, and refresh can be revoked
  3. Access tokens cannot be signed without one
  4. It removes the need for HTTPS

Answer: B. A leaked access token is useful only briefly, and refresh can be revoked

Explanation

Access tokens are checked statelessly, so short expiry limits exposure. The refresh token is checked against server state when new access tokens are issued.

Step-by-step solution

  1. Access tokens expire quickly to limit damage if leaked.
  2. Refresh tokens can be revoked easily if compromised.
  3. Short-lived access tokens reduce risk of misuse.
  4. Long-lived refresh tokens allow reissuance of new access tokens.
  5. This ensures security even if access tokens are stolen.

Why the other options are wrong

  • A. Refresh tokens do not compress access tokens; they exist to renew access after expiry.
  • C. Access tokens can be signed on their own; signing does not need a refresh token.
  • D. HTTPS is still needed; token pairs do not replace transport encryption.

Tip: Use short-lived tokens for security, long ones to manage access.

Practise all 18 questions free

The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.

Open this topic in the app →

Related topics

See all Node.js & Backend APIs topics · All topics