赛普拉斯:以编程方式登录到auth0

时间:2020-10-22 00:29:23

标签: javascript authentication asynchronous cypress auth0

对于我的测试,我决定使用程序化登录Auth0。我正在使用以下npm软件包:auth0-js。登录功能就像一个魅力,但我面临一个问题。我无法在自定义命令中从此请求返回值,以将其传递给回调URL。

这是我的auth0.WebAuth客户端示例:

cypress / utils / auth0-helper.js

import auth0 from "auth0-js";

function initializeAuth0Client() {
  return new auth0.WebAuth({
    domain: "{YOUR_AUTH0_DOMAIN}",
    clientID: "{YOUR_AUTH0_CLIENT_ID}",
  });
}

function handleLogin() {
  const username = user.email;
  const password = user.password;
  return new Promise((resolve, reject) => {
    client.login(
      {
        realm,
        username,
        password,
      },
      function (err, authResult) {
        if (err) {
          reject(authResult);
        }
        resolve(authResult);
      }
    );
  });
}

module.exports = {
  handleLogin,
  initializeAuth0Client,
};

这是我的自定义命令:

Cypress.Commands.add("loginByApiWeb", (user) => {
  const auth0Client = initializeAuth0Client();
  cy.wrap(handleLogin(), {
    timeout: 10000,
  }).then((authResult) => {
    cy.log(authResult);   //<<<<<<<<<<<< doesn't return any value
  });
});

当我尝试将客户端从auth0.WebAuth更改为auth0.Authentication时,我能够返回响应并收到我的access_token。

function initializeAuth0Client() {
  return new auth0.Authentication({
    clientID,
    domain,
    audience,
    redirectUri,
    responseType,
    scope,
    overrides: {
      __token_issuer: `https://${domain}/`,
    },
  });
}

但是我没有通过该应用的身份验证,但出现此错误:

{original: {…}, code: "login_required", description: "Login required", error: "login_required", error_description: "Login required"}

任何建议将不胜感激!

0 个答案:

没有答案