moltin / js-sdk

JavaScript & Node.js SDKs for the Elastic Path Commerce Cloud eCommerce API
http://documentation.elasticpath.com/
MIT License
173 stars 77 forks source link

401 and CORS issue #63

Closed ozziexsh closed 7 years ago

ozziexsh commented 7 years ago

Getting a 401 error on /api/v1/products and

XMLHttpRequest cannot load https://api.molt.in/v1/products. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:3000' is therefore not allowed access. The response had HTTP status code 401.

Building the app using Next.js / react. You don't need moltin.Authenticate() to list the products, correct?

Contrived example below at what I'm trying to do

import Moltin from 'moltin'

// ...

componentDidMount() {
  this.setState({
     moltin: new Moltin({publicId: 'XXX'});
  });
}

_getProducts() {
  console.log(this.state.moltin.Product.List());
}

// ...
ynnoj commented 7 years ago

@nehero It looks like you're hitting the v1 endpoints - is this intended? If so, you need to wrap your API calls in the Authenticate method.

Otherwise you'll need to install the newer package to use with v2.

npm install moltin@beta

Let me know if this helps 🙃

ozziexsh commented 7 years ago

The docs say to use the v1 api? https://github.com/moltin/js-sdk#installation

I'm trying to grab the client-side SDK from npm. Do you only offer the CDN? I can grab it locally but that's kinda annyoing.

Installing via NPM using yarn add moltin@beta and building gives me the error

These modules were not found:

* fs in ./~/node-localstorage/LocalStorage.js, ./~/graceful-fs/graceful-fs.js and 1 other

To install it, you can run: npm install --save fs

I'm assuming it's trying to pull the node version instead of the client one? Is there a client moltin sdk available on NPM?

ynnoj commented 7 years ago

@nehero That documentation is from the V1 branch of the repository. You'll find the README for the V2 compatible package here. A lot of this still in flux and work is still being done toward a stable release, hence why the package is tagged beta and the old branch is the default here on GitHub.

The problem is occurring because of the lack of localStorage on the server. I'll be working to fix that today, but in the meantime adding the below (👇) to your webpack config should fix the issue you're seeing.

node: {
  fs: 'empty'
}