0.0.12 • Published 7 years ago

vuestrap-client-auth-support v0.0.12

Weekly downloads
2
License
MIT
Repository
github
Last release
7 years ago

Vuestrap | Client Auth Support

Instant implementation of user authentication and authorization for Vuestack client apps (Vue + Vue-Router + Vuex).

WIP

Not ready for public use until version 0.1.0 - Syntax and logic are in frequent flux.

Table of Contents

Introduction

What is a Vuestrap?

A Vuestrap is a Vue plugin designed to bootstrap Vue apps with robust, fully-operational application layers.

The plugins strap themselves across the full horizontal stack of the Vue anatomy: adding router logic, state management handling, application logic, data models, and components, allowing you to rapidly compose powerful Vue application boilerplates.

Client Auth Support

The Client Auth Support Vuestrap provides persisent user authentication and authorization sessions within a Vue client application.

If you want to support user auth for your isomorphic (server-side & client-side) application, use vuestrap-iso-auth-support instead.

TBC - More Details

Prerequisites

The Client Auth Support plugin requires:

To harness the instant implementation of auth support, vue-router is needed:

If a vue-router instance is not provided when bootstrapping, or vue-router is not being utilized in your application stack, you can leverage the store API and data models to programmatically implement auth support.

How to Use

Install

$ npm install vuestrap-client-auth-support --save

Bootstrap Your Vue App

import Vue from 'vue';
import store from './store'; // your vuex store instance
import router from './router'; // your vue-router instance
import ClientAuthSupport from 'vuestrap-client-auth-support';

Vue.use(ClientAuthSupport, {
  store,
  router,
  authenticator: <my_authenticator>,
  persistType: 'local',
  tokenName: 'user-auth-token',
});

Namespace

The default namespace for the Client Auth Support store API is: auth.

However, you can set your preferred namespace when bootstrapping, using the namespace option.

Options

The following options are available when bootstrapping the app.

NameRequired?Description
storeYesThe Vuex instance.
routerNoThe Vue-Router instance. If provided, full auth support logic will be automatically configured. If a router instance is not provided, the store actions must be leveraged to implement auth support in your app.
authenticatorYesCompatible authentication logic (see Authentictors).
persistTypeNoA string identifier, specifying the method of persistence to use for the token. Available values: 'local' (default), 'session', 'cookie'.
tokenNameNoThe name to use for the persisted auth token. If not specified, the default token name used is user-auth-token.
directUnAuthedToNoThe route name to direct un-authenticated traffic, when a route is guarded using the requiresAuth meta property. Default value: 'login'.
namespaceNoThe namespace to use with the store API. By default, the namespace is auth.
logInitNoSet to true to log messages on bootstrapping. Defaults to false.
debugNoSet to true to log debug messages during utilization. Defaults to false.

Models

ModelDescription
UserRepresents the active user and auth status of the active web session.

User

The User data model encapsulates the state and values of the user that is actively using the app. The Vuestrap creates and manages this model internally, whenever authentication and authorization occurs.

Properties

NameTypeDescription
modelStringThe name of the model type (i.e. User).
is_anonBooleanWhether or not the user is acting as anonymous.
is_logged_inBooleanWhether or not the user is logged-in.
idNumber or StringThe ID of the user.
external_idNumber or StringAn external (third-party) associated ID of the user.
usernameStringThe username of the user.
emailStringThe email of the user.
first_nameStringThe first name of the user.
last_nameStringThe last name of the user.
display_nameStringThe display name of the user.
preferred_localeStringThe preferred locale of the user.
avatar_urlStringThe avatar URL for the user.
avatar_thumb_urlStringA thumbnail version of the avatar URL for the user.
rolesArrayAn array of role names granted to the user.

Functions

NameParametersReturnsDescription
login(none)(void)A convenience method for setting the model to a logged-in state.
logout(none)(void)A convenience method for setting the model to a logged-out state.
hasRoleroleNameBooleanA convenience method for checking if the user has a specified role.
getInfo(none)ObjectGenerates a light-weight info object describing the user.
toSession(none)StringGenerates a stringified version of the info object describing the user.
serialize(none)StringSerializes the full set of user properties into a String, that is safe for HTTP transport.

User as Anonymous

The activeUser always returns a User model. When un-authenticated, the anonymized shape and state of the model instance will hold:

NameTypeValue
modelStringUser
is_anonBooleantrue
is_logged_inBooleanfalse
idNumber-1
external_idnullnull
usernameStringnull
display_nameString'Anonymous'
avatar_urlStringTBD
avatar_thumb_urlStringTBD

Store

Getters

GetterReturnsDescriptionExample
pluginNameStringThe plugin name/identifier. This value cannot be changed.this.$store.getters['auth/pluginName']
activeUserUserThe active user that is using the app. The user returned is never null.this.$store.getters['auth/activeUser']
activeUserTokenStringThe active user auth token, obtained when a user has successfully authenticated. Returns null if an auth session has not been successfully established (or the active user is not logged-in).this.$store.getters['auth/activeUserToken']
isAuthenticatingBooleanReturns true when the app is performing authentication.this.$store.getters['auth/isAuthenticating']
isAuthorizingBooleanReturns true when the app is performing authorization.this.$store.getters['auth/isAuthorizing']
isFetchingUserInfoBooleanReturns true when the app is fetching user information via the authenticator hook.this.$store.getters['auth/isFetchingUserInfo']

Actions

All actions are Promises, but not all actions are asynchronous.

NameParametersReturnsDescriptionExample
authorize(none){ user: <the_user_model>, token: <the_auth_token> }TBCthis.$store.dispatch('auth/authorize')
loginUsercreds = { username: <username>, password: <password> }{ user: <the_user_model>, token: <the_auth_token> }TBCthis.$store.dispatch('auth/loginUser', creds)
logoutUser(none){ user: <the_user_model>, token: null }TBCthis.$store.dispatch('auth/logoutUser')

Internal Actions

The following actions are available in the store, but are primarily utilized internally by the Vuestrap logic. Unless you are performing customized logic with the Client Auth Support store, you will not likely use these actions.

NameParametersReturnsDescriptionExample
setActiveUserUser(void)TBCthis.$store.dispatch('auth/setActiveUser', user)
clearActiveUser(none)(void)TBCthis.$store.dispatch('auth/clearActiveUser')
fetchUserInfo(none){ <the_user_info> }TBCthis.$store.dispatch('auth/fetchUserInfo')

Router

If a Vue router instance is provided to the Vuestrap when bootstrapping (i.e. with Vue.use), a beforeEach hook is registered that calls the authorize store action on every route transition. This enables the app to maintain its active auth session, so a refresh of the app will keep the active user logged-in and authorized.

If you want to enforce that a user must be authenticated (logged-in) in order to enter/view a particular route, you can use the requiresAuth meta property to enforce this policy.

For any routes with the requiresAuth guard attached, the authorize hook will check if the active user is logged-in before entering the route. If not, the router will forward the un-authenticated user to the route specified with the directUnAuthedTo option (by default the route is 'login').

Example

new Router({
  routes: [
    {
      path: '/',
      name: 'home',
      component: HomePage,
    },
    {
      path: '/',
      name: 'login',
      component: LoginPage,
    },
    {
      path: '/account',
      name: 'account',
      component: MyAccountPage,
      meta: { requiresAuth: true }, // the user must be logged-in to view
    },
  ],
});

Components

TBC

NamePropsDescriptionExample
local-account-loginTBCProvides a simple username/password login box.<local-account-login />

Events

NameReturnsDescriptionExample
logincreds = { ...}Emitted when login submit is clicked.<local-account-login v-on:login="doLogin" />

Authenticators

TBC

For Developers

Dev Run

To provide a fully working Vue app environment to test and develop the plugin, a simple Vue application will build (the plugin & the app bundle) and serve when running:

$ npm run dev

By default, the development app environment will hot-reload changes and will run on localhost:3301.

You can change the configuration for the development environment via test/simulator/config.js.

Dev Lint

The plugin uses ESLint for source code linting. The linting will run automatically on git commit.

$ npm run lint

Dev Test

The plugin uses Mocha for the testing framework, and Chai and Chai-HTTP for its assertions.

$ npm run test

Dev Build

The plugin is automatically built on npm publish. But, you can manually build the plugin using:

$ npm run build-plugin

License

MIT

|M| manicprone

0.0.12

7 years ago

0.0.11

7 years ago

0.0.10

7 years ago

0.0.9

7 years ago

0.0.8

7 years ago

0.0.7

7 years ago

0.0.6

7 years ago

0.0.5

7 years ago

0.0.4

7 years ago

0.0.3

7 years ago

0.0.2

7 years ago