{"action":"created","issue":{"url":"https://api.github.com/repos/maxmantz/redux-oidc/issues/19","repository_url":"https://api.github.com/repos/maxmantz/redux-oidc","labels_url":"https://api.github.com/repos/maxmantz/redux-oidc/issues/19/labels{/name}","comments_url":"https://api.github.com/repos/maxmantz/redux-oidc/issues/19/comments","events_url":"https://api.github.com/repos/maxmantz/redux-oidc/issues/19/events","html_url":"https://github.com/maxmantz/redux-oidc/issues/19","id":199575900,"number":19,"title":"How do I set up proper API calls, after the user refreshes the application and the user object already exists","user":{"login":"michaelklopf","id":2892138,"avatar_url":"https://avatars3.githubusercontent.com/u/2892138?v=4","gravatar_id":"","url":"https://api.github.com/users/michaelklopf","html_url":"https://github.com/michaelklopf","followers_url":"https://api.github.com/users/michaelklopf/followers","following_url":"https://api.github.com/users/michaelklopf/following{/other_user}","gists_url":"https://api.github.com/users/michaelklopf/gists{/gist_id}","starred_url":"https://api.github.com/users/michaelklopf/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/michaelklopf/subscriptions","organizations_url":"https://api.github.com/users/michaelklopf/orgs","repos_url":"https://api.github.com/users/michaelklopf/repos","events_url":"https://api.github.com/users/michaelklopf/events{/privacy}","received_events_url":"https://api.github.com/users/michaelklopf/received_events","type":"User","site_admin":false},"labels":[{"id":355139091,"url":"https://api.github.com/repos/maxmantz/redux-oidc/labels/question","name":"question","color":"cc317c","default":true}],"state":"closed","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":11,"created_at":"2017-01-09T15:14:02Z","updated_at":"2018-05-02T17:53:49Z","closed_at":"2017-01-11T12:02:23Z","author_association":"NONE","body":"Hi,\r\n\r\nI'm mentally stuck how to tackle my problem properly. I use redux-oidc to authorize myself, user is set up by the library and I can do my API calls with the user token in the state, like this:\r\n\r\n```\r\n// API requests - done with redux-thunk\r\nexport function fetchData() {\r\n  return function(dispatch, getState) {\r\n    const token = getState().oidc.user.access_token;\r\n    if (token === '') {\r\n      return;\r\n    }\r\n\r\n    dispatch(requestData());\r\n\r\n    const request = new Request(fetchDataUrl, {\r\n      method: 'GET',\r\n      headers: getAuthHeader(token)\r\n    });\r\n\r\n    return fetch(request)\r\n      .then(response => response.json())\r\n      .then(json => {\r\n        if (json.StatusCode !== 200) {\r\n          triggerNotification(json.StatusCode, json.ResponseMessage);\r\n        }\r\n        return json;\r\n      })\r\n      .then(json => dispatch(receiveData(json)))\r\n      .catch(() => {\r\n        triggerNotification(900);\r\n      });\r\n  };\r\n}\r\n```\r\n\r\nThe problem is, when the user reloads the page, there is no user in the state, because the middleware is not yet finished with adding the user to it.\r\n\r\nUsing it like this in a component:\r\n\r\n```\r\nclass Data extends Component {\r\n  componentWillMount() {\r\n    this.props.fetchData().then(() => {\r\n      this.props.sortData(this.props.sort.column, this.props.sort.direction);\r\n    });\r\n  }\r\n  ....\r\n```\r\n\r\nI don't know how to tackle that problem. I was thinking about the userManger.getUser().then() promise, but I wasn't able to make it work yet. Any ideas what to pay attention to?\r\n\r\nIs there a hook in the library to only do the fetchData when the user is loaded into the state?\r\n\r\nI hope it's ok if I ask that here.\r\n\r\n"},"comment":{"url":"https://api.github.com/repos/maxmantz/redux-oidc/issues/comments/386064747","html_url":"https://github.com/maxmantz/redux-oidc/issues/19#issuecomment-386064747","issue_url":"https://api.github.com/repos/maxmantz/redux-oidc/issues/19","id":386064747,"user":{"login":"maxmantz","id":6616421,"avatar_url":"https://avatars3.githubusercontent.com/u/6616421?v=4","gravatar_id":"","url":"https://api.github.com/users/maxmantz","html_url":"https://github.com/maxmantz","followers_url":"https://api.github.com/users/maxmantz/followers","following_url":"https://api.github.com/users/maxmantz/following{/other_user}","gists_url":"https://api.github.com/users/maxmantz/gists{/gist_id}","starred_url":"https://api.github.com/users/maxmantz/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/maxmantz/subscriptions","organizations_url":"https://api.github.com/users/maxmantz/orgs","repos_url":"https://api.github.com/users/maxmantz/repos","events_url":"https://api.github.com/users/maxmantz/events{/privacy}","received_events_url":"https://api.github.com/users/maxmantz/received_events","type":"User","site_admin":false},"created_at":"2018-05-02T17:53:49Z","updated_at":"2018-05-02T17:53:49Z","author_association":"OWNER","body":"@acaravia Explaining how to use redux would be out of scope for discussions regarding this library. There are several ways how you can 'listen to an action' and which way you use is dependent on your project and should not be dictated by me or anyone else not involved in your project."}}
{"actor":{"display_login":"maxmantz"}}