【问题标题】:Fetch the idToken using using amazon-cognito-identity-js javascript使用 amazon-cognito-identity-js javascript 获取 idToken
【发布时间】: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


【解决方案1】:

amazon-cognito-identity-js NPM 页面说你必须要么

  • 使用 Webpack 之类的打包工具或
  • 使用 NPM 下载包并从下载的dist 目录中获取min.js 文件。

第一个选项是更好的长期选项,因为您最终会得到一个从较少来源下载的构建,但如果您只是在试验,则第二个可能更容易。

跟进第二个选项,而不是使用 require 您可以运行

npm i amazon-cognito-identity-js

并将以下内容放在您在 HTML 中获取自己的 javascript 的位置之前

&lt;script src="node_modules/amazon-cognito-identity-js/dist/amazon-cognito-identity.min.js"&gt;&lt;/script&gt;

确保调整src="node_modules/... 路径或移动min.js 文件以匹配您的目录结构。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-12-15
    • 2019-12-11
    • 1970-01-01
    • 2021-09-13
    • 2019-05-19
    • 2023-02-01
    • 2018-07-14
    • 2022-01-02
    相关资源
    最近更新 更多