Giter Site home page Giter Site logo

icepick / vue-auth-href Goto Github PK

View Code? Open in Web Editor NEW

This project forked from nachodd/vue-auth-href

0.0 0.0 0.0 413 KB

A VueJS directive for downloading files that are under a protected route schema

License: MIT License

JavaScript 93.26% HTML 6.74%

vue-auth-href's Introduction

๐Ÿ”’ vue-auth-href

A VueJS directive for downloading files that are under a protected route schema (that needs an Authorization Header).

It's a common thing on projects to have routes that are wrapped by an authentication middleware. Sometimes, depending on the project security schema, the access to files can be protected too. The problem is, that when in your frontend you put a link (an anchor tag) to a file that is protected, it can be tricky to make the authorization process. This is where this little plugin comes to help.

This plugin comes in the form of a Vue.js directive that is ready to work with JWT auth schema. On the GET request made by an anchor tag when it's clicked, it adds the Authorization: Bearer <TOKEN> header automatically.

๐Ÿ“ฆ Installation

npm install --save vue-auth-href
# or 
yarn add vue-auth-href

๐Ÿ”ง Initialization

JWT Token must be set in order to the download works. It can be set via option in the initialization, providing a function that returns the JWT Token, or inline.

import Vue from 'vue'
import VueAuthHref from 'vue-auth-href'
import store from "store/index"

// Not mandatory, options can be set inline
const options = {
  token: () => store.getters["jwt_token"], // Note that this MUST BE a function that returns the token.
  // other options here (full list of options described below)
}
Vue.use(VueAuthHref, options)

๐Ÿ•น Usage

<!-- Initialization via Options:  -->
<a v-auth-href href="https://link.to/your/protected/file.zip">Your File</a>

<!-- Inline Initialization:  -->
<a v-auth-href="{ dotsAnimation: false }" href="https://link.to/your/protected/file.zip">Your File</a>
๐Ÿ”Ž Demo:

Demo 1

Some options can be passed inline to the directive, like:

<a v-auth-href="{token: 'TOKEN'}" href="https://link.to/your/protected/file.zip">Your File</a>

โš™๏ธ Options

Option Type Default Can be set on Description
token String Initialization / Inline The JWT Token used for authentication. This parameter is REQUIRED
headerAuthKey String "Authorization" Initialization The key used in the authorization header
headerAuthValuePrefix String "Bearer " Initialization The prefix of the value used in the authorization header
aditionalHeaders Object {} Initialization Aditional headers to be sent on the request header. If it is setted, must be a javascript object
textMode String "text" Initialization / Inline Indicates to use 'text' or 'html' when link is clicked (these two are the only possible values)
downloadingText String "Downloading" Initialization / Inline Text to be shown when link is clicked and before the file is downloaded
downloadingHtml String "" Initialization / Inline HTML to be shown when link is clicked and before the file is downloaded. Can be used, for instance, to display an icon (see examples below)
dotsAnimation Boolean true Initialization / Inline Show the fancy dots animation when link is clicked. Works only when textMode: 'text'

๐Ÿ”Ž Other Demos:

...
Vue.use(VueAuthHref, {
  token: () => store.getters["auth/token"],
  textMode: "text",
  downloadingText: "Descargando",
  aditionalHeaders: { env: "test" }, // aditional headers set on the request
}
...

Demo 2

<a v-auth-href="{
  token: $store.getters['auth/token'],
  textMode: 'html',
  downloadingHtml: '<i class=\'fas fa-cog fa-spin\'></i>',
}" href="https://link.to/your/protected/file.zip">Your File</a>

Demo 3

๐Ÿค Contributing

This repository is open for contribution. So, go ahead, fork it & send me a PR!

โญ๏ธ Support

If you like this project, You can support me with starring โญ this repository

๐Ÿ“„ License

MIT

Developed with โค๏ธ by nachodd

vue-auth-href's People

Contributors

nachodd avatar icepick avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.