0.5.111 • Published 3 years ago

@mccowngordon/vue-secure-token v0.5.111

Weekly downloads
-
License
-
Repository
-
Last release
3 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

3 years ago

0.5.110

3 years ago

0.5.109

3 years ago

0.5.108

3 years ago

0.5.107

3 years ago

0.5.106

3 years ago

0.5.105

3 years ago

0.5.104

3 years ago

0.5.103

3 years ago

0.5.102

3 years ago

0.5.101

3 years ago

0.5.1

3 years ago

0.5.0

3 years ago

0.4.27

3 years ago

0.4.26

3 years ago

0.4.25

3 years ago

0.4.24

3 years ago

0.4.23

3 years ago

0.4.22

3 years ago

0.4.21

3 years ago

0.4.20

3 years ago

0.4.10

4 years ago

0.4.1

4 years ago

0.4.0

4 years ago

0.3.10

4 years ago

0.3.6

4 years ago

0.3.5

4 years ago

0.3.4

4 years ago

0.3.3

4 years ago

0.3.2

4 years ago

0.3.1

4 years ago

0.3.0

4 years ago

0.2.90

4 years ago

0.2.86

4 years ago

0.2.85

4 years ago

0.2.84

4 years ago

0.2.83

4 years ago

0.2.82

4 years ago

0.2.81

4 years ago

0.2.80

4 years ago

0.2.76

4 years ago

0.2.75

4 years ago

0.2.74

4 years ago

0.2.73

4 years ago

0.2.72

4 years ago

0.2.71

4 years ago

0.2.7

4 years ago

0.2.63

4 years ago

0.2.62

4 years ago

0.2.61

4 years ago

0.2.6

4 years ago

0.2.54

4 years ago

0.2.53

4 years ago

0.2.52

4 years ago

0.2.51

4 years ago

0.2.50

4 years ago

0.2.43

4 years ago

0.2.42

4 years ago

0.2.41

4 years ago

0.2.4

4 years ago

0.2.32

4 years ago

0.2.31

4 years ago

0.2.30

5 years ago

0.2.29

5 years ago

0.2.27

5 years ago

0.2.26

5 years ago

0.2.24

5 years ago

0.2.23

5 years ago

0.2.22

5 years ago

0.2.21

5 years ago

0.2.20

5 years ago

0.2.19

5 years ago

0.2.18

5 years ago

0.2.17

5 years ago

0.2.16

5 years ago

0.2.15

5 years ago

0.2.14

5 years ago

0.2.13

5 years ago

0.2.12

5 years ago

0.2.11

5 years ago

0.2.1

5 years ago

0.2.0

5 years ago

0.1.95

5 years ago

0.1.94

5 years ago

0.1.93

5 years ago

0.1.92

5 years ago

0.1.91

5 years ago

0.1.9

5 years ago

0.1.89

5 years ago

0.1.88

5 years ago

0.1.87

5 years ago

0.1.86

5 years ago

0.1.85

5 years ago

0.1.84

5 years ago

0.1.83

5 years ago

0.1.82

5 years ago

0.1.81

5 years ago

0.1.8

5 years ago

0.1.79

5 years ago

0.1.78

5 years ago

0.1.76

5 years ago

0.1.75

5 years ago

0.1.74

5 years ago

0.1.73

5 years ago

0.1.72

5 years ago

0.1.71

5 years ago

0.1.7

5 years ago

0.1.55

5 years ago

0.1.5

5 years ago

0.1.44

5 years ago

0.1.43

5 years ago

0.1.42

5 years ago

0.1.4

5 years ago

0.1.3

5 years ago

0.1.2

5 years ago

0.1.0

5 years ago