【发布时间】:2020-12-09 21:16:59
【问题描述】:
我想访问需要有效 idToken 的 Codefi Compliance API 端点,可以使用 amazon-cognito-identity-js 获取。
我已尝试使用此代码,但收到错误 index.js:3 Uncaught ReferenceError: exports is not defined
<html>
<head>
<title>Max</title>
<html>
<head>
<title>Max</title>
</head>
<body>
<script src="https://cdn.jsdelivr.net/jquery/1.12.4/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/require.js/2.3.6/require.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/node-fetch@2.6.0/browser.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/amazon-cognito-identity-js@4.3.4/lib/index.min.js"></script>
<script>
global.fetch = require('node-fetch');
var AmazonCognitoIdentity = require('amazon-cognito-identity-js');
global.navigator = () => null;
global.fetch = require('node-fetch');
var AmazonCognitoIdentity = require('amazon-cognito-identity-js');
var authenticationData = {
Username : 'YOUR_USERNAME',
Password : 'YOUR_PASSWRD',
};
var authenticationDetails = new AmazonCognitoIdentity.AuthenticationDetails(authenticationData);
var poolData = { UserPoolId : 'us-east-1_22Qg1ksxH',
ClientId : '19dph7p79mhboibih2b9qk4j9p'
};
var userPool = new AmazonCognitoIdentity.CognitoUserPool(poolData);
var userData = {
Username : 'YOUR_USERNAME',
Pool : userPool
};
var cognitoUser = new AmazonCognitoIdentity.CognitoUser(userData);
cognitoUser.authenticateUser(authenticationDetails, {
onSuccess: function (result) {
/* Use the idToken for Logins Map when Federating User Pools with identity pools or when passing through an Authorization Header to an API Gateway Authorizer*/
var idToken = result.idToken.jwtToken;
console.log('idToken:', idToken);
},
onFailure: function(err) {
console.error('ERRR: ', err);
},
});
</script>
</body>
</html>
</head>
<body>
<script>
global.fetch = require('node-fetch');
var AmazonCognitoIdentity = require('amazon-cognito-identity-js');
global.navigator = () => null;
global.fetch = require('node-fetch');
var AmazonCognitoIdentity = require('amazon-cognito-identity-js');
var authenticationData = {
Username : 'YOUR_USERNAME',
Password : 'YOUR_PASSWRD',
};
var authenticationDetails = new AmazonCognitoIdentity.AuthenticationDetails(authenticationData);
var poolData = { UserPoolId : 'us-east-1_22Qg1ksxH',
ClientId : '19dph7p79mhboibih2b9qk4j9p'
};
var userPool = new AmazonCognitoIdentity.CognitoUserPool(poolData);
var userData = {
Username : 'YOUR_USERNAME',
Pool : userPool
};
var cognitoUser = new AmazonCognitoIdentity.CognitoUser(userData);
cognitoUser.authenticateUser(authenticationDetails, {
onSuccess: function (result) {
/* Use the idToken for Logins Map when Federating User Pools with identity pools or when passing through an Authorization Header to an API Gateway Authorizer*/
var idToken = result.idToken.jwtToken;
console.log('idToken:', idToken);
},
onFailure: function(err) {
console.error('ERRR: ', err);
},
});
</script>
</body>
</html>
【问题讨论】:
-
我认为网络浏览器不支持 node-fetch。只需使用内置的浏览器获取
-
我通过添加js库更新了代码,仍然给我错误
-
您正在使用 RequireJS,但您的代码使用了不兼容的 CommonJS sytnax,这就是您遇到错误的原因
-
如何解决这个问题?
标签: node.js requirejs amazon-cognito npm-install aws-sdk-js