gpt4 book ai didi

reactjs - AWS Cognito 不适用于 IE 11,但适用于所有其他浏览器

转载 作者:行者123 更新时间:2023-12-04 10:55:54 24 4
gpt4 key购买 nike

所以我试图在我的 react 应用程序中使用 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

最佳答案

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

const cognitoSetup = props => {
...
}

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

另外,你有没有进口 react-app-polyfill在您的 src/index.js 的第一行?这是 react 应用程序在 IE 11 中运行所必需的。您可以引用 this thread 中的答案。了解详细步骤。

关于reactjs - AWS Cognito 不适用于 IE 11,但适用于所有其他浏览器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59198486/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com