1

I have a NextJS application that uses Auth0 for authorization. I am trying to write Cypress tests for e2e testing, but I am having trouble with logging in via Auth0. I have followed the documentaion on Cypress Documentation for both "Login with cy.origin()" and "programmatic login". Both of these have given me two bugs that I can't get around.

Programmatic login:

Here is my definition of the custom command

/// <reference types="cypress" />

import jwt from 'jsonwebtoken'

Cypress.Commands.add('loginByAuth0Api', (username: string, password: string) => {
  cy.log(`Logging in as ${username}`)
  const client_id = Cypress.env('TENANT_CLIENT_ID')
  const client_secret = Cypress.env('TENANT_CLIENT_SECRET')
  const audience = Cypress.env('TENANT_AUDIENCE')
  const scope = 'openid email profile'

  cy.request({
    method: 'POST',
    url: `${Cypress.env('TENANT_DOMAIN')}/oauth/token`,
    body: {
      grant_type: 'password',
      username,
      password,
      audience,
      scope,
      client_id,
      client_secret
    }
  }).then(({ body }) => {
    const claims = jwt.decode(body.id_token)
    const { nickname, name, picture, updated_at, email, email_verified, sub, exp } = claims

    const item = {
      body: {
        ...body,
        decodedToken: {
          claims,
          user: {
            nickname,
            name,
            picture,
            updated_at,
            email,
            email_verified,
            sub
          },
          audience,
          client_id
        }
      },
      expiresAt: exp
    }

    window.localStorage.setItem('auth0Cypress', JSON.stringify(item))
  })
})

declare global {
  namespace Cypress {
    interface Chainable {
      loginByAuth0Api(email: string, password: string): Chainable<void>
    }
  }
}

export {}

Here is my implementation of the command, and the cypress test that is run:

describe('Auth0', () => {
  it('should login', () => {
    cy.loginByAuth0Api(Cypress.env('TENANT_EMAIL'), Cypress.env('TENANT_PASSWORD'))
    cy.visit('localhost:3000/')
  })
})

export {}

The issue that I am having, is that the application's session is not set. When cypress visits localhost:3000/, it should display the page, but if a user is not logged in (i.e. there is not active session), then it gets redirected to /login. When the test is run, cypress redirects to /login, which should not happen.

Login with cy.origin():

When logging in with cy.origin(), cypress redirects to the login page as well. However, if I get cypress to click on the sign in button again, it will go the correct page! This seems good at first, but when clicking around on other pages, the session is not actually set.

Thank you for your time

  • I have same issue man, I think the token is not persisted properly.. but not sure. Have you solve it? – robto09 Apr 04 '23 at 08:11
  • Still no solution, I've created an issue in Cypress' github here: https://github.com/cypress-io/cypress/issues/26304, let me know if you figure it out... – Sebastian Sole Apr 04 '23 at 08:58
  • oh but that looks env variables issue. So for you no login form is shown? I mean can you bypass auth login form succesfully? My issue is more related that auth0 login form always appear.. when making use of auth0 programmatic login – robto09 Apr 04 '23 at 16:33
  • Sorry, you're right, I created an issue for a different problem I was facing. I found another solution for how to work around the issue mentioned in this post. It seems that there is a bug with this implementation and setting the cookie. Here is a link to the implementation I used to bypass this: https://community.auth0.com/t/next-auth-auth0-e2e-with-cypress-test-not-working-on-host-but-redirecting-to-universal-flow/86683 – Sebastian Sole Apr 05 '23 at 10:22

1 Answers1

0

It seems that there is a bug in the implementation with setting the cookie/creating session. I found a different implementation which doesn't have this issue. Here is the link to that: https://community.auth0.com/t/next-auth-auth0-e2e-with-cypress-test-not-working-on-host-but-redirecting-to-universal-flow/86683