auth0cypressauth0-lock

Programmatically authenticate in Auth0


I'm setting up cypress.io to test a react app. I'm trying to programmatically authenticate with Auth0 Lock so I don't have to test their UI. Here are the steps:

  1. POST to https://[auth0-domain].com/usernamepassword/login

     client_id: "",
     client_secret: "",
     audience: "audience",
     redirectUri: "http://[auth0-domain].com:8383/callback",
     scope: "openid email crud:all",
     protocol: "oauth2",
     sso: true,
     username: "",
     password: "",
     connection: "conn",
     tenant: "tenant",
     popup: false,
    
  2. Collect returned form data

  3. POST to https://[auth0-domain].com/login/callback

    wa=wsignin1.0&wresult=token&wctx=metastuff

The first steps works but in the third step I'm able to post to login/callback but I get an HTML page with this error message:

Looks like something went wrong! There could be a misconfiguration in the system or a service outage. We track these errors automatically, but if the problem persists feel free to <a href="https://auth0.com/support" target="_new">contact us</a> with this tracking id: <b>e88e08e43b7cdee78458</b>.<br/>Please try again.

I'm wondering if there is something with Auth0 blocking me from doing this or if I'm not sending the correct data/header.


Solution

  • I ended up using the auth0-js module which calls the auth0 callback. Then I use 'should' to wait for the localStorage to be set:

    import auth0 from 'auth0-js';
    import url from 'url';
    
    Cypress.Commands.add('login', () => {
      const auth = new auth0.WebAuth({
        audience: 'http://audience',
        domain: 'domain.auth0.com',
        clientID: 'qqqqqqqqqqqq',
        redirectUri: 'http://localhost/callback',
        responseType: 'token id_token',
        scope: 'openid email profile name username groups roles',
        sso: true
      });
    
      auth.login({username: 'username', password: 'pass'});
      cy.window().its('localStorage.user_profile').should('exist')
    });