# Automated authentication using cypress with organization and PKCE

**URL:** <https://community.auth0.com/t/automated-authentication-using-cypress-with-organization-and-pkce/93232>\
**Category:** Get Help\
**Tags:** organizations, pkce, testing\
**Created:** [October 27, 2022, 2:27pm UTC](https://community.auth0.com/t/automated-authentication-using-cypress-with-organization-and-pkce/93232 "2022-10-27T14:27:54Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![augustin.riedinger](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/augustin.riedinger/32/34077_2.png) [@augustin.riedinger](https://community.auth0.com/u/augustin.riedinger)\
**Post date:** [October 27, 2022, 2:27pm UTC](https://community.auth0.com/t/automated-authentication-using-cypress-with-organization-and-pkce/93232/1 "2022-10-27T14:27:54Z")

</div>

This is really driving me crazy.

I’m trying to implement automated tests on my vue frontend app using cypress.

I read and used all articles I collected on the topic like [this one](https://auth0.com/blog/end-to-end-testing-with-cypress-and-auth0/) or [that one](https://docs.cypress.io/guides/end-to-end-testing/auth0-authentication).

Then I realized what made it different is that we use organizations and `grant_type=authorization_code` so I read deeply [this topic](https://community.auth0.com/t/auth0-password-grant-with-organization/80278) from this forum.  
I also have been told by a team member that we use PKCE, so I followed the [dedicated page](https://auth0.com/docs/get-started/authentication-and-authorization-flow/call-your-api-using-the-authorization-code-flow-with-pkce).

In the end, I build a request with the following parameters:

```auto
{
    response_type: 'code',
    code_challenge,
    code_challenge_method: 'S256',
    client_id,
    redirect_uri,
    scope,
    state,
    organization,
}

```

which looks like:

```auto
https://APP_SLUG.eu.auth0.com/authorize?response_type=code&code_challenge=CODE_CHALLENGE&code_challenge_method=S256&client_id=CLIENT_ID&redirect_uri=http://frontend:3000/login/callback&scope=openid%20profile%20email%20offline_access&state=whatever&organization=org_ORGANIZATION_ID

```

But then all articles/tutorials/posts seem to agree that fetching `/authorize` with `response_type=code` would redirect to `/callback?code=MY_CODE`

 ![image](https://global.discourse-cdn.com/auth0/original/3X/b/d/bdbc1da67e0ec74d47a680f4653143f80b5b605c.png)

But in my case (I tried within cypress, with curl and with the browser), I get redirected to `/u/login?state=STATE`:

```auto
HTTP/2 302 
location: /u/login?state=hqFo2SBxNlB5TGFvZlc0UmJ6eHVIZDF4ZGJFUC05aW5KSmpFdKFur3VuaXZlcnNhbC1sb2dpbqN0aWTZIHVicVRkaTFEX2MwMUlIcVJpRk41cjlVdmJuajV4QU5Co2NpZNkgNVJMS0RWOFVNek1BZGI2elFiT3FYbjFmUm9lTlRNSTWlb3GnaWS0b3JnXXdxdFBSZHRHZmg5a2s1Nkqnb3JnbmFtZa5taWNyb3NvZnQtZGVtbw

```

which is the auth0 login screen, which is not what I want…

And if I do authenticate manually on the `/u/login` screen, then I get redirected to my `/callback?code=MY_CODE` but then I’m loosing the benefits of automation.

What am I doing wrong?? 😮‍💨 Is there something to activate/deactivate to receive the code and THEN submit the authentication credentials to `/oauth/token`?

I tried multiple options in auth0 config, created a dedicated application for this, nothing worked 😒

Thanks a lot for support 🤗

---

<div class="post-metadata">

**Author:** ![augustin.riedinger](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/augustin.riedinger/32/34077_2.png) [@augustin.riedinger](https://community.auth0.com/u/augustin.riedinger)\
**Post date:** [November 7, 2022, 8:43am UTC](https://community.auth0.com/t/automated-authentication-using-cypress-with-organization-and-pkce/93232/2 "2022-11-07T08:43:14Z")

</div>

Anyone can help? 😵‍💫

I’m completely stuck here !

---

<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:** [February 25, 2026, 5:59pm UTC](https://community.auth0.com/t/automated-authentication-using-cypress-with-organization-and-pkce/93232/3 "2026-02-25T17:59:38Z")

</div>

Hi everyone!

In the context of performing automated tests in the context of organizations, it can be a difficult task since the usual [ROPG grant flow](https://auth0.com/docs/get-started/authentication-and-authorization-flow/resource-owner-password-flow) cannot be enabled.

However, this can be resolved by using a [Custom Token Exchange Trigger](https://auth0.com/docs/customize/actions/explore-triggers/signup-and-login-triggers/custom-token-exchange-trigger) in which you will be able to set the organization as seen in this [example](https://auth0.com/docs/customize/actions/explore-triggers/signup-and-login-triggers/custom-token-exchange-trigger/custom-token-exchange-api-object#api-authentication-setorganization-organization):

```auto
exports.onExecuteCustomTokenExchange = async (event, api) => {

  // 1. Validate subject_token
  const subject_token = await validateToken(event.transaction.subject_token, jwksUri);

  // 2. Apply your authorization policy on the user
  const isAuthorized = await authorizeAccess(subject_token.sub);
  if (!isAuthorized) {
    api.access.deny('Unauthorized_login', 'User cannot login due to reason: X');
  }

  // 3. Set the user for the transaction
  api.authentication.setOrginzation('org_xS525r979AS33MSf');

  // 4. Set the user for the transaction. You may also use setUserByConnection()
  api.authentication.setUserById(subject_token.sub);

  return;
};

```

Hope this helps anybody who stumbles across the same issue!

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:** [March 11, 2026, 6:00pm UTC](https://community.auth0.com/t/automated-authentication-using-cypress-with-organization-and-pkce/93232/4 "2026-03-11T18:00:10Z")

</div>

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