简体   繁体   English

如何保护 Vue.js 中的客户端路由?

[英]How to protect client side routes in Vue.js?

I'm building a spa right now that uses Vue.js as the front end framework which talks to a pure json backend which uses jsonwebtokens.我现在正在构建一个使用 Vue.js 作为前端框架的水疗中心,它与使用 jsonwebtokens 的纯 json 后端进行通信。 I am more familiar with the React ecosystem.我更熟悉 React 生态系统。 I'm currently not sure how I would protect client side routes in Vue.js.我目前不确定如何保护 Vue.js 中的客户端路由。 (Not my decision on the framework. I am a new hire. yay!) (不是我对框架的决定。我是新员工。耶!)

In react I would do something like this.作为反应,我会做这样的事情。 In the index.js file of the project.在项目的 index.js 文件中。 Before the app mounts check whether or not there is a jsonwebtoken in localstorage.在应用挂载之前检查本地存储中是否有 jsonwebtoken。 If there is, set redux state to logged in. If not set to logged out.如果有,将 redux 状态设置为登录。如果没有设置为注销。

I would then use higher order components to protect my routes so that whenever a user tries to enter a client side protected route, I would use the componentWillMount life cycle method which checks for logged in state.然后我会使用高阶组件来保护我的路由,这样每当用户尝试进入客户端受保护的路由时,我都会使用 componentWillMount 生命周期方法来检查登录状态。 If logged in render the component.如果已登录,则呈现组件。 Else redirect to log in page.否则重定向到登录页面。

It seems that higher order components in vue are not able to achieve the same behaviour.似乎 vue 中的高阶组件无法实现相同的行为。 Or I just cant find documentation that shows me how to achieve it.或者我只是找不到向我展示如何实现它的文档。 Can someone share with me how they would tackle this problem?有人可以与我分享他们如何解决这个问题吗?

As explained in the documentation you can use meta fields on all the routes you want to check for authentication文档中所述,您可以在要检查身份验证的所有路由上使用元字段

The example provided in docs:文档中提供的示例:

router.beforeEach((to, from, next) => { 
    if (to.matched.some(record => record.meta.requiresAuth)) { 
        // this route requires auth, check if logged in 
        // if not, redirect to login page. 
        if (!auth.loggedIn()) { 
            next({ 
                path: '/login', 
                query: { redirect: to.fullPath } 
            }) 
        } else { 
            next() 
        } 
    } else { 
        next() // make sure to always call next()! 
    } 
}) 

Or for in component navigation guards you can follow the 2nd link provided by wostex或者对于组件导航守卫,您可以按照 wostex 提供的第二个链接

This is a good example of auth implementation using Vue.js: link这是使用 Vue.js 实现身份验证的一个很好的例子:链接

To be more specific, here is manual about nav.更具体地说,这里是关于导航的手册。 guards: link守卫:链接

i think i missed the documentation for routes meta feilds.我想我错过了路由元字段的文档。 i was doing something stupid but working i suppose.我在做一些愚蠢的事情,但我想我在工作。 haha.哈哈。

router.beforeEach((to, from, next) => {
    let web = ["home", "login", "verifyAccount", "resetPassword","forgotPassword","register"];
    if(web.includes(to.name)){
        next();
    }else{
        axios.post('/api/auth/verify-token',{
            token: localStorage.token
        }).then(response=>{
            if(response.data.verification === true){
                next();
            }else{
                router.push({
                    name:'home',
                    params:{
                        serverError:true,
                        serverMsg: 'Please login to continue.'
                    }
                });
            }
        }).catch(response=> {
            console.log(response);
        });
    }
});

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

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