vue-router导航卫队不限制导航

发布于 2025-02-07 22:21:22 字数 1447 浏览 1 评论 0原文

访问任何页面之前,除了登录并注册;我想通过导航警卫对用户进行身份验证。 在以后,您可以看到我的Vue-Router代码。 “这里”已记录,但是之后没有在行中取消导航。如果用户未经认证,他仍然可以访问我

的路由器文件:

import { createRouter, createWebHistory } from "vue-router";
import axios from "axios";
import HomeView from "../views/HomeView.vue";
import RegisterView from "../views/RegisterView.vue";
import LoginView from "../views/LoginView.vue";
import MeHomeView from "../views/MeHomeView.vue";

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: "/",
      name: "home",
      component: HomeView,
    },
    {
      path: "/register",
      name: "register",
      component: RegisterView,
    },
    {
      path: "/login",
      name: "login",
      component: LoginView,
    },
    {
      path: "/me",
      name: "me",
      component: MeHomeView,
    },
  ],
});

router.beforeEach((to, from) => {
  if(to.name !== 'login' && to.name !== 'register') {
    console.log(to.name);
    axios.post("http://localhost:4000/authenticate/", {accessToken: localStorage.getItem("accessToken")})
        .then(message => {
          console.log(message.data.code);
          if(message.data.code === 200) {
          } else {
            console.log("here");
            return false;
          }
        })
        .catch(error => {
          console.log(error);
          return false;
        })
  }
})

export default router;


Before accessing any page, except login and register; I want to authenticate the user with Navigation Guards.
Following you can see my code for the vue-router. The "here" gets logged, but the navigation is not cancelled in the line afterwards. It is still possible that if the user is not authenticated that he can access the /me-route

my router-file:

import { createRouter, createWebHistory } from "vue-router";
import axios from "axios";
import HomeView from "../views/HomeView.vue";
import RegisterView from "../views/RegisterView.vue";
import LoginView from "../views/LoginView.vue";
import MeHomeView from "../views/MeHomeView.vue";

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: "/",
      name: "home",
      component: HomeView,
    },
    {
      path: "/register",
      name: "register",
      component: RegisterView,
    },
    {
      path: "/login",
      name: "login",
      component: LoginView,
    },
    {
      path: "/me",
      name: "me",
      component: MeHomeView,
    },
  ],
});

router.beforeEach((to, from) => {
  if(to.name !== 'login' && to.name !== 'register') {
    console.log(to.name);
    axios.post("http://localhost:4000/authenticate/", {accessToken: localStorage.getItem("accessToken")})
        .then(message => {
          console.log(message.data.code);
          if(message.data.code === 200) {
          } else {
            console.log("here");
            return false;
          }
        })
        .catch(error => {
          console.log(error);
          return false;
        })
  }
})

export default router;


如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

泅人 2025-02-14 22:21:22

导航警卫支持在VUE路由器4中的承诺。问题是承诺链被打破,返回false不会影响任何内容。根据经验,每个诺言都需要束缚。

应该是:

return axios.post(...)

可以使用async..await以更可读性和较少错误的方式编写相同的功能。

Navigation guards support promises in Vue Router 4. The problem is that promise chain is broken, and return false doesn't affect anything. As a rule of thumb, each promise needs to be chained.

It should be:

return axios.post(...)

The same function can be written in more readable and less error-prone way with async..await.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文