【问题标题】:AWS Cognito not working for IE 11, but works for every other browserAWS Cognito 不适用于 IE 11,但适用于所有其他浏览器
【发布时间】:2019-12-05 15:34:54
【问题描述】:

所以我正在尝试使用 cognito 来管理我的反应应用程序中的身份验证,身份提供者是 SAML。这在 Chrome 和 Firefox 中运行得非常顺利,但在 IE 11 中却不行。这是我设置的身份验证:

import { Component } from 'react';
import { connect } from 'react-redux';
import { CognitoAuth } from 'amazon-cognito-auth-js';
import { signIn, signOutSuccess } from '../store/auth';
import { setupAxios } from '../axios';

import {
  AWS_COGNITO_CLIENT_ID,
  AWS_COGNITO_USER_POOL_ID,
  AWS_COGNITO_REDIRECT_SIGN_IN,
  AWS_COGNITO_REDIRECT_SIGN_OUT,
  AWS_COGNITO_APP_WEB_DOMAIN
} from '../env';

const cognitoSetup = props => {
//as per documentation
  const authData = {
    ClientId: AWS_COGNITO_CLIENT_ID,
    TokenScopesArray: ['email', 'openid', 'profile'],
    RedirectUriSignIn: AWS_COGNITO_REDIRECT_SIGN_IN,
    RedirectUriSignOut: AWS_COGNITO_REDIRECT_SIGN_OUT,
    AppWebDomain: AWS_COGNITO_APP_WEB_DOMAIN,
    IdentityProvider: 'SAML',
    UserPoolId: AWS_COGNITO_USER_POOL_ID
  };

  const auth = new CognitoAuth(authData);
  auth.useCodeGrantFlow(); //getting the refresh token

  auth.userhandler = {
    onSuccess: result => {
      const { profile, name, family_name, email } = result.idToken.payload;
      //passes role to store for use in the rest of the app
      const username = result.idToken.payload.identities[0].userId;
      const fullName = `${name} ${family_name}`;
      props.signIn({ username, profile, fullName, email });
    },
    onFailure: function(err) {
      console.error(err);
      throw err;
    }
  };
  return auth;
};


export class AuthService extends Component {
  constructor(props) {
    super(props);
    this.authService = cognitoSetup(this.props);
//passes the auth to axios to check for token on request
    setupAxios(this.authService);
  }

  componentDidMount() {
    const curUrl = window.location.href;
    if (curUrl.includes('?code=')) {
      this.authService.parseCognitoWebResponse(curUrl);
    } else if (!curUrl.includes('?error')) {
      this.authService.getSession();
    }
  }

  signOut = async () => {
    await this.authService.signOut();
  };

  async componentDidUpdate(prevProps) {

    if (prevProps.shouldSignOut !== this.props.shouldSignOut) {
      if (this.props.shouldSignOut) {
        await this.signOut();
        this.props.signOutSuccess();
      }
    }
  }
//render nothing 
  render() {
    return null;
  }
}

const mapState = state => ({
  username: state.auth.username,
  signedIn: state.auth.signedIn,
  shouldSignOut: state.auth.shouldSignOut
});

const mapDispatch = dispatch => ({
  signIn: (username, profile) => dispatch(signIn(username, profile)),
  signOutSuccess: () => dispatch(signOutSuccess())
});

export default connect(mapState, mapDispatch)(AuthService);

此 AuthService.js 在加载应用程序时呈现。但是在IE11中加载时出现错误var jsonDataObject = JSON.parse(jsonData);无效字符。

我不知道为什么会这样。我已经调查并得出结论,这是在 amazon-cognito-auth-js 包中发生的。我的印象是这个包裹是亚马逊制造的,所以我相信这个包裹没有错,但我看不到其他人有这个问题。有人有什么建议吗?

编辑:我确实有一个 polyfill

【问题讨论】:

    标签: reactjs internet-explorer safari amazon-cognito saml


    【解决方案1】:

    我看到你在代码中使用了箭头函数=>,即 IE 的 not supported。您可以使用babel 将它和任何其他 ES6 语法编译为 ES5。例如编译:

    const cognitoSetup = props => {
      ...
    }
    

    到:

    var cognitoSetup = function cognitoSetup(props) {
      ...
    }
    

    此外,您是否在src/index.js 的第一行导入了react-app-polyfill?这是 React 应用在 IE 11 中运行所必需的。您可以参考this thread 中的答案了解详细步骤。

    【讨论】:

    • 我很欣赏这个建议,但是我确实有一个 polyfill,并且在我的整个应用程序中都使用了箭头函数。这不是问题,引发的问题是声称存在无效字符的 Json.parse() 错误。
    • 错误指向哪一行?您如何使用JSON.parse() 功能?我在您的问题中找不到有关此功能的代码 sn-p。你能提供the minimal related code which can reproduce这个问题吗?
    • 我没有在我的代码库中的任何地方使用 JSON.parse(),我相信它在 componentDidMount 内的this.authService.parseCognitoWebResponse(curUrl); 中使用。
    • 我检查了source code,似乎JSON.parse()不在函数parseCognitoWebResponse()中。错误是否可能发生在代码的其他部分?另外,传入的 jsonData 是什么?该错误通常发生在 json 数据格式无效时。
    • 没错,我也查看了源代码,似乎在 onSuccessExchangeForToken() 和 onSuccessRefreshToken() 中调用了它。我不完全确定是什么导致了这个问题,但是 IE 说 jsonData 是未定义的。但这几乎适用于所有其他浏览器,所以我不知所措,有什么想法吗?
    猜你喜欢
    • 2015-02-12
    • 1970-01-01
    • 2015-05-05
    • 1970-01-01
    • 2014-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-26
    相关资源
    最近更新 更多