auth0-blog / redux-auth

MIT License
384 stars 69 forks source link

Post-login behaviour (for items requiring token) #19

Open yarnball opened 8 years ago

yarnball commented 8 years ago

Hi,

So I'm getting my token on the actions.js page by doing this:

localStorage.setItem('id_token', JSON.stringify(user.token).replace(/\"/g, ""))

Then in Quotes.js, I've got some custom code (with a fetch).

However, the Quotes.js seems to get called even before the user is logged in. This creates a problem as my 'fetch` requires the token that is created at the login page.

The only way I can get it working is to refresh the page.

Is there a way around this?

I tried adding react-router but this didn't help.

Here's a sample of Quotes.js

componentWillMount () {
    return fetch(DATA_API, 
      {
      headers: {
        'Authorization': 'JWT  ' + localStorage.getItem('id_token'),
        'Content-Type': 'application/json'}
      }
      )
      .then((response) => response.json())
      .then((json) => {
        this.setState({testapi: json, uniqueCategories: this.getUniqueCategories(json) })
      })
    },