简体   繁体   English

Nuxt(Vue),登录后解析路由,在动态生成的路由上添加beforeGuard

[英]Nuxt (Vue), resolve route after login, add beforeGuard on dynamically generated routes

As you know Nuxt automatically generates VueRouter with given pages. 如您所知,Nuxt会自动生成具有给定页面的VueRouter。 What I want to achieve is to add a custom auth guard. 我要实现的是添加一个自定义身份验证防护。 So basically before rendering a route i check whether user is authenticated and if yes show the page, if not show login page and resolve the path after login. 因此,基本上在呈现路由之前,我会检查用户是否已通过身份验证,如果是,则显示页面,如果未显示,则显示登录页面并在登录后解析路径。

So the docs point to middleware as a way to handle such cases. 因此,文档指向中间件作为处理此类情况的一种方式。 In my case my auth.js in middleware folder: 就我而言,我的auth.js位于middleware文件夹中:

export default function (ctx) {
    const { store, redirect } = ctx
    if (!store.state.currentUser) {
        return redirect('/signin')
    }
}

That works fine for redirecting to the login/signup page. 对于重定向到登录/注册页面,效果很好。 The problem is that i don't have a way to resolve original path after user signs in. 问题是用户登录后我没有办法解析原始路径。

Something like: 就像是:

onUserLoggedIn(){
    this.$router.next()
}

in ths SignIn.vue component SignIn.vue组件中

Do you have an idea how to solve this 你有办法解决这个问题吗

Alternatively is there a way to set beforeGuard on dynamic routes generated by Nuxt? 或者,是否可以在beforeGuard生成的动态路由上设置beforeGuard?

Have you allready tried router.go(n) 您是否已经尝试过router.go(n)

Taken from the Docs : 取自文档

This method takes a single integer as parameter that indicates by how many steps to go forwards or go backwards in the history stack, similar to window.history.go(n).

The other method would be to store the path before redirect and reuse it later. 另一种方法是先store路径,然后再重定向。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM