Bird
0
0

Examine the following Cypress code snippet for token authentication:

medium📝 Debug Q6 of 15
Cypress - Authentication and Sessions
Examine the following Cypress code snippet for token authentication:
cy.request('POST', '/login', { username: 'user', password: 'pass' })
  .then(response => {
    const token = response.token;
    cy.setCookie('auth', token);
  });

What is the main issue with this code?
AThe username and password should be passed as query parameters
Bcy.setCookie cannot be used to store tokens
CThe HTTP method should be GET instead of POST
DThe token should be accessed via response.body.token, not response.token
Step-by-Step Solution
Solution:
  1. Step 1: Understand response object structure

    Cypress response object contains the server response in the body property.
  2. Step 2: Identify token location

    The token is typically inside response.body.token, not directly on response.
  3. Step 3: Verify other options

    Using POST is correct; cy.setCookie can store tokens; credentials are sent in body, not query.
  4. Final Answer:

    The token should be accessed via response.body.token, not response.token -> Option D
  5. Quick Check:

    Token is in response.body [OK]
Quick Trick: Access token via response.body.token, not response.token [OK]
Common Mistakes:
  • Trying to get token directly from response object
  • Misusing HTTP methods for login
  • Assuming cy.setCookie cannot store tokens

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Cypress Quizzes