Auth0 client is null in Vue SPA component on page refresh

I used the solution provided above and managed to put it into a single Vuex action. The whole Vuex module to those interested:

import { getInstance } from "@/auth";

const state = {
  token: null
};

const getters = {
  token(state) {
    return state.token;
  }
};

const mutations = {
  setToken(state, token) {
    state.token = token;
  }
};

const actions = {
  retrieveTokenFromAuthz(context) {
    return new Promise((resolve, reject) => {
      const instance = getInstance();
      instance.$watch("loading", loading => {
        if (loading === false && instance.isAuthenticated) {
          instance
            .getTokenSilently()
            .then(authToken => {
              context.commit("setToken", authToken);
              resolve(authToken);
            })
            .catch(error => {
              reject(error);
            });
        }
      });
    });
  }
};

export default {
  state,
  actions,
  mutations,
  getters
};

I call the action at the created() hook in App.vue.

Feedback appreciated.

3 Likes