AWS Cognito unauthenticated login error (window not defined) [JS]

I use AWS Cognito for user pools and authentication.

My registration works, but my login function throws an error:

/node_modules/aws-sdk/lib/request.js:31 throw an error; ^

ReferenceError: window not defined

Here is the function:

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); }, }); }); 

I am sure that the error occurs during the execution of the following line, when I placed debug logs all over the code and was executed only here:

 var cognitoUser = new AWS.CognitoIdentityServiceProvider.CognitoUser(userData); 
+6
source share
3 answers

The AWS Cognito JS SDK is intended for use on the client side. If you want to use it on the server side, you can, for example, make fun of the window object using the window-mock library.

 npm install --save window-mock 

Then at the top of the file and before your function, add the following:

 import WindowMock from 'window-mock'; global.window = {localStorage: new WindowMock().localStorage}; 

After that, you will get navigator not defined error, which you can solve with:

 global.navigator = () => null; 

Then you should be able to print the result for any of the callbacks.

+10
source

I noticed that this problem occurs while working in NodeJS only when "remember your user devices" is enabled in the user pool. If I disable the same, the error will not happen. This makes sense because, as Sajjad said, it is intended for use on the client side, and since working with the server will not have these browser properties. I got a similar related error "ReferenceError: navigator not defined"

+4
source

Install No in all device management sections if you are working in nodejs and you do not want any device to be remembered. The CogintoUser API tries to get the UserAgent from the device and accepts the navigator as the device.

0
source

Source: https://habr.com/ru/post/1011653/


All Articles