1

I'm new to vue (started using vue 2) I'm using Store (vuex) and I'm trying to acheive something. basically I managed to install the vue-auth plugin : I have this.$auth that I can call from within .vue files. Now using the store I wanna call the userLogin function by dispatching the call like this from a vue file :

<script>
export default {
  computed: {
    comparePasswords() {
      return this.password === this.passwordConfirm
        ? true
        : "Passwords don't match";
    }
  },
  methods: {
    userSignUp() {
      if (this.comparePasswords !== true) {
        return;
      }
      this.$store.dispatch("userSignUp", {
        email: this.email,
        password: this.password
      });
    }
  },
  data() {
    return {
      email: "",
      password: "",
      passwordConfirm: ""
    };
  }
};
</script>

in the store/index I'm trying to access the 'this.$auth' I do understand is some kind of context switching but I don't know how to access the vue app instance. :

import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)
let app = this
export const store = new Vuex.Store({
  state: {
    appTitle: 'LiveScale Dashboard',
    user: null,
    error: null,
    loading: false
  },
  mutations: {
    setUser(state, payload) {
      state.user = payload
    },
    setError(state, payload) {
      state.error = payload
    },
    setLoading(state, payload) {
      state.loading = payload
    }
  },
  actions: {
    userLogin({ commit }, payload) {

      commit('setLoading', true)

      var redirect = this.$auth.redirect(); // THIS IS WRONG.
      this.$auth.login({                 // THIS IS WRONG. 
        body: payload, // Vue-resource
        data: payload, // Axios
        rememberMe: this.data.rememberMe,
        redirect: { name: redirect ? redirect.from.name : 'account' },
        fetchUser: this.data.fetchUser
      })
        .then(() => {
          commit('setUser', this.context)
          commit('setLoading', false)
          router.push('/home')
        }, (res) => {
          console.log('error ' + this.context);
          commit('setError', res.data)
          commit('setLoading', false)

        });
    },
    userSignUp({ commit }, payload) {
       // ...
    }
  },
  getters: {}
})

Thanks for your help

4
  • your question is confusing, I didn't understand anything. You say "in the store/index I'm trying to access the 'this.$auth' ..." which store/index? Commented Jun 21, 2018 at 23:26
  • it should be Vue.$auth, or pass this.$auth as one parameter of store->actions. Commented Jun 21, 2018 at 23:35
  • @samayo yes this what I wanna do. Commented Jun 21, 2018 at 23:36
  • @Sphinx it didn't work, however see my answer below, i had to pass the instance to the store.action function. Commented Jun 21, 2018 at 23:56

2 Answers 2

1

try using Vue.$auth in index.js it should work

Sign up to request clarification or add additional context in comments.

Comments

0

The idea (so far) is to pass the instance as an argument to the function as follows :

  this.$store.dispatch("userSignUp", {
    email: this.email,
    password: this.password,
    auth: this.$auth  //added this line
  });

and then in the store -> actions , payload.auth will contain my auth plugin :

userLogin({ commit }, payload) {

  commit('setLoading', true)

  var redirect = payload.auth.redirect();
  payload.auth.login({
    body: payload, // Vue-resource
    data: payload, // Axios
    rememberMe: this.data.rememberMe,
    redirect: { name: redirect ? redirect.from.name : 'account' },
    fetchUser: this.data.fetchUser
  })
    .then(() => {
      commit('setUser', this.context)
      commit('setLoading', false)
      router.push('/home')
    }, (res) => {
      console.log('error ' + this.context);
      commit('setError', res.data)
      commit('setLoading', false)

    });
},

I don't know if it's the best practice or not, but this is how I managed to do it. Please feel free to suggest anything.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.