Refreshing Moltin tokens with AXIOS

access-token
javascript
authentication

#1

So I am currently using axios to connect to all my MOLTIN API’S, however I am having issues everytime my auth tokens expire. Has anyone found a good solution to deal with this issue. I believe you can do something like this:

axios.interceptors.response.use(undefined, (err) => {
    if (err.response.status === 401) {
      err.config.headers['authorization'] = null
      return store.dispatch('moltin/credentials')
        .then((token) => {
          console.log('new token', token)

          axios.defaults.headers.common['Authorization'] = `Bearer ${token}`
        })
    }
}

#2

Not sure why you wouldn’t use the SDK as it silently handles it all for you.


#3

Am I able to use the SDK authentication while using API endpoints for everything else?


#4

You can unless it is customer specific data you are looking to get in which case you will need to do something like:

const customer = {
  email: 'ron@swanson.com',
  password: 'mysecretpassword'
}
​
Moltin.Customers.Token(customer).then(brands => {
  // Do something
})

The framework you choose to use is irrelevant really. Not sure why you would want to go build a copy of their SDK in AXIOS. You might as well use the SDK as it pretty much does everything you are going to need in one or two lines of code

Have a look at he JS SDK here https://github.com/moltin/js-sdk


#5

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.