amazon-archives / amazon-cognito-auth-js

The Amazon Cognito Auth SDK for JavaScript simplifies adding sign-up, sign-in with user profile functionality to web apps.
Apache License 2.0
424 stars 232 forks source link

"global is undefined" #209

Open mzguimaraes opened 4 years ago

mzguimaraes commented 4 years ago

Technologies used: Angular 8.1.3, Firefox 68.1.0esr

Importing this library using import { CognitoAuth } from 'amazon-cognito-auth-js/dist/amazon-cognito-auth'; to grab the prebuilt Webpack distribution, as recommended by issue #13 .

Cognito is set up to use a 3rd party federated ID provider internal to my company. We're using the code grant flow.

Some authentication step after returning with the grant code is calling a function named decode, I believe defined in DecodingHelper.js. That code calls global.atob(). which throws the following error at line 730 of the Webpacked file: ERROR TypeError: "global is undefined"

It appears to be preventing the library from writing the credentials to localStorage.

I was able to fix this issue by changing the definition of decode() in the Webpack file to

var decode = exports.decode = function decode(str) {
    // return global.atob(str);
    return global ? global.atob(str) : window.atob(str);
};
/* WEBPACK VAR INJECTION */}.call(exports, (function() { return this; }())))

but this is not an ideal way to fix this bug.

wallacemv commented 4 years ago

i have same problem with angular 8 :( pls help us

wallacemv commented 4 years ago

@mzguimaraes try this in your polyfills.ts (window as any).global = window;