How can I authenticate apollo with Auth0 in VueJS

I am struggling with a tutorial from Hasura which I believe might never have worked. I think it might be missing a piece and I’m interested in the most appropriate way to bridge the gap between when I’ve created and what will work.

From that documentation, as well as the Auth0 VueJS documentation I have been able to create a working VueJS application that authenticates via my Auth0 project and gives me back a user profile complete with user photo and email. So that’s all working.

Following the Hausura tutorial from this I added apollo. I am now able to access non-protected GraphQL queries from my API but not protected ones. The reason is pretty clearly the code I added from the tutorial here:

  getAuth: () => {
    // get the authentication token from local storage if it exists
    // return the headers to the context so httpLink can read them
    const token = localStorage.getItem('apollo-token')
    if (token) {
      return 'Bearer ' + token
    } else {
      return ''

A console.log shows that token is null even when I’m logged in. It’s pretty obvious why too. localStorage was never updated to contain a token. So of course it’s null. There’s no place in the tutorial that suggests this should be set anywhere but clearly it should.

So I did some digging and in that file src/vue-apollo.js there is an onLogin and onLogout function which require an ApolloClient object and in the case of the login, also a token. This will set the local storage appropriately.

Currently, my login is done within src/view/Home.vue like so:

login() {
   // ... maybe do something here ?

Now if I were to add something like this after the login, it will not actually be called because of the redirect. But I think I need to add something like this:

this.$auth.getTokenSilently(token => {
  onLogin(this.$apolloProvider.defaultClient, token)

Perhaps I need to add this into like the mounted() or beforeCreate() live cycle hook in my src/App.vue? I’m really not sure how to proceed here. Does this tutorial even really work?

Furthermore, isn’t storing an auth token in localStorage a bad idea because of cross site request forgery?

107 thoughts on “How can I authenticate apollo with Auth0 in VueJS”

  1. pharmacie de garde fresnes pharmacie brest geant , pharmacie bordeaux cours pasteur pharmacie de garde aujourd’hui montpellier , [url=]Acheter Viagra Super Active 100 mg pas cher[/url] pharmacie de garde marseille en beauvaisis [url=]Hydrea livraison Luxembourg[/url] , traitement jambes sans repos pharmacie seraucourt bourges , traitement gastro. pharmacie genevois annecy pharmacie saint jacques auchan ronq [url=]Ventolin Albuterol 100 mcg[/url] , pharmacie amiens rue jean moulin. pharmacie bourges tinteniac, pharmacie cap 3000 pharmacie la plus proche . pharmacie guetta argenteuil horaires, pharmacie a proximite ouvert pharmacie avignon intra muros pharmacie de beaulieu saint-etienne .


Leave a Comment