Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to set access_token to API client's headers and keep it in actual state after refreshing #153

Closed
zablik opened this issue Jan 14, 2021 · 3 comments

Comments

@zablik
Copy link

zablik commented Jan 14, 2021

In order to access an API a need to add the current JWT token to API client's default headers.
oidcStore module has an API to get access_token, it is oidcAccessToken getter.

So my question is how can I call this oidcAccessToken getter inside userLoaded event callback?
Maybe there is a better place to set this header?
Is there an event to handle access_token change?

Thanks!

// In my `store/index.js`

export default createStore({
  state: {},
  mutations: {},
  actions: {},
  modules: {
    oidcStore: vuexOidcCreateStoreModule(
      oidcSettings,
      {
        namespaced: true
      },
      {
        userLoaded(user) {
           // set access_token
          axios.defaults.headers.common['Auth-Token'] = ... ;
        },
        userUnloaded() {
           // delete access_token
           axios.defaults.headers.common['Auth-Token'] = null;
        }
      }
    )
  }
});
@perarnborg
Copy link
Owner

Inside of the userLoaded event listener you do not need to get the access token from the store, since you have it on the user object.

        userLoaded(user) {
           // set access_token
          axios.defaults.headers.common['Auth-Token'] = user.access_token;
        },

@zablik
Copy link
Author

zablik commented Jan 14, 2021

@perarnborg Thanks! Will this work on silent token refresh?

And sorry for the duplicates. Github sent me 404 on my submit attempts, I thought no issue was created :-)

@perarnborg
Copy link
Owner

Yes, it should!

No worries!

@zablik zablik closed this as completed Jan 15, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants