quasarframework / app-extension-apollo

The official Quasar App-Extension for Apollo and GraphQL - Currently Beta!
https://quasar.dev
109 stars 19 forks source link

Update authorization header #112

Closed vukadinFE closed 2 years ago

vukadinFE commented 2 years ago

Hi

I am using 2.0.0-beta.3 version and I wonder is there a simple way to update apolloClient options.

Particularly, I have onAuthStateChanges firebase listener placed in vuex action and from there I would like to update authorization header option.

Thanks

ejez commented 2 years ago

https://github.com/quasarframework/app-extension-apollo/tree/v2#apollo-client-options

vukadinFE commented 2 years ago

Thanks @ejez, I saw it already but the thing I was looking for was setLink method on apolloClient instance. I hoped that there is some super simple way for updating token via vuex: 'this.$apolloClient.updateHeaders()' :D

However I'll leave my implementation so if someone is searching for the same thing can use it:

//store/auth/actions.js
import { firebaseAuth, firebaseDb } from '../../boot/firebase';
import { createHttpLink } from '@apollo/client/core';
import { apolloClient } from 'src/boot/apollo';
import { defaultApolloOptions } from 'src/apollo';

export function authInit({ commit, dispatch, rootState }) {
  firebaseAuth.onAuthStateChanged(async user => {
    if (user) {   
    const token = await user.getIdToken();
    apolloClient.setLink(createHttpLink({
      ...defaultApolloOptions,
      headers: {
        authorization: token
      }
    }))
   }
 })
}