# Next-auth/Auth0 e2e with Cypress test not working on host, but redirecting to universal flow

**URL:** <https://community.auth0.com/t/next-auth-auth0-e2e-with-cypress-test-not-working-on-host-but-redirecting-to-universal-flow/86683>\
**Category:** Get Help\
**Tags:** testing, nextjs, auth0\
**Created:** [July 9, 2022, 9:59am UTC](https://community.auth0.com/t/next-auth-auth0-e2e-with-cypress-test-not-working-on-host-but-redirecting-to-universal-flow/86683 "2022-07-09T09:59:31Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![rnnyrk](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/rnnyrk/32/37963_2.png) [@rnnyrk](https://community.auth0.com/u/rnnyrk)\
**Post date:** [July 9, 2022, 9:59am UTC](https://community.auth0.com/t/next-auth-auth0-e2e-with-cypress-test-not-working-on-host-but-redirecting-to-universal-flow/86683/1 "2022-07-09T09:59:31Z")

</div>

Currently facing a complex issue where i want to authenticate my end-to-end test in Cypress using Auth0 and Next-auth. Already took me quite some effort using various different tutorials like;

- [Testing with Cypress | NextAuth.js](https://next-auth.js.org/tutorials/testing-with-cypress)
- [End-to-End Testing with Cypress and Auth0](https://auth0.com/blog/end-to-end-testing-with-cypress-and-auth0/)

On localhost I’ve got the whole flow working now. But when I run the test via CI in a Github workflow using a custom domain/host I run into issues where instead of being logged in, I’m redirected to the Universal Login page of Auth0. You can see a compared flow of the Cypress tests in the following screenshot:

 ![cypress-auth0-nextauth](https://global.discourse-cdn.com/auth0/original/3X/4/8/481893608f5d5638574b77d9aba538f089942fb8.jpeg)

Here I’m facing the issue that as soon as I set the `next-auth.session-token` on my test server and redirect to the platform, the app is redirected to the Auth0 Universal Login page instead. For logging into Auth0 I use a custom Cypress command creating tokens via the Auth0 passwordGrant:

```javascript
// cypress/support/commands.ts

import hkdf from '@panva/hkdf';
import { EncryptJWT, JWTPayload, decodeJwt } from 'jose';

// Function from https://github.com/nextauthjs/next-auth/blob/5c1826a8d1f8d8c2d26959d12375704b0a693bfc/packages/next-auth/src/jwt/index.ts#L113-L121
async function getDerivedEncryptionKey(secret: string) {
  return await hkdf('sha256', secret, '', 'NextAuth.js Generated Encryption Key', 32);
}

// Function from https://github.com/nextauthjs/next-auth/blob/5c1826a8d1f8d8c2d26959d12375704b0a693bfc/packages/next-auth/src/jwt/index.ts#L16-L25
export async function encode(token: JWTPayload, secret: string): Promise<string> {
  const maxAge = 30 * 24 * 60 * 60; // 30 days
  const encryptionSecret = await getDerivedEncryptionKey(secret);
  return await new EncryptJWT(token)
    .setProtectedHeader({ alg: 'dir', enc: 'A256GCM' })
    .setIssuedAt()
    .setExpirationTime(Math.round(Date.now() / 1000 + maxAge))
    .setJti('test')
    .encrypt(encryptionSecret);
}

Cypress.Commands.add('loginByAuth0Api', (username: string, password: string) => {
  const log = Cypress.log({
    displayName: 'AUTH0 LOGIN',
    message: [`🔐 Authenticating | ${username}`],
    autoEnd: false,
  });

  log.snapshot(`Logging in as ${username}`);

  const client_id = Cypress.env('auth0_client_id');
  const client_secret = Cypress.env('auth0_client_secret');
  const audience = Cypress.env('auth0_audience');
  const scope = Cypress.env('auth0_scope');

  log.snapshot('before');

  cy.request({
    method: 'POST',
    url: `https://${Cypress.env('auth0_domain')}/oauth/token`,
    body: {
      grant_type: 'password',
      username,
      password,
      audience,
      scope,
      client_id,
      client_secret,
    },
  }).then(({ body }) => {
    const user: any = decodeJwt(body.id_token);
    const userObj = {
      user: {
        accessToken: body.access_token,
        idToken: body.id_token,
        sub: user.sub,
        nickname: user.nickname,
        picture: user.name,
        email: user.email,
      },
    };

    // Generate and set a valid cookie that next-auth can decrypt
    cy.wrap(null)
      .then(() => {
        return encode(userObj.user, Cypress.env('NEXTAUTH_SECRET'));
      })
      .then((encryptedToken) => {
        cy.setCookie('next-auth.session-token', encryptedToken);

        // Gives error:
        // cy.setCookie() had an unexpected error setting the requested cookie in Chrome.
        // > Error: Sanitizing cookie failed
        // cy.setCookie('next-auth.session-token', encryptedToken, {
        // domain: `https://${Cypress.env('auth0_domain')}`,
        // expiry: body.expires_in,
        // secure: true,
        // httpOnly: true,
        // path: '/',
        // });

        Cypress.Cookies.defaults({
          preserve: 'next-auth.session-token',
        });
      });

    log.snapshot('after');
    log.end();
  });
});

```

As you can see I’m fetching the user on the supplied Auth0 issuer and fetching tokens. I can verify via [jwt.io](http://jwt.io) that the tokens fetched are valid on both localhost and the test server.

Furthermore the code is fairly simple. I use a next-auth middleware file in `src/middleware.ts` to redirect to user when unauthenticated and using a straight forwards next-auth config as well.

```javascript
// src/middleware.ts
export { default } from 'next-auth/middleware';

export const config = { matcher: ['/dashboard/:path*'] };

```

```javascript
// pages/api/auth/[...nextauth].ts

import NextAuth from 'next-auth';
import type { NextAuthOptions } from 'next-auth';
import Auth0Provider from 'next-auth/providers/auth0';

const authOptions: NextAuthOptions = {
  pages: {
    signIn: '/auth/signin',
    signOut: '/auth/signout',
  },
  session: {
    maxAge: 900, // 15 min
  },
  providers: [
    Auth0Provider({
      clientId: process.env.AUTH0_CLIENT_ID!,
      issuer: process.env.AUTH0_ISSUER!,
      clientSecret: process.env.AUTH0_CLIENT_SECRET!,
    }),
  ],
  callbacks: {
    async jwt({ token, account }) {
      if (account) {
        token.accessToken = account.access_token;
        token.idToken = account.id_token;
      }

      return token;
    },
    async session({ session, token }) {
      session.userId = token.sub;
      return session;
    },
  },
};

export default NextAuth(authOptions);

```

Also maybe helpful is the Cypress config:

```javascript
// cypress.config.ts

import { defineConfig } from 'cypress';

// Populate process.env with values from .env file
require('dotenv').config({
  path: '.env.development',
});

const env = process.env.NODE_ENV === 'production' ? 'PROD' : 'DEV';
const baseUrl = process.env[`${env}_CYPRESS_BASE_URL`] || 'http://localhost:3000';

export default defineConfig({
  e2e: {
    baseUrl,
    chromeWebSecurity: false,
  },
  env: {
    NEXTAUTH_URL: process.env[`${env}_NEXTAUTH_URL`] || process.env.NEXTAUTH_URL,
    NEXTAUTH_SECRET: process.env.NEXTAUTH_SECRET,
    auth0_username: '',
    auth0_password: '',
    auth0_domain: (process.env?.[`${env}_AUTH0_ISSUER`] || process.env?.AUTH0_ISSUER!).replace(
      'https://',
      '',
    ),
    auth0_audience: 'main-audience',
    auth0_scope: 'openid profile email offline_access',
    auth0_client_id: process.env[`${env}_AUTH0_CLIENT_ID`] || process.env.AUTH0_CLIENT_ID,
    auth0_client_secret:
      process.env[`${env}_AUTH0_CLIENT_SECRET`] || process.env.AUTH0_CLIENT_SECRET,
  },
});

```

As seen in the [Next Auth - Testing with Cypress](https://next-auth.js.org/tutorials/testing-with-cypress) tutorial I would expect that setting the `next-auth.session-token` cookie would be enough to login via Auth0 with next-auth. I also would expect the same behaviour on localhost as well as the test server.

Only the settings of the cookie is not as in the tutorial:

```javascript
// tutorial code
cy.setCookie(cookie.name, cookie.value, {
  domain: cookie.domain,
  expiry: cookie.expires,
  httpOnly: cookie.httpOnly,
  path: cookie.path,
  secure: cookie.secure,
})

// my used code
cy.setCookie('next-auth.session-token', encryptedToken);

// also tried
cy.setCookie('next-auth.session-token', encryptedToken, {
  domain: `https://${Cypress.env('auth0_domain')}`,
  expiry: body.expires_in,
  secure: true,
  httpOnly: true,
  path: '/',
});

```

But the latter results in a Cypress error: `> Error: Sanitizing cookie failed`

Although I’ve come a long way, I’m not sure what I’m missing or misunderstanding to fix the final step; so all the help is welcome. Thanks in advance.

---

<div class="post-metadata">

**Author:** ![l3s.negocios](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/l3s.negocios/32/45817_2.png) [@l3s.negocios](https://community.auth0.com/u/l3s.negocios)\
**Post date:** [March 28, 2023, 9:33am UTC](https://community.auth0.com/t/next-auth-auth0-e2e-with-cypress-test-not-working-on-host-but-redirecting-to-universal-flow/86683/2 "2023-03-28T09:33:35Z")

</div>

Hello rnnyrk,  
Did you manage to solve the problem mentioned above? I have the same issue, when I use a URL different from localhost, the application doesn’t recognize the cookies.

---

<div class="post-metadata">

**Author:** ![nik.baleca](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/nik.baleca/32/68749_2.png) [@nik.baleca](https://community.auth0.com/u/nik.baleca)\
**Post date:** [January 29, 2025, 7:16pm UTC](https://community.auth0.com/t/next-auth-auth0-e2e-with-cypress-test-not-working-on-host-but-redirecting-to-universal-flow/86683/3 "2025-01-29T19:16:59Z")

</div>

Hi @rnnyrk

Thank you for posting your question on the Auth0 Community!

I am sorry about the late reply to your inquiry.

The error that you are experiencing seems to be quite common when trying to set a cookie on Chrome/Firefox and there does not seem to be a specific solution since the documentation on the matter is scarce.

I found some possible causes for your issue which you can take into consideration if any updated for Cypress did not handle this issue:

- The problem might be related to the `domain` property in the `setCookie()` options. If you attempt to use a “mock” domain, you might be receiving the same error. You can attempt to remove the `domain` property:

```auto
 // Errors out 
  cy.setCookie("property", "test", { domain: "localhost:3000" });
  
  // Works fine
  cy.setCookie("property", "test");

```

- There have been some reported issues where a cookie starting with `_Host-` which also had the `domain` property set. You can double check if the cookie is set with the proper URL and strip the domain property.
- Double check if there is a new line at the end of the fixture file which might be added automatically by IDEs on save due to user configuration or `.editorconfig` rules.
- You can attempt to pass in the` secure: true` in the options of the cy.setCookie() as per [Cypress Documentation](https://docs.cypress.io/api/commands/setcookie#Arguments) or even remove it alltogether since your domain is using HTTPS already.
- Finally you can attempt to set the cookie as follows:

```auto
document.cookie = `__Host-Username=${Cypress.env('username')};secure;path=/`;

```

Hope the information above is helpful and feel free to reply or open a new post on the community page!

Kind Regards,  
Nik

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/auth0/original/3X/b/5/b560f266932fcce5d57f09a6595bd50518cae3f3.jpeg) [@system](https://community.auth0.com/u/system)\
**Post date:** [February 12, 2025, 7:17pm UTC](https://community.auth0.com/t/next-auth-auth0-e2e-with-cypress-test-not-working-on-host-but-redirecting-to-universal-flow/86683/4 "2025-02-12T19:17:14Z")

</div>

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
