0.2.0 • Published 6 years ago

vue-router-guard-trigger v0.2.0

Weekly downloads
2
License
Unlicense
Repository
github
Last release
6 years ago

Vue Route Guard Trigger

Introduction

Vue Router navigation guards are a very convenient place to define authorization rules. However, they are checked only when the current route changes, but the state of your application can change even when the route doesn't.

This plugin allows you to re-trigger the navigation guards on demand in order to make sure that the user is still authorized to access the current resource.

Getting started

Installation

You can add the plugin as dependency by running the following npm command.

npm install --save vue-route-guard-trigger

You can also use Yarn to accomplish the same thing.

yarn add vue-route-guard-trigger

Importing into the project

The following snippet demonstrates how to import and install the plugin before use.

import VueRouteGuardTrigger from 'vue-route-guard-trigger';

Vue.use(VueRouteGuardTrigger);

API

$triggerCurrentRouteGuards() ⇒ undefined

Trigger all navigation guards defined on the current route.

Example

$triggerCurrentRouteGuards() calls beforeEnter navigation guards on all currently matched routes.

export default {
    name: 'login-popup',
    // ...
    methods: {
        // ...
        onSubmit: function() {
            // ...
            this.afterLogin();
        },
        afterLogin: function() {
            this.$triggerCurrentRouteGuards();
        }
    }
    // ...
};

$triggerGuard(guard, to?) ⇒ undefined

Trigger a specific guard.

ParamTypeDescription
guardfunctionNavigation guard to be triggered
to (this.$route)objectDestination route object

Example

You can also trigger a specific guard by passing the name of the guard function to $triggerGuard() method.

function exampleGuard(to, from, next) {
    if (to.param.pass) {
        next();
    } else {
        next(false);
    }
}

this.$triggerGuard(exampleGuard);

License

This project is licensed under Unlicense license. This license does not require you to take the license with you to your project. Read the UNLICENSE file for more information.