Brad Goldsmith
Brad Goldsmith

Reputation: 293

Redirect in vue based on a response

So I'm trying to either load a clientdashboard or an admindashboard based on who is logging in. This is my very first time touching Vue so I'm not 100% sure this is even possible, but I figured if anyone knew it would be you folks on here. So here is my code:

export default {
    data () {
      return {
        model: {
          email: '',
          password: '',

        },
        modelValidations: {
          email: {
            required: true,
            email: true
          },
          password: {
            required: true,
            min: 5
          }
        },
      }
    },
    methods: {
      getError(fieldName) {
        return this.errors.first(fieldName)
      },
      validate() {


          let login = this;
          this.$auth.login({
            params: {
              email: login.model.email,
              password: login.model.password
            },
            success: function (response){
              console.log(response);
              let id = response.data.userinfo.id;
            },
            error: function (error) { 
              console.log(error); 
            },
              // rememberMe: true,
              // fetchUser: true,
          }).then(function(id) {
            if (id ==1) {
              this.$router.push('/admindashboard');
            } else {
              this.$router.push('/clientdashboard');
            }
          });
      }
    }
}

Upvotes: 0

Views: 279

Answers (1)

Sphinx
Sphinx

Reputation: 10729

Your router should be working fine except you pushed wrong name/path.

Below is one simple example on Vue-Router: Programmatic Navigation:

let UserView = Vue.component('user', {
  template: '<div>I am User</div>'
})

let AdminView = Vue.component('admin', {
  template: '<div>I am Admin</div>'
})

const router = new VueRouter({
  routes: [
    {
      path: '/Admin',
      name: 'Admin',
      component: AdminView
    },
    {
      path: '/User',
      name: 'User',
      component: UserView
    }
  ]
})
Vue.use(VueRouter)
app = new Vue({
  el: "#app",
  router,
  data: {
    isAdmin: true,
    loading: ''
  },
  methods: {
    goNextView: function () {
      this.loading = 'Loding View...'
      setTimeout( ()=> {
        this.isAdmin = !this.isAdmin
        if(this.isAdmin){
          this.$router.push({name: 'Admin', path: '/Admin'})
        } else {
          this.$router.push({name: 'User', path: '/User'})
        }
        this.loading = ''
      }, 1000)
    }
  }
})
.loading {
  background-color:red;
  font-weight:bold;
}
<script src="https://unpkg.com/[email protected]/dist/vue.js"></script>
<script src="https://unpkg.com/vue-router/dist/vue-router.js"></script>
<div id="app">
    <h2>Test Vue Router</h2>
    <p>Current Auth: {{isAdmin ? 'Admin' : 'User'}}</p>
    <button @click="goNextView()">Go to Next View</button>
    <p class="loading" v-if="loading">{{loading}}</p>
    <router-view></router-view>
</div>

Upvotes: 1

Related Questions