Search code examples
vue.jsvue-authenticate

vue-authenticate: Where is the $auth variable defined?


I am trying to implement vue-authenticate, but I'm having an issue with the following example code taken from their documentation:

new Vue({
  methods: {
    login: function () {
      this.$auth.login({ email, password }).then(function () {
        // Execute application logic after successful login
      })
    },

    register: function () {
      this.$auth.register({ name, email, password }).then(function () {
        // Execute application logic after successful registration
      })
    }
  }
})

Where is the $auth property coming from? I can't see it defined anywhere. I've looked through the documentation, and the example code in the repo, but neither provide any help.


Solution

  • As you know vue-authenticate is a Vue-plugin.

    And when you use this plugin using the line.

    Vue.use(VueAuthenticate, ...data)
    

    And this is where it gets defined in this file

    Object.defineProperties(Vue.prototype, {
      $auth: {
        get() {
          if (!vueAuthInstance) {
            // Request handler library not found, throw error
            if (!this.$http) {
              throw new Error('Request handler instance not found')
            }
    
            vueAuthInstance = new VueAuthenticate(this.$http, options)
          }
          return vueAuthInstance
        }
      }
    })
    

    Also you may want to go through this documentation on Adding Instance Properties.