因此,我正在尝试使用cognito来管理我的React应用程序中的身份验证,身份提供者为SAML。在Chrome和Firefox中,此功能运行非常顺利,但在IE 11或Safari中却无法正常运行。这是我设置的身份验证:
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);
无效字符。在野生动物园中,我在控制台中收到{"error":"invalid_grant"}
错误。
我不知道为什么会这样。我已经调查并得出结论,这在amazon-cognito-auth-js软件包中正在进行。我对这个程序包是由亚马逊制造的印象很深,所以我相信这个程序包没有错,但是我看不到其他人遇到这个问题。有人有建议吗?
编辑:野生动物园的问题是,对后端的请求末尾缺少'/'字符,因此未发送令牌。
编辑第2部分:我有一个polyfill
我看到您在代码中使用了箭头功能=>
,IE中的箭头功能为not supported。您可以使用babel编译它以及将其他ES6语法编译为ES5。例如,编译:
const cognitoSetup = props => {
...
}
至:
var cognitoSetup = function cognitoSetup(props) {
...
}
此外,您是否在react-app-polyfill
的第一行中导入了src/index.js
?这是React app在IE 11中运行所必需的。有关详细步骤,请参考this thread中的答案。