0.5.111 • Published 2 years ago

@mccowngordon/vue-secure-token v0.5.111

Weekly downloads
-
License
-
Repository
-
Last release
2 years ago

McCownGordon Construction Vue Secure Token

Description

Securely handles API authentication between Vue.js / Sharepoint Framework Extensions projects (SPFX) and Azure Functions using Active Directory Provider tokens.

  • Dynamically adds a vuex module to your store called SecureToken
  • Provides Vue.axios() and Vue.apiHandler() methods
  • Includes wrapper component <RenderComponent> that displays loading indicator and friendly error message

SecureToken store

GetterDescription
sharepointContextspfx this.context
apiBaseUrlDepending on the environment, for example: http://localhost:7071/api/RewardsRecognition or https://mgcforms.azurewebsites.net/api/RewardsRecognition
environment'local' or 'production'
tokenjwt encoded token
serverErrortrue or false - the apiHandler() method will update this if an error occurs
unauthorizedtrue or false - the apiHandler() method will update this if the server returns a 401 response
waitingOnServertrue or false - starts out as true and gets updated to false after apiHandler() method finishes with either status 200 or an error

Setup

install:

    # NPM
    npm install @mccowngordon/vue-secure-token --save

Import the package:

In projectNameWebParts.ts

import VueSecureToken, { VueSecureTokenConfig } from '@mccowngordon/vue-secure-token';

And initialize inside the render() method:

    const vueSecureTokenConfig: VueSecureTokenConfig = {
      store,
      spContext: this.context,
      localFunctionAppRoot: "http://localhost:7071/api/RewardsRecognition"
      azureFunctionAppRoot: "https://mgcforms.azurewebsites.net/api/RewardsRecognition"
    };
    Vue.use(VueSecureToken, vueSecureTokenConfig);

Update types in your project

types/vue/shims-vue.d.ts:

import { AxiosStatic } from "axios";
import { ApiHandler } from '@mccowngordon/vue-secure-token';

declare module 'vue/types/vue' {
    interface VueConstructor {
        axios: AxiosStatic,
        apiHandler: ApiHandler
    }
}

Usage

Anytime you want to call the MGC Function api:

// If first request is an error because of an expired token,
// retryFunc is called after retrieving a new valid token
const retryFunc = () => this.context.dispatch('fetchInitData');

Vue.apiHandler({
    method: 'get',
    url: 'giftsAndCards',
    waitingIndicator: true,
})
    .then((response) => {
        this.context.commit('SET_GIFTS_AND_CARDS', response.data);
    })
    .catch((err) => {
        if (err.message.includes(401)) {
            this.context.commit('SET_UNAUTHORIZED');
        } else {
            this.context.commit('SET_SERVER_ERROR');
        }
    });

How to use custom errors

if (typeof response === 'object' && response.toString().includes('403')) {
    // this is a forbidden response/error
    this.context.dispatch(
        'SecureToken/setCustomError',
        {
            set: true,
            message: 'Sorry you are not authorized to access this application.',
            type: 'Error',
        },
        {
            root: true,
        },
    );
} else {
    this.context.commit('SET_SUBMISSIONS', response.data);
    this.context.commit('SET_FETCHED');
    if (!this._statusFilter) {
        this.context.commit('SET_FILTER_SUBMISSIONS', this._filters[0]);
    }
    return true;
}

Display an error (from a Vue Component)

this.$store.dispatch(
    'SecureToken/setCustomError',
    {
        set: true,
        message: 'Error loading calendar.  Please contact the IT Department with this error message. 87400.',
        type: 'Error',
    },
    {
        root: true,
    },
);

Clear errors (from a Vue Component)

this.$store.dispatch(
    'SecureToken/setCustomError',
    {
        set: false,
        message: null,
        type: null,
    },
    {
        root: true,
    },
);

How to manually toggle the waiting indicator

  // loading indicator from separate module
  @secureToken.Action
  public setWaitingOnServer;


 this.setWaitingOnServer(true);

To Publish An Update

Currently this project is not in its own git repo. @TODO Save it as its own repo run npm run build ; npm publish

0.5.111

2 years ago

0.5.110

2 years ago

0.5.109

2 years ago

0.5.108

2 years ago

0.5.107

2 years ago

0.5.106

2 years ago

0.5.105

2 years ago

0.5.104

2 years ago

0.5.103

2 years ago

0.5.102

2 years ago

0.5.101

2 years ago

0.5.1

2 years ago

0.5.0

2 years ago

0.4.27

2 years ago

0.4.26

2 years ago

0.4.25

2 years ago

0.4.24

2 years ago

0.4.23

2 years ago

0.4.22

2 years ago

0.4.21

2 years ago

0.4.20

2 years ago

0.4.10

3 years ago

0.4.1

3 years ago

0.4.0

3 years ago

0.3.10

3 years ago

0.3.6

3 years ago

0.3.5

3 years ago

0.3.4

3 years ago

0.3.3

3 years ago

0.3.2

3 years ago

0.3.1

3 years ago

0.3.0

3 years ago

0.2.90

3 years ago

0.2.86

3 years ago

0.2.85

3 years ago

0.2.84

3 years ago

0.2.83

3 years ago

0.2.82

3 years ago

0.2.81

3 years ago

0.2.80

3 years ago

0.2.76

3 years ago

0.2.75

3 years ago

0.2.74

3 years ago

0.2.73

3 years ago

0.2.72

3 years ago

0.2.71

3 years ago

0.2.7

3 years ago

0.2.63

3 years ago

0.2.62

3 years ago

0.2.61

3 years ago

0.2.6

3 years ago

0.2.54

3 years ago

0.2.53

3 years ago

0.2.52

3 years ago

0.2.51

3 years ago

0.2.50

3 years ago

0.2.43

3 years ago

0.2.42

3 years ago

0.2.41

3 years ago

0.2.4

3 years ago

0.2.32

3 years ago

0.2.31

3 years ago

0.2.30

3 years ago

0.2.29

3 years ago

0.2.27

3 years ago

0.2.26

3 years ago

0.2.24

3 years ago

0.2.23

3 years ago

0.2.22

3 years ago

0.2.21

3 years ago

0.2.20

3 years ago

0.2.19

3 years ago

0.2.18

3 years ago

0.2.17

3 years ago

0.2.16

3 years ago

0.2.15

3 years ago

0.2.14

3 years ago

0.2.13

4 years ago

0.2.12

4 years ago

0.2.11

4 years ago

0.2.1

4 years ago

0.2.0

4 years ago

0.1.95

4 years ago

0.1.94

4 years ago

0.1.93

4 years ago

0.1.92

4 years ago

0.1.91

4 years ago

0.1.9

4 years ago

0.1.89

4 years ago

0.1.88

4 years ago

0.1.87

4 years ago

0.1.86

4 years ago

0.1.85

4 years ago

0.1.84

4 years ago

0.1.83

4 years ago

0.1.82

4 years ago

0.1.81

4 years ago

0.1.8

4 years ago

0.1.79

4 years ago

0.1.78

4 years ago

0.1.76

4 years ago

0.1.75

4 years ago

0.1.74

4 years ago

0.1.73

4 years ago

0.1.72

4 years ago

0.1.71

4 years ago

0.1.7

4 years ago

0.1.55

4 years ago

0.1.5

4 years ago

0.1.44

4 years ago

0.1.43

4 years ago

0.1.42

4 years ago

0.1.4

4 years ago

0.1.3

4 years ago

0.1.2

4 years ago

0.1.0

4 years ago