我正在使用 AWS Cognito 来使用用户池和身份验证。
我的注册工作正常,但我的登录功能抛出错误:
/node_modules/aws-sdk/lib/request.js:31 抛出错误; ^
ReferenceError: 窗口未定义
函数如下:
app.post('/login', function(req, res, next) {
console.log("Email: " + req.body.email);
console.log("Password: " + req.body.password);
var authenticationData = {
Username: req.body.username,
Password: req.body.password
};
var authenticationDetails = new AWS.CognitoIdentityServiceProvider
.AuthenticationDetails(authenticationData);
var poolData = {
UserPoolId: '*removed for security*',
ClientId: '*removed for security*'
};
var userPool = new AWS.CognitoIdentityServiceProvider.CognitoUserPool(
poolData);
var userData = {
Username: req.body.username,
Pool: userPool
};
var cognitoUser = new AWS.CognitoIdentityServiceProvider.CognitoUser(
userData);
cognitoUser.authenticateUser(authenticationDetails, {
onSuccess: function(result) {
console.log('access token + ' + result.getAccessToken().getJwtToken());
AWS.config.credentials = new AWS.CognitoIdentityCredentials({
IdentityPoolId: '*removed for security*',
Logins: {
'*removed for security*': result
.getIdToken().getJwtToken()
}
});
},
onSuccess: function(suc) {
console.log('Login Successful!');
},
onFailure: function(err) {
console.log('Login Unsuccessful');
alert(err);
},
});
});
我很确定错误是在执行以下行期间发生的,因为我在整个代码中放置了调试日志并且它只执行到这里:
var cognitoUser = new AWS.CognitoIdentityServiceProvider.CognitoUser(userData);
最佳答案
AWS Cognito JS SDK 旨在用于客户端。如果你想在服务器端使用它,你可以使用例如 window-mock
库模拟窗口对象。
npm install --save window-mock
然后,在文件顶部和函数之前,添加以下内容:
import WindowMock from 'window-mock';
global.window = {localStorage: new WindowMock().localStorage};
在此之后,您将得到 navigator not defined
错误,您可以通过以下方式解决:
global.navigator = () => null;
然后您应该能够在任一回调中打印结果。
关于javascript - AWS Cognito 未经身份验证的登录错误(未定义窗口)[JS],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40219518/