Failing trying to integrate @auth0/auth0-react with Cypress

Hi trying to create a series of Cypress commands to authenticate to auth0 using cookies, so that when my tests run the redirect to login screen does not happen. This is not working. But I suspect it’s related to my cookie structure, either the name or data items. However when I look in chrome at my cookies I only see two: auth0.is.authenticated and _legacy_auth0.is.authenticated. And both have a value of true. I do not see any other cookies at all. And my app is of course working. So def confused about that.

Please include the following information in your post:

  • Which SDK this is regarding: @auth0/auth0-react

  • SDK Version: 1.6.0

  • Platform Version: Nextjs

  • Code Snippets/Error Messages/Supporting Details/Screenshots: This is what I have so far, but it’s still redirecting to the login screen.
    Cypress.Commands.add(“loginToAuth0”, () => {
    cy.loginWithAuth0Client()
    .then(response => {
    const {
    accessToken,
    expiresIn,
    idToken,
    scope,
    tokenType
    } = response;

      	cy.getUserInfo(accessToken)
      		.then(user => {
      			const session = {
      				user,
      				idToken,
      				accessToken,
      				accessTokenScope: scope,
      				accessTokenExpiresAt: Date.now() + expiresIn,
      				createdAt: Date.now()
      			}
      			console.log("login auth0 session", session);
      			cy.setCookie('auth0.is.authenticated', 'true');
      			cy.setCookie(`@@auth0spajs@@::${Cypress.env("auth_client_id")}::${Cypress.env("auth_audience")}::${scope}`, JSON.stringify(session));
      		})
      })
    

})

Cypress.Commands.add(‘loginWithAuth0Client’, (overrides = {}) => {
const scope = “openid profile email”;

return new Cypress.Promise((resolve, reject) => {
	auth.client.login({
		grant_type: "http://auth0.com/oauth/grant-type/password-realm",
		realm: "Username-Password-Authentication",
		username: Cypress.env("auth_username"),
		password: Cypress.env("auth_password"),
		audience: Cypress.env("auth_audience"),
		scope,
		client_secret: Cypress.env("auth_client_secret")
	}, (loginErr, response) => {
		if (loginErr) {
			console.log("Login to auth0 failed!", loginErr);
			reject(loginErr);				
		} 

		resolve(response);			
	});
});	

})

Cypress.Commands.add(‘getUserInfo’, (accessToken) => {
return new Cypress.Promise((resolve, reject) => {
auth.client.userInfo(accessToken, (err, user) => {
if (err) {
console.log(“Failed to get userinfo!”, loginErr);
reject(err);
}

		resolve(user);
	});
});

});

Is this a feature request or bug report? No, this is a how to question.

1 Like

I have the same issue. @dchoi Have you found a solution in the meantime?

1 Like

Same question above. Did you have any luck @dchoi? I posted a similar question here Authenticate to Auth0 (via Google) in Cypress; If i could just copy in some cookie values from my browser into an ENV file that cypress reads and sets, that would be ideal. So far, haven’t gotten it to work