NUXT3路由器不将用户推到某些页面

发布于 2025-02-10 09:12:35 字数 2005 浏览 0 评论 0原文

我有两个中间件:

  1. 登录页面中间件按登录用户登录到他的最大角色允许仪表板(例如:用户,所有者 - &> lands / user -user-用户 - >用户)
  2. 面板中间件检查用户角色,并且如果没有许可访问该页面重定向返回登录(如果他试图观看所有者仪表板,将重定向到登录页面)

以使登录功能我在第一个中间件中创建的查询路径(登录?lokout = true),当用户想要注销时,仪表板应重新定向他要使用此查询登录页面,尽管不是

当我进行重定向时,请在第一个中间件上打印我:console.log(to)方法:

fullpath:“/仪表板/所有者”

重定向方法

this.$router.replace({ name: 'login', query: { 'logout': true } })

第一中间件

import { authoritiesRoutes } from "~~/middleware/panel"
import { useAuthStore } from "~~/store/auth"

export default defineNuxtRouteMiddleware(async ({ $pinia }, to, from) => {
    const token = useCookie('accessToken')
    console.log(to)

    if (to.query.logout === 'true') {
        token.value = null
        return navigateTo('/login')
    }

    if (token.value) {
        const store = useAuthStore($pinia)
        await store.retrieveUser(token.value)
        if (store.user != null) {
            const permissionLevel = store.getUser.authorities.length
            const routeName = authoritiesRoutes[permissionLevel]
            const route = '/' + routeName.replace('-', '/')
            return route;
        }
    }
})

第二中间件

import { useAuthStore } from '~~/store/auth'

export enum authoritiesRoutes {
    'panel' = 1,
    'dashboard' = 2,
    'dashboard-owner' = 3
}

export default defineNuxtRouteMiddleware(async ({ $pinia, $event }, to) => {
    const token = useCookie('accessToken')

    if (!token.value) {
        return navigateTo('/login')
    }

    const store = useAuthStore($pinia)
    await store.retrieveUser(token.value)

    if (store.user == null) {
        return navigateTo('/login')
    }

    const permissionLevel = authoritiesRoutes[to.name];
    if (store.getUser.authorities.length < permissionLevel) {
        return navigateTo("/login?error=you%20don't%20have%20permission%20to%20watch%20this%20page")
    }
})

I have two middlewares:

  1. Login page middleware pushes logged in user to his maximum's role allowed dashboard (example: user, owner -> owner / user -> user)
  2. Panel middleware checks user roles and if there's no permission to visit the page it redirects back to login (worker will be redirected to login page if he tries to watch owner dashboard)

To make logout function I created query path in the first middleware (login?logout=true) and when user wants to logout, dashboard should redirect him to login page with this query, though it doesn't

The thing is that when I do redirection, console.log(to) method on the first middleware prints me:

fullPath: "/dashboard/owner"

Redirect method

this.$router.replace({ name: 'login', query: { 'logout': true } })

First middleware

import { authoritiesRoutes } from "~~/middleware/panel"
import { useAuthStore } from "~~/store/auth"

export default defineNuxtRouteMiddleware(async ({ $pinia }, to, from) => {
    const token = useCookie('accessToken')
    console.log(to)

    if (to.query.logout === 'true') {
        token.value = null
        return navigateTo('/login')
    }

    if (token.value) {
        const store = useAuthStore($pinia)
        await store.retrieveUser(token.value)
        if (store.user != null) {
            const permissionLevel = store.getUser.authorities.length
            const routeName = authoritiesRoutes[permissionLevel]
            const route = '/' + routeName.replace('-', '/')
            return route;
        }
    }
})

Second middleware

import { useAuthStore } from '~~/store/auth'

export enum authoritiesRoutes {
    'panel' = 1,
    'dashboard' = 2,
    'dashboard-owner' = 3
}

export default defineNuxtRouteMiddleware(async ({ $pinia, $event }, to) => {
    const token = useCookie('accessToken')

    if (!token.value) {
        return navigateTo('/login')
    }

    const store = useAuthStore($pinia)
    await store.retrieveUser(token.value)

    if (store.user == null) {
        return navigateTo('/login')
    }

    const permissionLevel = authoritiesRoutes[to.name];
    if (store.getUser.authorities.length < permissionLevel) {
        return navigateTo("/login?error=you%20don't%20have%20permission%20to%20watch%20this%20page")
    }
})

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

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

发布评论

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

评论(1

探春 2025-02-17 09:12:35

我发现(async({$ pinia},to,tor,trom from)在中间中的应该为(async(to,{$ pinia})
我还发现,usecookie方法仅在重新加载页面时才有效,因此我以这种方式完成了任务:

  1. 第一个中间件
export default defineNuxtRouteMiddleware(async (to, { $pinia }) => {
    var token = useCookie('accessToken')
    const store = useAuthStore($pinia)

    if (to.query.logout === 'true') {
        if (!token.value) {
            return navigateTo('/login')
        }
        token.value = null
        if (process.client) {
            store.logout()
        }
    }

    if (token.value) {
        await store.retrieveUser(token.value)
        if (store.user != null) {
            const permissionLevel = store.getUser.authorities.length
            const routeName = authoritiesRoutes[permissionLevel]
            const route = '/' + routeName.replace('-', '/')
            return route;
        }
    }
})
  1. 第二中间件保持相同的
  2. 登录。VUE现在创建了看起来像这样的方法:
created () {
      if (this.$route.query.logout == 'true') {
        this.$router.go(0)
      }
    }

I figured out that (async ({ $pinia }, to, from) in the middlewares should be (async (to, { $pinia })
I also found out that useCookie method works only when page is reloaded so I did my task this way:

  1. First middleware
export default defineNuxtRouteMiddleware(async (to, { $pinia }) => {
    var token = useCookie('accessToken')
    const store = useAuthStore($pinia)

    if (to.query.logout === 'true') {
        if (!token.value) {
            return navigateTo('/login')
        }
        token.value = null
        if (process.client) {
            store.logout()
        }
    }

    if (token.value) {
        await store.retrieveUser(token.value)
        if (store.user != null) {
            const permissionLevel = store.getUser.authorities.length
            const routeName = authoritiesRoutes[permissionLevel]
            const route = '/' + routeName.replace('-', '/')
            return route;
        }
    }
})
  1. Second middleware stays the same
  2. Login.vue now has created method that looks like this:
created () {
      if (this.$route.query.logout == 'true') {
        this.$router.go(0)
      }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文