A
A
Alexander Koregin2019-12-12 15:48:56
Vue.js
Alexander Koregin, 2019-12-12 15:48:56

How to check for authorized users?

Good day, I'm trying to make a check whether the user has passed authorization or not.
Here is my route.js

import Vue from "vue";
import Router from "vue-router";
import VueRouter from "vue-router";

Vue.use(VueRouter);

const router = new Router({
  mode: "history",
  base: process.env.BASE_URL,
  routes: [
    {
      path: "/",
      name: "login",
      meta: {
        layout: "login"
      },
      component: () => import("../views/admin/Login.vue")
    },
    {
      path: "/panel",
      name: "panel",
      meta: {
        layout: "panel"
      },
      component: () => import("../views/admin/Main.vue")
    },
    {
      path: "/goods",
      name: "goods",
      a: {
        layout: "panel"
      },
      component: () => import("../views/admin/Goods.vue")
    },
    {
      path: "/goods/:id",
      name: "about-goods",
      props: true,
      meta: {
        layout: "panel"
      },
      component: () => import("../views/admin/AboutGoods.vue")
    },
    {
      path: "/orders",
      name: "orders",
      meta: {
        layout: "panel"
      },
      component: () => import("../views/admin/Orders.vue")
    },
    {
      path: "/sales",
      name: "sales",
      meta: {
        layout: "panel"
      },
      component: () => import("../views/admin/Sales.vue")
    },
    {
      path: "/about-order",
      name: "about-order",
      meta: {
        layout: "panel"
      },
      component: () => import("../views/admin/AboutOrder.vue")
    },
    {
      path: "/admin-control",
      name: "admin-control",
      meta: {
        layout: "panel"
      },
      component: () => import("../views/admin/AdminControl.vue")
    },
    {
      path: "/categories",
      name: "categories",
      meta: {
        layout: "panel"
      },
      component: () => import("../views/admin/Categories.vue")
    },
    {
      path: "/create-goods",
      name: "create-foods",
      meta: {
        layout: "panel"
      },
      component: () => import("../views/admin/CreateGoods.vue")
    }
  ]
});

router.beforeEach((to, from, next) => {
  const currentUser = localStorage.getItem("auth");
  if (currentUser == 1) {
    next("panel");
  } else {
    next();
  }
});

export default router;

I'm trying to do validation like this (in route.js file)
router.beforeEach((to, from, next) => {
  const currentUser = localStorage.getItem("auth");
  if (currentUser == 1) {
    next("panel");
  } else {
    next();
  }
});

But I get an error
5df2371adc027044281313.png
. I also have a question, how can I take data not from LocalStorage, but from Vuex

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
Oleg Koltunov, 2019-12-12
@dragonika8

router.beforeEach((to, from, next) => {
  const currentUser = localStorage.getItem("auth");
  if (currentUser && to.path !== '/panel' ) {
    next("panel");
  } else {
    next();
  }
});

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question