npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@warrantdev/vue-warrant

v2.0.0

Published

Vuejs components for Warrant

Downloads

6

Readme

@warrantdev/vue-warrant

npm Slack

Overview

The Warrant Vuejs library provides a plugin, vue-router middleware, and components for controlling access to pages and components in Vuejs using Warrant. The library interacts directly with the Warrant API using short-lived session tokens that must be created server-side using your API key. Refer to this guide to see how to generate session tokens for your users.

Installation

Use npm to install the module:

npm install @warrantdev/vue-warrant

Usage

Plugin

Add the Warrant plugin to your Vuejs app, passing it your Client Key using the clientKey config option. The plugin allows you to access utility methods for performing access checks anywhere in your app.

// main.js
import Vue from "vue";
import router from "./router";
import App from "./App.vue";
import { Warrant } from "@warrantdev/vue-warrant";

Vue.config.productionTip = false;

Vue.use(Warrant, {
    clientKey: "client_test_f5dsKVeYnVSLHGje44zAygqgqXiLJBICbFzCiAg1E=",
});

new Vue({
    router,
    // store,
    render: h => h(App)
}).$mount("#app");

Setting the Session Token

In order to finish initializing the plugin and begin performing access checks in your app, you must provide a server-generated session token and set it using the setSessionToken plugin method. Otherwise requests from your Vuejs application will be denied by the Warrant API.

Set the session token using the setSessionToken plugin method:

// Login.vue
<template>
    <form @submit.prevent="onSubmit(email, password)" class="login-form">
        <!-- email & password inputs, etc. -->
    </form>
</template>

<script>
export default {
    name: "login",
    data() {
        return {
            email: null,
            password: null,
        };
    },
    methods: {
        async onSubmit(email, password) {
            const response = await login(email, password);

            // NOTE: This session token must be generated
            // server-side when logging users into your
            // application and then passed to the client.
            // Access check calls in this library will fail
            // if the session token is invalid or not set.
            this.$warrant.setSessionToken(response.data.warrantSessionToken);

            //
            // Redirect user to logged in page
            //
        }
    }
};
</script>

hasWarrant(warrantCheck)

hasWarrant is a plugin method that returns a Promise which resolves with true if the user for the current session token has the specified warrants and returns false otherwise. Use it for fine-grained conditional rendering or for specific logic within components.

Using hasWarrant plugin method:

<template>
    <div v-if="protectedInfo">
        <protected-info>{{ protectedInfo }}</protected-info>
    </div>
</template>
<script>
export default {
    data() {
        protectedInfo: null,
    },
    async created() {
        const isAuthorized = await this.$warrant.hasWarrant({
            warrants: [{
                objectType: "info",
                objectId: "protected_info",
                relation: "viewer",
            }]
        });
        if (isAuthorized) {
            // request protected info from server
        }
    }
};
</script>

authorize Middleware

authorize is a vue-router middleware function you can use in components rendered by vue-router to easily protect your routes behind a warrant.

<template>
    <div>My Super Secret Component</div>
</template>

<script>
import { ProtectedView, authorize } from "@warrantdev/vue-warrant";

export default {
    beforeRouteEnter: authorize({
        warrants: [{
            objectType: "secret",
            objectId: "secretId",
            relation: "viewer",
        }],
        redirectTo: "/",
    })
}
</script>

ProtectedView

ProtectedView is a utility component you can wrap around markup or components that should only be accessible to users with certain privileges. It only renders the components it wraps if the user has the given warrant.

<template>
    <div>
        <my-public-component/>
        <protected-view
            :warrants="warrants"
        >
            <my-protected-component/>
        </protected-view>
    </div>
</template>
<script>
import { ProtectedView } from "@warrantdev/vue-warrant";

export default {
    components: {
        ProtectedView,
    },
    data: function() {
        return {
            warrants: [{
                objectType: "myObject",
                objectId: this.$route.params.objectId,
                relation: "view",
            }]
        };
    },
};
</script>

Support for Multiple Warrants

warrants contains the list of warrants evaluted to determine if the user has access. If warrants contains multiple warrants, the op parameter is required and specifies how the list of warrants should be evaluated.

anyOf specifies that the access check request will be authorized if any of the warrants are matched and will not be authorized otherwise.

allOf specifies that the access check request will be authorized if all of the warrants are matched and will not be authorized otherwise.

// User is authorized if they are a 'viewer' of protected_info OR a 'viewer' of 'another_protected_info'
const isAuthorized = await this.$warrant.hasWarrant({
    op: "anyOf",
    warrants: [{
        objectType: "info",
        objectId: "protected_info",
        relation: "viewer",
    }, {
        objectType: "info",
        objectId: "another_protected_info",
        relation: "viewer",
    }]
});

Notes

We’ve used a random Client Key in these code examples. Be sure to replace it with your actual Client Key to test this code through your own Warrant account.

For more information on how to use the Warrant API, please refer to the Warrant API reference.

TypeScript support

This package includes TypeScript declarations for Warrant.

Note that we may release new minor and patch versions of @warrantdev/vue-warrant with small but backwards-incompatible fixes to the type declarations. These changes will not affect Warrant itself.

Warrant Documentation